@actvalue/mysql-redis-cache
v4.1.0
Published
wrapper for MySQL queries with Redis caching
Readme
@actvalue/mysql-redis-cache
Wrapper for MySQL queries with Redis caching.
Install
yarn add @actvalue/mysql-redis-cachenpm i @actvalue/mysql-redis-cacheClient Usage
The client is used to execute queries and caching the result.
import { MRCClient } from '@actvalue/mysql-redis-cache';
const mysqlConfig = {
host: '<mysql host>',
port: 3306,
user: '<user>',
password: '<password>',
database: '<db>',
connectionLimit: 5,
};
const redisConfig = {
username: '<user>',
password: '<password>',
socket: {
host: '<redis host>',
port: 6379,
connectionTimeout: 30000,
},
};
// create instance and connect
const mrc = new MRCClient(mysqlConfig, redisConfig);
// execute queries
const query = 'SELECT * FROM table WHERE name = ?';
const params = ['Alberto'];
const paramNames = ['Name'];
const ttl = 3600; // default is 24h
const result = await mrc.queryWithCache(query, params, paramNames, ttl);Server Usage
The server is used to delete cached queries.
import { MRCServer } from '@actvalue/mysql-redis-cache';
const redisConfig = {
username: '<user>',
password: '<password>',
socket: {
host: '<redis host>',
port: 6379,
connectionTimeout: 30000,
},
};
// create instance and connect
const mrc = new MRCServer(redisConfig);
// delete all queries with StoreId = 6 (default: OR logic)
await mrc.dropOutdatedCache(['StoreId'], [6]);
// delete with multiple parameters (OR logic - matches either)
await mrc.dropOutdatedCache(['StoreId', 'UserId'], [6, 123]);
// Deletes cache with StoreId=6 OR UserId=123
// delete requiring ALL parameters to match (AND logic)
await mrc.dropOutdatedCache(['StoreId', 'UserId'], [6, 123], true);
// Deletes cache with BOTH StoreId=6 AND UserId=123