|
| 1 | +const Redis = require('ioredis') |
| 2 | +const InMemoryRedis = require('ioredis-mock') |
| 3 | + |
| 4 | +const { CI, NODE_ENV, REDIS_URL, REDIS_MAX_DB } = process.env |
| 5 | + |
| 6 | +// Do not use real a Redis client for CI, tests, or if the REDIS_URL is not provided |
| 7 | +const useRealRedis = !CI && NODE_ENV !== 'test' && !!REDIS_URL |
| 8 | + |
| 9 | +// By default, every Redis instance supports database numbers 0 - 15 |
| 10 | +const redisMaxDb = REDIS_MAX_DB || 15 |
| 11 | + |
| 12 | +// Enable better stack traces in non-production environments |
| 13 | +const redisOptions = { |
| 14 | + showFriendlyErrorStack: NODE_ENV !== 'production' |
| 15 | +} |
| 16 | + |
| 17 | +class RedisAccessor { |
| 18 | + constructor ({ databaseNumber = 0, prefix = null, allowSetFailures = false } = {}) { |
| 19 | + if (!Number.isInteger(databaseNumber) || databaseNumber < 0 || databaseNumber > redisMaxDb) { |
| 20 | + throw new TypeError( |
| 21 | + `Redis database number must be an integer between 0 and ${redisMaxDb} but was: ${JSON.stringify(databaseNumber)}` |
| 22 | + ) |
| 23 | + } |
| 24 | + |
| 25 | + const redisUrl = `${REDIS_URL}/${databaseNumber}` |
| 26 | + const redisClient = useRealRedis ? new Redis(redisUrl, redisOptions) : new InMemoryRedis() |
| 27 | + this._client = redisClient |
| 28 | + |
| 29 | + this._prefix = prefix ? prefix.replace(/:+$/, '') + ':' : '' |
| 30 | + |
| 31 | + // Allow for graceful failures if a Redis SET operation fails? |
| 32 | + this._allowSetFailures = allowSetFailures === true |
| 33 | + } |
| 34 | + |
| 35 | + /** @private */ |
| 36 | + prefix (key) { |
| 37 | + if (typeof key !== 'string' || !key) { |
| 38 | + throw new TypeError(`Key must be a non-empty string but was: ${JSON.stringify(key)}`) |
| 39 | + } |
| 40 | + |
| 41 | + return this._prefix + key |
| 42 | + } |
| 43 | + |
| 44 | + static translateSetArguments (options = {}) { |
| 45 | + const setArgs = [] |
| 46 | + |
| 47 | + const defaults = { |
| 48 | + newOnly: false, |
| 49 | + existingOnly: false, |
| 50 | + expireIn: null, // No expiration |
| 51 | + rollingExpiration: true |
| 52 | + } |
| 53 | + const opts = { ...defaults, ...options } |
| 54 | + |
| 55 | + if (opts.newOnly === true) { |
| 56 | + if (opts.existingOnly === true) { |
| 57 | + throw new TypeError('Misconfiguration: entry cannot be both new and existing') |
| 58 | + } |
| 59 | + setArgs.push('NX') |
| 60 | + } else if (opts.existingOnly === true) { |
| 61 | + setArgs.push('XX') |
| 62 | + } |
| 63 | + |
| 64 | + if (Number.isFinite(opts.expireIn)) { |
| 65 | + const ttl = Math.round(opts.expireIn) |
| 66 | + if (ttl < 1) { |
| 67 | + throw new TypeError('Misconfiguration: cannot set a TTL of less than 1 millisecond') |
| 68 | + } |
| 69 | + setArgs.push('PX') |
| 70 | + setArgs.push(ttl) |
| 71 | + } |
| 72 | + // otherwise there is no expiration |
| 73 | + |
| 74 | + if (opts.rollingExpiration === false) { |
| 75 | + if (opts.newOnly === true) { |
| 76 | + throw new TypeError('Misconfiguration: cannot keep an existing TTL on a new entry') |
| 77 | + } |
| 78 | + setArgs.push('KEEPTTL') |
| 79 | + } |
| 80 | + |
| 81 | + return setArgs |
| 82 | + } |
| 83 | + |
| 84 | + async set (key, value, options = {}) { |
| 85 | + const fullKey = this.prefix(key) |
| 86 | + |
| 87 | + if (typeof value !== 'string' || !value) { |
| 88 | + throw new TypeError(`Value must be a non-empty string but was: ${JSON.stringify(value)}`) |
| 89 | + } |
| 90 | + |
| 91 | + // Handle optional arguments |
| 92 | + const setArgs = this.constructor.translateSetArguments(options) |
| 93 | + |
| 94 | + try { |
| 95 | + const result = await this._client.set(fullKey, value, ...setArgs) |
| 96 | + return result === 'OK' |
| 97 | + } catch (err) { |
| 98 | + const errorText = `Failed to set value in Redis. |
| 99 | +Key: ${fullKey} |
| 100 | +Error: ${err.message}` |
| 101 | + |
| 102 | + if (this._allowSetFailures === true) { |
| 103 | + // Allow for graceful failure |
| 104 | + console.error(errorText) |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + throw new Error(errorText) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + async get (key) { |
| 113 | + const value = await this._client.get(this.prefix(key)) |
| 114 | + return value |
| 115 | + } |
| 116 | + |
| 117 | + async exists (key) { |
| 118 | + const result = await this._client.exists(this.prefix(key)) |
| 119 | + return result === 1 |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +module.exports = RedisAccessor |
0 commit comments