Socket
Socket
Sign inDemoInstall

popular-cache

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

popular-cache - npm Package Compare versions

Comparing version 1.1.4 to 1.1.5

13

lib/proxy.js

@@ -25,2 +25,6 @@ function Proxy(cache, proxyFunc) {

}
Proxy.prototype.accept = function(acceptFunc){
this._acceptFunc = acceptFunc;
return this;
}
function request(self, key, callback, context){

@@ -40,6 +44,13 @@ if(!self._waitingList.hasOwnProperty(key)){

value = (arguments.length == 1) ? arguments[0]: arguments;
if(value !== null) self.set(key, value);
if(doAccept(self, key, value, context)) self.set(key, value);
done(value);
}, context);
}
function doAccept(self, key, value, context){
if(typeof self._acceptFunc === 'function'){
return self._acceptFunc(key, value, context);
}else{
return value != null;
}
}
function notify(self, key, value){

@@ -46,0 +57,0 @@ if(self._waitingList.hasOwnProperty(key)){

2

package.json
{
"name": "popular-cache",
"version": "1.1.4",
"version": "1.1.5",
"description": "An in-memory LRU cache with built-in statistics and proxy mode.",

@@ -5,0 +5,0 @@ "keywords": "lru, ttl, cache, memory, storage, statistics, analysis, proxy",

@@ -43,17 +43,21 @@ # Popular Cache

// some time consuming process like HTTP request
var httpRequest = function(key, callback){
var httpRequest = function(key, callback, context){
setTimeout(function(){
if(key=='popular-cache') callback('I am popular-cache');
else callback('others');
callback(key + ' proxy ' + context);
}, 1000);
}
// use the proxied cache returned by proxy()
var cache = pcache(50).proxy(httpRequest);
// define the proxied cache
var cache = pcache(50).proxy(httpRequest)
.accept(function(key, value, context){
return key.length > 5; // don't cache key that is too short
});
cache.get('popular-cache', function(value){
console.log(value); // I am popular-cache
});
cache.get('another key', function(value){
console.log(value); // others
});
cache.get('hello', function(value){
console.log(value); // hello proxy world
console.log(cache.size()); // 0
}, 'world');
cache.get('longer key', function(value){
console.log(value); // longer key proxy anything
console.log(cache.size()); // 1
}, 'anything');

@@ -70,3 +74,3 @@ Note that the value is not returned directly in proxy mode. Instead, it's returned via callback.

- **get(key)** (Normal Mode)
- **get(key)** (normal Mode)

@@ -99,3 +103,3 @@ - Gets a value for a given key. Returns null if not found.

- Iterate over recent used entries in reverse chronological order.
- Iterates over recent used entries in reverse chronological order.
- `key`: the key of the entry.

@@ -108,3 +112,3 @@ - `value`: the value of the entry.

- Iterate over most popular entries in descending order of hits.
- Iterates over most popular entries in descending order of hits.
- `key`: the key of the entry.

@@ -117,18 +121,13 @@ - `value`: the value of the entry.

- Build and return a proxied cache.
- The proxy function `function(key, callback(value), [context])` will be called automatically on cache misses.
- Sets a proxy function and returns the proxied cache.
- The proxy function `function(key, callback(value), [context])` will be called when cache misses occur to retrieve the latest value. `key` and `context` is passed from `get(key, function(value), [context])` directly. See the example below.
`key` and `context` is passed from `get(key, function(value), [context])` directly. `context` is optional and could be anything that is useful for the proxy function.
- **accept(function(key, value, context))** (proxy mode)
var cache = pcache().proxy(function(key, callback, context){
callback(key + ' proxy ' + context);
});
cache.get('hello', function(value){
console.log(value); // hello proxy world
}, 'world');
- Sets an acceptance function in proxy mode.
- The acceptance function `function(key, value, context)` will be called after the latest value is retrieved to determine whether or not the value should be cached.
- **get(key, function(value), [context])** (only in proxy mode)
- **get(key, function(value), [context])** (proxy mode)
- Same as `get(key)` except how the value is returned. `function(value)` is required in proxy mode to receive the value.
- `context` is only useful on cache misses and will be passed to the proxy function directly.
- Gets a value for a given key in proxy mode. Note that `function(value)` is required here to receive the value.
- `context` is optional and could be anything that is helpful for the proxy function. It will only be passed to the proxy function when cache misses occur.

@@ -147,2 +147,26 @@ var assert = require('assert'),

})
it('should support accept function', function(done){
var cache = pcache().proxy(function(key, callback, context){
callback(key);
}).accept(function(key, value, context){
return context != 'dont cache me';
});
// should set cache
cache.get('hello', function(value){
assert.equal('hello', value);
assert.equal(1, cache.size());
}, 'world');
// should not set cache
cache.get('popular', function(value){
assert.equal('popular', value);
assert.equal(1, cache.size());
}, 'dont cache me');
// should set cache
cache.get('cache', function(value){
assert.equal('cache', value);
assert.equal(2, cache.size());
done();
}, '');
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc