Registry / database / mysql-redis

mysql-redis

JSON →
library0.7.2jsnpmunverified

A caching layer between MySQL (mysql/mysql2) and Redis (redis/ioredis) that transparently caches SELECT query results in Redis to reduce database load. Stable version 0.7.2, last updated in 2018. It hashes queries using configurable algorithms (md5, farmhash32/64, blake2b512, or full query) and stores results in Redis with TTL. Zero mandatory external dependencies (farmhash is optional). Designed for read-heavy workloads with static or rarely updated data. Falls back to MySQL if Redis is unavailable. Not suitable for frequently updated data due to potential cache staleness.

npm install mysql-redis
INSTALL
IMPORT
SIG · MYSQL-REDIS
M
mysql-redis
databasejavascriptv0.7.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MysqlRedis
const { MysqlRedis } = require('mysql-redis')
const MysqlRedis = require('mysql-redis')
CommonJS named export. No default export. Use destructuring.
MysqlRedisAsync
const { MysqlRedisAsync } = require('mysql-redis')
import { MysqlRedisAsync } from 'mysql-redis'
No ESM exports. Package is CommonJS only. Use require.
HashTypes
const { HashTypes } = require('mysql-redis')
const HashTypes = require('mysql-redis').HashTypes
Named export, destructure or access via require result.

Shows synchronous callback usage of MysqlRedis with mysql2 and redis. Cache options are optional.

const mysql = require('mysql2'); const redis = require('redis'); const { MysqlRedis } = require('mysql-redis'); const mysqlConn = mysql.createConnection({ host: 'localhost', user: 'root', database: 'test' }); const redisClient = redis.createClient({ host: 'localhost' }); const cacheOptions = { expire: 3600, keyPrefix: 'sql.', hashType: 'md5' }; const mysqlRedis = new MysqlRedis(mysqlConn, redisClient, cacheOptions); mysqlRedis.query('SELECT * FROM countries', (err, results) => { if (err) throw err; console.log(results); }); mysqlRedis.query('SELECT * FROM countries', (err, results) => { // This call will return cached result from Redis console.log(results); }); mysqlRedis.close(); // close both connections
Debug
Known issues
breakingfarmhash is now optional as of v0.7. If you were using farmhash hash types without explicitly installing farmhash, your code will break.
fix
Install farmhash as a separate dependency: npm install farmhash. Or switch to md5 hash type.
affects: >=0.7.0
gotchaOnly SELECT queries are cached. INSERT, UPDATE, DELETE queries are passed through to MySQL without caching.
fix
Ensure you only use MySQLRedis.query() for SELECT statements. For writes, use the native MySQL connection directly.
affects: *
gotchaCache is never invalidated on writes. Data can become stale if underlying MySQL data changes.
fix
Manually delete or update the Redis keys when data changes, or use a TTL and accept eventual consistency.
affects: *
deprecatedfarmhash32 and farmhash64 hash types use non-cryptographic hashing which may collide with many queries.
fix
Use md5 (default) or blake2b512 for collision-safe hashing, especially with millions of queries.
affects: *
gotchaThe library does not support ESM imports. Using import will fail.
fix
Use require() or use a dynamic import with module.createRequire.
affects: *
Errors
Common errors & fixes
Cannot find module 'farmhash'
Using farmhash32 or farmhash64 hash type without installing farmhash package.
fix
Install farmhash: npm install farmhash, or change hashType to 'md5'.
TypeError: MysqlRedis is not a constructor
Importing the package incorrectly (e.g., using default import or wrong destructuring).
fix
Use const { MysqlRedis } = require('mysql-redis');
Error: Redis connection failed
Redis server is not running or connection options are incorrect.
fix
Ensure Redis is running on localhost:6379 or provide correct connection options to redis.createClient.
Upgrade
Version history
0.7.2latest on npm
Audit
Dependencies
farmhashoptionalOptional dependency for farmhash32/64 hash types. Not needed if using md5 or other hash types.
Agent activity
2 hits · last 30 days
node
2
Resources
mysql-redis — npm install mysql-redis · libregistry