receptacle
Advanced tools
Comparing version 1.0.0 to 1.0.1
198
index.js
@@ -1,9 +0,9 @@ | ||
"use strict"; | ||
'use strict' | ||
module.exports = Receptacle; | ||
var toMS = require("ms"); | ||
var cache = Receptacle.prototype; | ||
var counter = new Date % 1e9; | ||
module.exports = Receptacle | ||
var toMS = require('ms') | ||
var cache = Receptacle.prototype | ||
var counter = new Date() % 1e9 | ||
function getUID () { return (Math.random() * 1e9 >>> 0) + (counter++); } | ||
function getUID () { return (Math.random() * 1e9 >>> 0) + (counter++) } | ||
@@ -19,16 +19,16 @@ /** | ||
function Receptacle (options) { | ||
options = options || {}; | ||
this.id = options.id || getUID(); | ||
this.max = options.max || Infinity; | ||
this.items = options.items || []; | ||
this.size = this.items.length; | ||
this.lastModified = new Date(options.lastModified || new Date); | ||
options = options || {} | ||
this.id = options.id || getUID() | ||
this.max = options.max | ||
this.items = options.items || [] | ||
this.size = this.items.length | ||
this.lastModified = new Date(options.lastModified || new Date()) | ||
// Setup initial timers and indexes for the cache. | ||
for (var item, ttl, i = this.items.length; i--;) { | ||
item = this.items[i]; | ||
ttl = new Date(item.expires) - new Date; | ||
this.items[item.key] = item; | ||
if (ttl > 0) this.expire(item.key, ttl); | ||
} | ||
// Setup initial timers and indexes for the cache. | ||
for (var item, ttl, i = this.items.length; i--;) { | ||
item = this.items[i] | ||
ttl = new Date(item.expires) - new Date() | ||
this.items[item.key] = item | ||
if (ttl > 0) this.expire(item.key, ttl) | ||
} | ||
} | ||
@@ -44,4 +44,4 @@ | ||
cache.has = function (key) { | ||
return key in this.items; | ||
}; | ||
return key in this.items | ||
} | ||
@@ -55,11 +55,11 @@ /** | ||
cache.get = function (key) { | ||
if (!this.has(key)) return null; | ||
var record = this.items[key]; | ||
// Update expiry for "refresh" keys | ||
if (record.refresh) this.expire(key, record.refresh); | ||
// Move to front of the line. | ||
this.items.splice(this.items.indexOf(record), 1); | ||
this.items.push(record); | ||
return record.value; | ||
}; | ||
if (!this.has(key)) return null | ||
var record = this.items[key] | ||
// Update expiry for "refresh" keys | ||
if (record.refresh) this.expire(key, record.refresh) | ||
// Move to front of the line. | ||
this.items.splice(this.items.indexOf(record), 1) | ||
this.items.push(record) | ||
return record.value | ||
} | ||
@@ -73,6 +73,6 @@ /** | ||
cache.meta = function (key) { | ||
if (!this.has(key)) return null; | ||
var record = this.items[key]; | ||
if (!("meta" in record)) return null; | ||
return record.meta; | ||
if (!this.has(key)) return null | ||
var record = this.items[key] | ||
if (!('meta' in record)) return null | ||
return record.meta | ||
} | ||
@@ -89,30 +89,30 @@ | ||
cache.set = function (key, value, options) { | ||
var oldRecord = this.items[key]; | ||
var record = this.items[key] = { key: key, value: value }; | ||
// Mark cache as modified. | ||
this.lastModified = new Date(); | ||
var oldRecord = this.items[key] | ||
var record = this.items[key] = { key: key, value: value } | ||
// Mark cache as modified. | ||
this.lastModified = new Date() | ||
if (oldRecord) { | ||
// Replace an old key. | ||
clearInterval(oldRecord.timeout); | ||
this.items.splice(this.items.indexOf(oldRecord), 1, record); | ||
} else { | ||
// Remove least used item if needed. | ||
if (this.items.length >= this.max) this.delete(this.items[0].key); | ||
// Add a new key. | ||
this.items.unshift(record); | ||
this.size++; | ||
} | ||
if (oldRecord) { | ||
// Replace an old key. | ||
clearInterval(oldRecord.timeout) | ||
this.items.splice(this.items.indexOf(oldRecord), 1, record) | ||
} else { | ||
// Remove least used item if needed. | ||
if (this.items.length >= this.max) this.delete(this.items[0].key) | ||
// Add a new key. | ||
this.items.unshift(record) | ||
this.size++ | ||
} | ||
if (options) { | ||
// Setup key expiry. | ||
if ("ttl" in options) this.expire(key, options.ttl); | ||
// Store user options in the record. | ||
if ("meta" in options) record.meta = options.meta; | ||
// Mark a auto refresh key. | ||
if (options.refresh) record.refresh = options.ttl; | ||
} | ||
if (options) { | ||
// Setup key expiry. | ||
if ('ttl' in options) this.expire(key, options.ttl) | ||
// Store user options in the record. | ||
if ('meta' in options) record.meta = options.meta | ||
// Mark a auto refresh key. | ||
if (options.refresh) record.refresh = options.ttl | ||
} | ||
return this; | ||
}; | ||
return this | ||
} | ||
@@ -126,11 +126,11 @@ /** | ||
cache.delete = function (key) { | ||
var record = this.items[key]; | ||
if (!record) return false; | ||
this.lastModified = new Date(); | ||
this.items.splice(this.items.indexOf(record), 1); | ||
clearTimeout(record.timeout); | ||
delete this.items[key]; | ||
this.size--; | ||
return this; | ||
}; | ||
var record = this.items[key] | ||
if (!record) return false | ||
this.lastModified = new Date() | ||
this.items.splice(this.items.indexOf(record), 1) | ||
clearTimeout(record.timeout) | ||
delete this.items[key] | ||
this.size-- | ||
return this | ||
} | ||
@@ -145,11 +145,11 @@ /** | ||
cache.expire = function (key, ttl) { | ||
var ms = ttl || 0; | ||
var record = this.items[key]; | ||
if (!record) return this; | ||
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.expires = Number(new Date) + ms; | ||
return this; | ||
var ms = ttl || 0 | ||
var record = this.items[key] | ||
if (!record) return this | ||
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.expires = Number(new Date()) + ms | ||
return this | ||
} | ||
@@ -162,5 +162,5 @@ | ||
cache.clear = function () { | ||
for (var i = this.items.length; i--;) this.delete(this.items[i].key); | ||
return this; | ||
}; | ||
for (var i = this.items.length; i--;) this.delete(this.items[i].key) | ||
return this | ||
} | ||
@@ -172,21 +172,21 @@ /** | ||
cache.toJSON = function () { | ||
var items = new Array(this.items.length); | ||
var item; | ||
for (var i = items.length; i--;) { | ||
item = this.items[i]; | ||
items[i] = { | ||
key: item.key, | ||
meta: item.meta, | ||
value: item.value, | ||
expires: item.expires, | ||
refresh: item.refresh, | ||
}; | ||
} | ||
var items = new Array(this.items.length) | ||
var item | ||
for (var i = items.length; i--;) { | ||
item = this.items[i] | ||
items[i] = { | ||
key: item.key, | ||
meta: item.meta, | ||
value: item.value, | ||
expires: item.expires, | ||
refresh: item.refresh | ||
} | ||
} | ||
return { | ||
id: this.id, | ||
max: this.max, | ||
lastModified: this.lastModified, | ||
items: items | ||
}; | ||
}; | ||
return { | ||
id: this.id, | ||
max: this.max, | ||
lastModified: this.lastModified, | ||
items: items | ||
} | ||
} |
{ | ||
"name": "receptacle", | ||
"version": "1.0.0", | ||
"description": "In memory cache lru cache with ttl support.", | ||
"version": "1.0.1", | ||
"author": "Dylan Piercey <pierceydylan@gmail.com>", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/DylanPiercey/receptacle" | ||
"bugs": "https://github.com/DylanPiercey/receptacle/issues", | ||
"dependencies": { | ||
"ms": "^0.7.1" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^13.1.0", | ||
"bundle-collapser": "^1.2.1", | ||
"coveralls": "^2.11.14", | ||
"exorcist": "^0.4.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.1.2", | ||
"snazzy": "^5.0.0", | ||
"standard": "^8.4.0", | ||
"uglifyjs": "^2.4.10" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"homepage": "https://github.com/DylanPiercey/receptacle", | ||
"keywords": [ | ||
"cache", | ||
"expires", | ||
"lru", | ||
"ttl", | ||
"expires", | ||
"map", | ||
"memory", | ||
"receptacle", | ||
"memory" | ||
"ttl" | ||
], | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp-mocha": "^2.1.3" | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/DylanPiercey/receptacle" | ||
}, | ||
"dependencies": { | ||
"ms": "^0.7.1" | ||
"scripts": { | ||
"build": "make build", | ||
"coveralls": "make coveralls", | ||
"lint": "make lint", | ||
"test": "npm run lint && make test", | ||
"test-ci": "npm run lint && make test-ci" | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"GIT_COMMIT", | ||
"describe", | ||
"it", | ||
"before", | ||
"beforeEach", | ||
"after", | ||
"afterEach" | ||
], | ||
"ignore": [ | ||
"dist/" | ||
] | ||
} | ||
} |
@@ -1,2 +0,41 @@ | ||
# Receptacle. | ||
<h1 align="center"> | ||
<!-- Logo --> | ||
Receptacle | ||
<br/> | ||
<!-- Stability --> | ||
<a href="https://nodejs.org/api/documentation.html#documentation_stability_index"> | ||
<img src="https://img.shields.io/badge/stability-stable-brightgreen.svg?style=flat-square" alt="API stability"/> | ||
</a> | ||
<!-- Standard --> | ||
<a href="https://github.com/feross/standard"> | ||
<img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square" alt="Standard"/> | ||
</a> | ||
<!-- NPM version --> | ||
<a href="https://npmjs.org/package/receptacle"> | ||
<img src="https://img.shields.io/npm/v/receptacle.svg?style=flat-square" alt="NPM version"/> | ||
</a> | ||
<!-- Travis build --> | ||
<a href="https://travis-ci.org/DylanPiercey/receptacle"> | ||
<img src="https://img.shields.io/travis/DylanPiercey/receptacle.svg?style=flat-square" alt="Build status"/> | ||
</a> | ||
<!-- Coveralls coverage --> | ||
<a href="https://coveralls.io/github/DylanPiercey/receptacle"> | ||
<img src="https://img.shields.io/coveralls/DylanPiercey/receptacle.svg?style=flat-square" alt="Test Coverage"/> | ||
</a> | ||
<!-- File size --> | ||
<a href="https://github.com/DylanPiercey/receptacle/blob/master/dist/receptacle.js"> | ||
<img src="https://badge-size.herokuapp.com/DylanPiercey/receptacle/master/dist/receptacle.js?style=flat-square" alt="File size"/> | ||
</a> | ||
<!-- Downloads --> | ||
<a href="https://npmjs.org/package/receptacle"> | ||
<img src="https://img.shields.io/npm/dm/receptacle.svg?style=flat-square" alt="Downloads"/> | ||
</a> | ||
<!-- Gitter chat --> | ||
<a href="https://gitter.im/DylanPiercey/receptacle"> | ||
<img src="https://img.shields.io/gitter/room/DylanPiercey/receptacle.svg?style=flat-square" alt="Gitter Chat"/> | ||
</a> | ||
</h1> | ||
In memory cache for node and the browser that supports `lru` and `ttl` algorithms. | ||
@@ -6,4 +45,5 @@ | ||
# Installation | ||
## Installation | ||
#### Npm | ||
@@ -14,4 +54,13 @@ ```console | ||
# Example | ||
#### [Download](https://raw.githubusercontent.com/DylanPiercey/receptacle/master/dist/receptacle.js) | ||
```html | ||
<script type="text/javascript" src="receptacle.js"></script> | ||
<script> | ||
define(['receptacle'], function (receptacle) {...}) // AMD | ||
window.receptacle // Global rill if no module system in place. | ||
</script> | ||
``` | ||
## Example | ||
```js | ||
@@ -39,3 +88,3 @@ var Receptacle = require('receptacle'); | ||
# Serialization | ||
## Serialization | ||
You can easily serialize and rehydrate your cache as JSON. | ||
@@ -56,3 +105,3 @@ | ||
# API | ||
## API | ||
@@ -97,4 +146,4 @@ ###`Receptacle({ max=Infinity, items=[], id=# })` | ||
* Use gulp to run tests. | ||
* Use `npm test` to run tests. | ||
Please feel free to create a PR! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
145
0
10784
9
3
167
1