|
| 1 | +const util = require('util') |
| 2 | +const CacheStore = require('./cache-store') |
| 3 | + |
| 4 | +function RedisStore(sails) { |
| 5 | + CacheStore.call(this, sails) |
| 6 | +} |
| 7 | + |
| 8 | +RedisStore.prototype = Object.create(CacheStore.prototype) |
| 9 | +RedisStore.prototype.constructor = RedisStore |
| 10 | +RedisStore.prototype.getStore = CacheStore.prototype.getStore |
| 11 | + |
| 12 | +/** |
| 13 | + * Retrieves the value associated with the specified key from the cache store. |
| 14 | + * @param {string} key - The key to retrieve the value for. |
| 15 | + * @param {any | Function} [defaultValueOrCallback] - Optional. Default value or callback function. |
| 16 | + * @returns {Promise<any>} Promise resolving to the value associated with the key or default value. |
| 17 | + */ |
| 18 | +RedisStore.prototype.get = async function (key, defaultValueOrCallback) { |
| 19 | + const store = await this.getStore() |
| 20 | + const value = await store.leaseConnection(async function (db) { |
| 21 | + const cacheHit = await util.promisify(db.get).bind(db)(key) |
| 22 | + |
| 23 | + if (cacheHit === null) { |
| 24 | + if (typeof defaultValueOrCallback === 'function') { |
| 25 | + const defaultValue = await defaultValueOrCallback() |
| 26 | + return defaultValue |
| 27 | + } else { |
| 28 | + return defaultValueOrCallback |
| 29 | + } |
| 30 | + } else { |
| 31 | + return JSON.parse(cacheHit) |
| 32 | + } |
| 33 | + }) |
| 34 | + return value |
| 35 | +} |
| 36 | +/** |
| 37 | + * Sets the value associated with the specified key in the cache store. |
| 38 | + * @param {string} key - The key for the value. |
| 39 | + * @param {any} value - The value to store. |
| 40 | + * @param {number} [ttlInSeconds] - Optional. Time to live for the key-value pair in seconds. |
| 41 | + * @returns {Promise<void>} Promise indicating completion of the set operation. |
| 42 | + */ |
| 43 | +RedisStore.prototype.set = async function (key, value, ttlInSeconds) { |
| 44 | + const store = await this.getStore() |
| 45 | + await store.leaseConnection(async function (db) { |
| 46 | + if (ttlInSeconds) { |
| 47 | + await util.promisify(db.setex).bind(db)( |
| 48 | + key, |
| 49 | + ttlInSeconds, |
| 50 | + JSON.stringify(value), |
| 51 | + ) |
| 52 | + } else { |
| 53 | + await util.promisify(db.set).bind(db)(key, JSON.stringify(value)) |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Checks if the cache store contains the specified key. |
| 60 | + * @param {string} key - The key to check. |
| 61 | + * @returns {Promise<boolean>} Promise resolving to true if the key exists, false otherwise. |
| 62 | + */ |
| 63 | +RedisStore.prototype.has = async function (key) { |
| 64 | + const cacheHit = await this.get(key) |
| 65 | + if (cacheHit) return true |
| 66 | + return false |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Deletes the key-value pair associated with the specified key from the cache store. |
| 71 | + * @param {string | string[]} key - The key to delete. |
| 72 | + * @returns {Promise<void>} Promise indicating completion of the delete operation. |
| 73 | + */ |
| 74 | +RedisStore.prototype.delete = async function (key) { |
| 75 | + const store = await this.getStore() |
| 76 | + const deletedCount = await store.leaseConnection(async function (db) { |
| 77 | + return await util.promisify(db.del).bind(db)(key) |
| 78 | + }) |
| 79 | + return deletedCount |
| 80 | +} |
| 81 | +/** |
| 82 | + * Retrieves the value associated with the specified key from the cache store. |
| 83 | + * If the key exists in the cache, returns the corresponding value. |
| 84 | + * If the key does not exist in the cache, the provided default value or the result of the callback function will be stored in the cache and returned. |
| 85 | + * @param {string} key - The key to retrieve the value for. |
| 86 | + * @param {any | Function} [defaultValueOrCallback] - Optional. Default value or callback function to compute the default value. |
| 87 | + * @param {number} [ttlInSeconds] - Optional. Time to live for the key-value pair in seconds. |
| 88 | + * @returns {Promise<any>} A promise that resolves to the value associated with the key, or the default value if the key does not exist in the cache. |
| 89 | + */ |
| 90 | +RedisStore.prototype.fetch = async function ( |
| 91 | + key, |
| 92 | + defaultValueOrCallback, |
| 93 | + ttlInSeconds, |
| 94 | +) { |
| 95 | + const cacheHit = await this.get(key) |
| 96 | + if (!cacheHit) { |
| 97 | + let defaultValue |
| 98 | + if (typeof defaultValueOrCallback === 'function') { |
| 99 | + defaultValue = await defaultValueOrCallback() |
| 100 | + } else { |
| 101 | + defaultValue = defaultValueOrCallback |
| 102 | + } |
| 103 | + await this.set(key, defaultValue, ttlInSeconds) |
| 104 | + return defaultValue |
| 105 | + } else { |
| 106 | + return cacheHit |
| 107 | + } |
| 108 | +} |
| 109 | +/** |
| 110 | + * Adds a key-value pair to the cache store if the key does not already exist. |
| 111 | + * If the key already exists, the value is not updated. |
| 112 | + * @param {string} key - The key for the value. |
| 113 | + * @param {any} value - The value to store. |
| 114 | + * @param {number} [ttlInSeconds] - Optional. Time to live for the key-value pair in seconds. |
| 115 | + * @returns {Promise<boolean>} A promise that resolves to true if the key was added successfully, false if the key already exists. |
| 116 | + */ |
| 117 | +RedisStore.prototype.add = async function (key, value, ttlInSeconds) { |
| 118 | + const cacheHit = await this.get(key) |
| 119 | + if (!cacheHit) { |
| 120 | + await this.set(key, value, ttlInSeconds) |
| 121 | + return true |
| 122 | + } else { |
| 123 | + return false |
| 124 | + } |
| 125 | +} |
| 126 | +/** |
| 127 | + * Retrieves and removes the value associated with the specified key from the cache store. |
| 128 | + * If the key exists in the cache, returns the corresponding value and removes the key-value pair. |
| 129 | + * If the key does not exist, returns null. |
| 130 | + * @param {string} key - The key to retrieve and remove the value for. |
| 131 | + * @returns {Promise<any>} A promise that resolves to the value associated with the key, or null if the key does not exist in the cache. |
| 132 | + */ |
| 133 | +RedisStore.prototype.pull = async function (key) { |
| 134 | + const value = await this.get(key) |
| 135 | + await this.delete(key) |
| 136 | + return value |
| 137 | +} |
| 138 | +/** |
| 139 | + * Stores the specified key-value pair in the cache store indefinitely. |
| 140 | + * The key-value pair will not have an expiry time and will remain in the cache until explicitly removed. |
| 141 | + * @param {string} key - The key for the value. |
| 142 | + * @param {any} value - The value to store. |
| 143 | + * @returns {Promise<void>} A promise indicating completion of the operation. |
| 144 | + */ |
| 145 | +RedisStore.prototype.forever = async function (key, value) { |
| 146 | + await this.set(key, value) |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Destroys the cache store, removing all stored key-value pairs. |
| 151 | + * @returns {Promise<void>} A promise indicating completion of the operation. |
| 152 | + */ |
| 153 | +RedisStore.prototype.destroy = async function () { |
| 154 | + const store = await this.getStore() |
| 155 | + await store.leaseConnection(async function (db) { |
| 156 | + return await util.promisify(db.flushall).bind(db)('ASYNC') |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +module.exports = RedisStore |
0 commit comments