kwalski / mysql-redis Goto Github PK
View Code? Open in Web Editor NEWmysql redis cache layer
License: MIT License
mysql redis cache layer
License: MIT License
Hello @kwalski !
I followed your instructions regarding setting async version of MysqlRedis:
const { MysqlRedisAsync, HashTypes, Caching } = require("mysql-redis");
const redis = asyncRedis.createClient(settings.REDIS_PORT, settings.REDIS_HOST);
const pool = mysql.createPool({
host: settings.MYSQL_HOST,
user: settings.MYSQL_USER,
password: settings.MYSQL_PASS,
database: settings.MYSQL_DB
});
const promisePool = pool.promise();
const defaultCacheOptions = {
expire: settings.REDIS_CACHE_RETENTION,
keyPrefix: "sql.",
caching: Caching.CACHE
};
const mysqlRedis = new MysqlRedisAsync(
promisePool,
redis,
defaultCacheOptions
);
and then I execute simple query:
query = "SELECT * FROM categories where id=2;"
with async function
[queryOutput, queryDetails]=await mysqlRedis.query(query,
{
keyPrefix: 'sql.',
expire: settings.REDIS_CACHE_RETENTION,
caching: cachingOption
});
exchanging cachingOption with either
Caching.CACHE
or
Caching.REFRESH
I understand that if CACHE is used it uses Redis, if REFRESH it should update Redis with mysql data and return data from mysql
So now the problem...
I did query with Caching.CACHE and it created entry in Redis:
127.0.0.1:6379> keys *
1) "sql.M/LnlpXD5l24tMjRGgSX5w=="
All good
Then I updated DB manually with some small change...
again executing with Caching.CACHE and it gets (now outdated) data from Redis...
so far - ALL GOOD
so now I execute the same function but with Caching.REFRESH
and what should happen here?
from what I see it creates another entry in Redis
127.0.0.1:6379> keys *
1) "sql.Dg/8759875Xj3gS8cOzUbw=="
2) "sql.M/LnlpXD5l24tMjRGgSX5w=="
and it shows valid (updated) data from DB
but then when I switch back to Caching.CACHE it uses first entry (outdated) from Redis and displays wrong data...
shouldn't it be taken from new Redis entry?
Did I misunderstand something?
Hi,
Thanks for this ,
I use this for socket.io, but i found that the connection not release, may i know how can i release connection after used?
My code before
const db = function () {
const redisOptions = {};
const cacheOptions = {
expire: 2629746,// seconds, defaults to 30 days
keyPrefix: "sql.", // default
hashType: HashTypes.blake2b512, //default
caching: Caching.SKIP //default
};
const pool = mysql.createPool({
connectionLimit: 2,
host: env.DB_HOST,
user: env.DB_USERNAME,
password: env.DB_PASSWORD,
database: env.DB_DATABASE
}).promise();
const redis = asyncRedis.createClient();
return new MysqlRedisAsync(
pool,
redis,
cacheOptions
);
}
[data, fields] = db.query('SELECT * FROM USERS');
console.log(data)
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.