declarative caching with Redis and PostgreSQL
MIT License
Declarative caching for PostgreSQL with redis
Gajus Kuizinas wrote a post on dev.to called "A declarative way to cache PostgreSQL queries using Node.js" that outlined a great pattern for caching with the Slonik PostgreSQL client library. This is an implementation of that pattern that caches not with a local cache but with Redis.
npm install slonik-redis-cache
Option | Description |
---|---|
prefix | Any string. This will be added to the begining of the Redis key. Defaults to slonik . |
queryHashFn | A function that accepts only one string argument. This will be used to convert the Postgres query into a Redis-friendly. String. |
const
{ sql, createPool } = require('slonik'),
redis = require('redis'),
redisCache = require('slonik-redis-cache'),
client = redis.createClient({ ...your connection options...});
const pool = createPool(
'postgres://localhost:5432/testdb', {
interceptors: [
redisCache(client)
]
});
pool.connect(async (connection) => {
let results = await connection.query(sql`SELECT * FROM aTable -- @cache-ttl 10`);
console.log(results);
});