Skip to content

Commit

Permalink
v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Oct 21, 2015
1 parent 453f0d4 commit 91f5e68
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 107 deletions.
22 changes: 5 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
```shell
npm install urlcache --save-dev
```
**Note:** Node.js v0.10 will need a `Promise` polyfill.


## Constructor
Expand All @@ -26,25 +25,13 @@ var cache = new UrlCache(options);
Removes `url` from cache. If `url` is not defined, *all* cached key value pairs will be removed.

### .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"
});

cache.get("unstored").catch(function(error) {
// not in cache (or value is a rejected Promise)
});
```
Returns the stored value of `url`. If no such value exists, `undefined` will be returned.

### .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").then(function(value) {
console.log(value); //-> {"key":"value"}
});
cache.get("url"); //-> {"key":"value"}

cache.set("url", new Promise(function(resolve, reject) {
// set value after some delayed event
Expand All @@ -53,7 +40,7 @@ cache.set("url", new Promise(function(resolve, reject) {
}, 500);
});

cache.get("url").then(function(value) {
Promise.resolve(cache.get("url")).then(function(value) {
console.log(value); //-> "value"
});
```
Expand Down Expand Up @@ -83,6 +70,7 @@ When `true`, will remove `#hashes` from URLs. They are most likely not useful to
## Changelog
* 0.5.0 removed use of Promises as they were unnecessary
* 0.4.0 simpler `Promise`-based API
* 0.3.0 added `options.defaultPorts`, more tests
* 0.2.0 simplified API
Expand Down
2 changes: 1 addition & 1 deletion index-browser.js

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ UrlCache.prototype.clear = function(url)
url = parseUrl(url, this.options);
url = stringifyUrl(url);

if (this.cache[url] !== undefined)
if (this.values[url] !== undefined)
{
delete this.cache[url];
delete this.expiries[url];
delete this.values[url];
}
}
else
{
this.cache = {};
this.expiries = {};
this.values = {};
}
};

Expand All @@ -46,14 +48,9 @@ UrlCache.prototype.get = function(url)
{
url = formatUrl(url, this.options);

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];
};


Expand All @@ -67,11 +64,8 @@ UrlCache.prototype.set = function(url, value, expiryTime)

if (expiryTime == null) expiryTime = this.options.expiryTime;

this.cache[url] =
{
expiryTime: Date.now() + expiryTime,
value: value
};
this.expiries[url] = Date.now() + expiryTime;
this.values[url] = value;
};


Expand Down Expand Up @@ -120,13 +114,14 @@ function parseUrl(url, options)



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];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "urlcache",
"description": "URL key-value cache and store.",
"version": "0.4.1",
"version": "0.5.0",
"license": "MIT",
"homepage": "https://github.com/stevenvachon/urlcache",
"author": {
Expand All @@ -22,10 +22,10 @@
},
"devDependencies": {
"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",
"node-static": "~0.7.7",
"uglify-js": "^2.5.0"
Expand Down
Loading

0 comments on commit 91f5e68

Please sign in to comment.