// cache.js // Copyright (C) 2024 DTP Technologies, LLC // All Rights Reserved 'use strict'; import { SiteService } from '../../lib/site-lib.js'; export default class CacheService extends SiteService { static get slug () { return 'cache'; } static get name ( ) { return 'CacheService'; } constructor (dtp) { super(dtp, CacheService); } async set (name, value) { return this.dtp.redis.set(name, value); } async setEx (name, seconds, value) { return this.dtp.redis.setex(name, seconds, value); } async get (name) { return this.dtp.redis.get(name); } async setObject (name, value) { return this.dtp.redis.set(name, JSON.stringify(value)); } async setObjectEx (name, seconds, value) { return this.dtp.redis.setex(name, seconds, JSON.stringify(value)); } async getObject (name) { const value = await this.dtp.redis.get(name); if (!value) { return; // undefined } return JSON.parse(value); } async hashSet (name, field, value) { return this.dtp.redis.hset(name, field, value); } async hashInc (name, field, value) { return this.dtp.redis.hincrby(name, field, value); } async hashIncFloat (name, field, value) { return this.dtp.redis.hincrbyfloat(name, field, value); } async hashGet (name, field) { return this.dtp.redis.hget(name, field); } async hashGetAll (name) { return this.dtp.redis.hgetall(name); } async hashDelete (name, field) { return this.dtp.redis.hdel(name, field); } async del (name) { return this.dtp.redis.del(name); } getKeys (pattern) { return new Promise((resolve, reject) => { return this.dtp.redis.keys(pattern, (err, response) => { if (err) { return reject(err); } return resolve(response); }); }); } }