diff --git a/.travis.yml b/.travis.yml index 1ec9045..b18eb9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ node_js: - 12 - 14 - 16 + - 18 diff --git a/README.md b/README.md index 163d81e..1ada37c 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,6 @@ Buckets: - `per_interval` (number): is the amount of tokens that the bucket receive on every interval. - `interval` (number): defines the interval in milliseconds. - `unlimited` (boolean = false): unlimited requests (skip take). -- `enable_cache` (boolean = false): caching non-conformant answers of the bucket until the next drip. Ping: diff --git a/lib/db.js b/lib/db.js index 49a125f..3b7f65d 100644 --- a/lib/db.js +++ b/lib/db.js @@ -4,7 +4,6 @@ const _ = require('lodash'); const async = require('async'); const utils = require('./utils'); const Redis = require('ioredis'); -const TTLCache = require('@isaacs/ttlcache'); const { validateParams } = require('./validation'); const DBPing = require("./db_ping"); const EventEmitter = require('events').EventEmitter; @@ -107,11 +106,6 @@ class LimitDBRedis extends EventEmitter { this.emit('node error', err, node); }); - this.cache = new TTLCache({ - max: config.cacheSize || 1000, - ttl: 1000, - checkAgeOnGet: true - }); } #setupPing(config) { @@ -188,10 +182,6 @@ class LimitDBRedis extends EventEmitter { return Math.ceil((now + msToCompletion) / 1000); } - cacheEnabled(bucket) { - return bucket.enable_cache; - } - /** * Take N elements from a bucket if available. * @@ -207,7 +197,6 @@ class LimitDBRedis extends EventEmitter { const bucket = this.buckets[params.type]; const bucketKeyConfig = this.bucketKeyConfig(bucket, params); - const cacheEnabled = this.cacheEnabled(bucketKeyConfig); const key = `${params.type}:${params.key}`; const count = this._determineCount({ @@ -223,17 +212,9 @@ class LimitDBRedis extends EventEmitter { reset: Math.ceil(Date.now() / 1000), limit: bucketKeyConfig.size, delayed: false, - cached: false }); } - if (cacheEnabled) { - const cached = this.cache.get(key); - if (cached) { - return callback(null, cached); - } - } - this.redis.take(key, bucketKeyConfig.ms_per_interval || 0, bucketKeyConfig.size, @@ -255,18 +236,8 @@ class LimitDBRedis extends EventEmitter { reset: Math.ceil(reset / 1000), limit: bucketKeyConfig.size, delayed: false, - cached: false }; - if (cacheEnabled && conformant === false) { - // cache if bucket is empty and only until almost the moment it should get a new token - const msUntilFull = reset - currentMS; - const ttl = Math.floor(msUntilFull / bucketKeyConfig.size * 0.99); - if (ttl > 1) { // pointless to cache less than 1ms and prevent against negative values - this.cache.set(key, Object.assign({}, res, {cached: true}), { ttl }); - } - } - return callback(null, res); }); } diff --git a/lib/utils.js b/lib/utils.js index 74e51af..12b5fd9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -17,7 +17,6 @@ function normalizeTemporals(params) { 'interval', 'size', 'unlimited', - 'enable_cache' ]); INTERVAL_SHORTCUTS.forEach(intervalShortcut => { diff --git a/package.json b/package.json index 29fd1d6..85683f8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "author": "Auth0", "license": "MIT", "dependencies": { - "@isaacs/ttlcache": "^1.4.0", "async": "^2.6.1", "disyuntor": "^3.5.0", "ioredis": "^4.28.5", diff --git a/test/db.tests.js b/test/db.tests.js index d3f6b4d..6f4da58 100644 --- a/test/db.tests.js +++ b/test/db.tests.js @@ -530,79 +530,6 @@ describe('LimitDBRedis', () => { }); }); - it('should cache buckets intervals until their reset', (done) => { - db.take({type: 'cached', key: 'test', count: 3}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, true); - assert.equal(res.remaining, 0); - db.take({type: 'cached', key: 'test'}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, false); - assert.equal(res.remaining, 0); - assert.equal(res.cached, false); - const ttl = db.cache.getRemainingTTL('cached:test'); - assert(ms('30m') > ttl); - assert(ms('29m') < ttl); - done(); - }); - }); - }); - - it('should cache buckets accurately in small windows', (done) => { - db.take({type: 'cached', key: 'faster', count: 3}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, true); - assert.equal(res.remaining, 0); - db.take({type: 'cached', key: 'faster'}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, false); - assert.equal(res.remaining, 0); - assert.equal(res.cached, false); - const ttl = db.cache.getRemainingTTL('cached:faster'); - assert(ms('1s') > ttl); - assert(ms('900ms') < ttl); - done(); - }); - }); - }); - - it('should not cache when enable_cache is undefined', (done) => { - db.take({type: 'cached', key: 'disabled', count: 5}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, true); - assert.equal(res.remaining, 0); - db.take({type: 'cached', key: 'disabled'}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, false); - assert.equal(res.remaining, 0); - assert.equal(res.cached, false); - assert.equal(db.cache.has('cached:disabled'), false); - done(); - }); - }); - }); - - it('should indicate the response came from cache', (done) => { - db.take({type: 'cached', key: 'test', count: 3}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, true); - assert.equal(res.remaining, 0); - db.take({type: 'cached', key: 'test'}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, false); - assert.equal(res.remaining, 0); - assert.equal(res.cached, false); - - db.take({type: 'cached', key: 'test'}, (err, res) => { - assert.ifError(err); - assert.equal(res.conformant, false); - assert.equal(res.remaining, 0); - assert.equal(res.cached, true); - done(); - }); - }); - }); - }); }); describe('PUT', () => {