Comparing version 0.4.1 to 0.5.0
33
index.js
@@ -32,5 +32,6 @@ "use strict"; | ||
if (this.cache[url] !== undefined) | ||
if (this.values[url] !== undefined) | ||
{ | ||
delete this.cache[url]; | ||
delete this.expiries[url]; | ||
delete this.values[url]; | ||
} | ||
@@ -40,3 +41,4 @@ } | ||
{ | ||
this.cache = {}; | ||
this.expiries = {}; | ||
this.values = {}; | ||
} | ||
@@ -51,10 +53,5 @@ }; | ||
removeExpired(url, this.cache); | ||
removeExpired(url, this.expiries, this.values); | ||
if (this.cache[url] !== undefined) | ||
{ | ||
return Promise.resolve( this.cache[url].value ); | ||
} | ||
return Promise.reject( new Error("key not found") ); | ||
return this.values[url]; | ||
}; | ||
@@ -73,7 +70,4 @@ | ||
this.cache[url] = | ||
{ | ||
expiryTime: Date.now() + expiryTime, | ||
value: value | ||
}; | ||
this.expiries[url] = Date.now() + expiryTime; | ||
this.values[url] = value; | ||
}; | ||
@@ -127,9 +121,10 @@ | ||
function removeExpired(url, cache) | ||
function removeExpired(url, expiries, values) | ||
{ | ||
if (cache[url] !== undefined) | ||
if (values[url] !== undefined) | ||
{ | ||
if ( cache[url].expiryTime < Date.now() ) | ||
if ( expiries[url] < Date.now() ) | ||
{ | ||
delete cache[url]; | ||
delete expiries[url]; | ||
delete values[url]; | ||
} | ||
@@ -136,0 +131,0 @@ } |
{ | ||
"name": "urlcache", | ||
"description": "URL key-value cache and store.", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"license": "MIT", | ||
@@ -25,6 +25,6 @@ "homepage": "https://github.com/stevenvachon/urlcache", | ||
"bhttp": "^1.2.1", | ||
"bluebird": "^2.10.2", | ||
"browserify": "^11.2.0", | ||
"chai": "^3.3.0", | ||
"chai-as-promised": "^5.1.0", | ||
"es6-promise": "^3.0.2", | ||
"mocha": "^2.3.3", | ||
@@ -31,0 +31,0 @@ "node-static": "~0.7.7", |
@@ -1,2 +0,2 @@ | ||
# urlcache [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][david-image]][david-url] | ||
# urlcache [![NPM Version][npm-image]][npm-url] [![Bower Version][bower-image]][bower-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][david-image]][david-url] | ||
@@ -12,3 +12,2 @@ > URL key-value cache and store. | ||
``` | ||
**Note:** Node.js v0.10 will need a `Promise` polyfill. | ||
@@ -30,22 +29,10 @@ | ||
### .get(url) | ||
Returns a `Promise` with the stored value of `url`. If no such value exists, the promise will be rejected. | ||
```js | ||
cache.get("url").then(function(value) { | ||
console.log(value); //-> "value" | ||
}); | ||
Returns the stored value of `url`. If no such value exists, `undefined` will be returned. | ||
cache.get("unstored").catch(function(error) { | ||
// not in cache (or value is a rejected Promise) | ||
}); | ||
``` | ||
### .set(url, value[, expiryTime]) | ||
Stores `value` (any type) into `url` key. Optionally, define `expiryTime` to override `options.expiryTime`. **Note:** any value passed in gets wrapped in `Promise.resolve`. | ||
Stores `value` (any type) into `url` key. Optionally, define `expiryTime` to override `options.expiryTime`. | ||
```js | ||
cache.set("url", {"key":"value"}); | ||
cache.get("url"); //-> {"key":"value"} | ||
cache.get("url").then(function(value) { | ||
console.log(value); //-> {"key":"value"} | ||
}); | ||
cache.set("url", new Promise(function(resolve, reject) { | ||
@@ -58,3 +45,3 @@ // set value after some delayed event | ||
cache.get("url").then(function(value) { | ||
Promise.resolve(cache.get("url")).then(function(value) { | ||
console.log(value); //-> "value" | ||
@@ -89,2 +76,3 @@ }); | ||
## Changelog | ||
* 0.5.0 removed use of Promises as they were unnecessary | ||
* 0.4.0 simpler `Promise`-based API | ||
@@ -98,2 +86,4 @@ * 0.3.0 added `options.defaultPorts`, more tests | ||
[npm-url]: https://npmjs.org/package/urlcache | ||
[bower-image]: https://img.shields.io/bower/v/urlcache.svg | ||
[bower-url]: https://github.com/stevenvachon/urlcache | ||
[travis-image]: https://img.shields.io/travis/stevenvachon/urlcache.svg | ||
@@ -100,0 +90,0 @@ [travis-url]: https://travis-ci.org/stevenvachon/urlcache |
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
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
7356
99
88