secondary-cache
Advanced tools
+1
-1
@@ -43,3 +43,3 @@ import { ILRUCacheOptions, LRUCache } from './lru-cache'; | ||
| * @example | ||
| * const cache = new Cache({ max: 100, expires: 60000, fixedCapacity: 50 }); | ||
| * const cache = new Cache({ capacity: 100, expires: 60000, fixedCapacity: 50 }); | ||
| */ | ||
@@ -46,0 +46,0 @@ constructor(options?: ICacheOptions|number); |
+8
-5
@@ -80,3 +80,3 @@ "use strict"; | ||
| LRUCache.prototype.set = function (id, value, expires) { | ||
| let event, oldValue; | ||
| let event; | ||
| if (this.cleanInterval > 0 && Date.now() - this.lastCleanTime >= this.cleanInterval) { | ||
@@ -86,4 +86,10 @@ setImmediate(this.clearExpires); | ||
| let item = this._cacheLRU[id]; | ||
| const oldValue = item && item.value; | ||
| if (item !== undefined) { | ||
| oldValue = item.value; | ||
| event = 'update'; | ||
| } else { | ||
| event = 'add'; | ||
| } | ||
| this.emit('before_' + event, id, value, oldValue); | ||
| if (item !== undefined) { | ||
| item.value = value; | ||
@@ -97,3 +103,2 @@ if (expires <= 0) { | ||
| } | ||
| event = 'update'; | ||
| if (this._lruQueue) { | ||
@@ -103,3 +108,2 @@ this._lruQueue.use(item); | ||
| } else { | ||
| event = 'add'; | ||
| if (expires > 0) { | ||
@@ -112,3 +116,2 @@ expires = Date.now() + expires; | ||
| } | ||
| this.emit('before_' + event, id, value, oldValue); | ||
| item = new LRUCacheItem(id, value, expires); | ||
@@ -115,0 +118,0 @@ this._cacheLRU[id] = item; |
+2
-2
| { | ||
| "name": "secondary-cache", | ||
| "version": "2.0.0", | ||
| "version": "2.0.1", | ||
| "description": "support secondary cache mechanism. the first level cache is fixed memory-resident always with the highest priority. the second level is the LRU cache.", | ||
@@ -49,3 +49,3 @@ "homepage": "https://github.com/snowyu/secondary-cache.js", | ||
| "eslint-plugin-tsdoc": "^0.2.17", | ||
| "mocha": "~10.2.0", | ||
| "mocha": "~10.6.0", | ||
| "prettier": "^2.8.8", | ||
@@ -52,0 +52,0 @@ "sinon": "~15.1.0", |
+1
-1
@@ -1,2 +0,2 @@ | ||
| ## Secondary Cache [](http://travis-ci.org/snowyu/secondary-cache.js) [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) | ||
| ## Secondary Cache [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) [](https://npmjs.org/package/secondary-cache) | ||
@@ -3,0 +3,0 @@ |
+1
-1
@@ -43,3 +43,3 @@ import { ILRUCacheOptions, LRUCache } from './lru-cache'; | ||
| * @example | ||
| * const cache = new Cache({ max: 100, expires: 60000, fixedCapacity: 50 }); | ||
| * const cache = new Cache({ capacity: 100, expires: 60000, fixedCapacity: 50 }); | ||
| */ | ||
@@ -46,0 +46,0 @@ constructor(options?: ICacheOptions|number); |
+8
-5
@@ -85,3 +85,3 @@ import {eventable} from 'events-ex' | ||
| LRUCache.prototype.set = function(id, value, expires) { | ||
| let event, oldValue; | ||
| let event; | ||
| if (this.cleanInterval > 0 && Date.now() - this.lastCleanTime >= this.cleanInterval) { | ||
@@ -91,4 +91,10 @@ setImmediate(this.clearExpires); | ||
| let item = this._cacheLRU[id]; | ||
| const oldValue = item && item.value; | ||
| if (item !== undefined) { | ||
| oldValue = item.value; | ||
| event = 'update'; | ||
| } else { | ||
| event = 'add'; | ||
| } | ||
| this.emit('before_' + event, id, value, oldValue); | ||
| if (item !== undefined) { | ||
| item.value = value; | ||
@@ -102,3 +108,2 @@ if (expires <= 0) { | ||
| } | ||
| event = 'update'; | ||
| if (this._lruQueue) { | ||
@@ -108,3 +113,2 @@ this._lruQueue.use(item); | ||
| } else { | ||
| event = 'add'; | ||
| if (expires > 0) { | ||
@@ -117,3 +121,2 @@ expires = Date.now() + expires; | ||
| } | ||
| this.emit('before_' + event, id, value, oldValue); | ||
| item = new LRUCacheItem(id, value, expires); | ||
@@ -120,0 +123,0 @@ this._cacheLRU[id] = item; |
@@ -152,2 +152,12 @@ import chai from 'chai' | ||
| cache = Cache(2); | ||
| it('should listen to "before_add" event', function(done) { | ||
| var expected; | ||
| expected = Math.random(); | ||
| cache.on('before_add', function(key, value) { | ||
| key.should.be.equal('before_add_key'); | ||
| value.should.be.equal(expected); | ||
| done(); | ||
| }); | ||
| cache.set('before_add_key', expected); | ||
| }); | ||
| it('should listen to "add" event', function(done) { | ||
@@ -163,2 +173,17 @@ var expected; | ||
| }); | ||
| it('should listen to "before_update" event', function(done) { | ||
| var newValue, oldValue; | ||
| newValue = Math.random(); | ||
| oldValue = cache.get('before_add_key'); | ||
| should.exist(oldValue); | ||
| cache.on('before_update', function(key, value, aOldValue) { | ||
| key.should.be.equal('before_add_key'); | ||
| value.should.be.equal(newValue); | ||
| aOldValue.should.be.equal(oldValue); | ||
| done(); | ||
| }); | ||
| cache.set('before_add_key', newValue); | ||
| oldValue = cache.get('before_add_key'); | ||
| oldValue.should.be.equal(newValue); | ||
| }); | ||
| it('should listen to "update" event', function(done) { | ||
@@ -165,0 +190,0 @@ var newValue, oldValue; |
95780
0.91%2513
1.25%