Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Immediately remove a key from the cache.
###`#expire(key, [ms=0])`
Update the expire time for a key. You can also use any valid [ms](https://github.com/rauchg/ms.js) string for a timeout.

###`#on('expiration', function)`
Add a callback for handling expiration events. The handler will be passed a single parameter,
the key name that was just expired. Note, the handler is called AFTER the key has been removed
from the Receptacle.

###`#clear()`
Remove all keys from the cache.

Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ namespace Receptacle {
key: string;
value: T;
}

export Handler(key: string) : void

export enum Events {
eviction,
}
}

class Receptacle<T, X = undefined> {
Expand All @@ -37,6 +43,7 @@ class Receptacle<T, X = undefined> {
public max: number;
public items: Receptacle.Items<T>[];
public size: number;
public on(event: Receptacle.Event, handler: Receptacle.Handler): void;
public has(key: string): boolean;
public get(key: string): T|null;
public meta(key: string): X|undefined;
Expand Down
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module.exports = Receptacle
var toMS = require('ms')
var cache = Receptacle.prototype
console.dir(cache)
var counter = new Date() % 1e9
var EventEmitter = require('events')

function getUID () { return (Math.random() * 1e9 >>> 0) + (counter++) }

Expand All @@ -21,6 +23,7 @@ function Receptacle (options) {
this.max = options.max || Infinity
this.items = options.items || []
this._lookup = {}
this._events = new EventEmitter()
this.size = this.items.length
this.lastModified = new Date(options.lastModified || new Date())

Expand All @@ -34,6 +37,17 @@ function Receptacle (options) {
}
}

/**
* Currently only the evict type is supported.
*
* @param {String} event - the event name.
* @param {Receptacle.Event} handler - the event handler
* @return {Boolean}
*/
cache.on = function (event, handler) {
return this._events.on(event, handler.bind(this))
}

/**
* Tests if a key is currently in the cache.
* Does not check if slot is empty.
Expand Down Expand Up @@ -144,7 +158,10 @@ cache.expire = function (key, ttl) {
if (typeof ms === 'string') ms = toMS(ttl)
if (typeof ms !== 'number') throw new TypeError('Expiration time must be a string or number.')
clearTimeout(record.timeout)
record.timeout = setTimeout(this.delete.bind(this, record.key), ms)
record.timeout = setTimeout(function () {
this.delete(record.key)
this._events.emit('eviction', record.key)
}.bind(this), ms)
record.expires = Number(new Date()) + ms
return this
}
Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe('Receptacle', function () {
})
})

describe('#on', function () {
it('should emit an expiration event', function (done) {
var cache = new Receptacle()
cache.on('eviction', function (key) {
console.log('evicted: ' + key)
done()
})
cache.set('a', 1, { ttl: 200 })
})
})

describe('#meta', function () {
it('should return a set key', function () {
var cache = new Receptacle()
Expand Down