Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

memoize-cache

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoize-cache - npm Package Compare versions

Comparing version 0.1.1 to 1.0.0

cache.js

4

index.js
var ramCache = require('./ram-cache');
var cache = require('./cache');
module.exports = {
ramCache: ramCache
ramCache: ramCache,
cache: cache
};
{
"name": "memoize-cache",
"version": "0.1.1",
"version": "1.0.0",
"description": "A cache support for memoized functions",

@@ -24,2 +24,3 @@ "main": "index.js",

"devDependencies": {
"cache-manager": "^1.4.0",
"chai": "^1.10.0",

@@ -32,2 +33,3 @@ "eslint": "^1.10.3",

"dependencies": {
"bluebird": "^3.3.4",
"md5-o-matic": "^0.1.1",

@@ -34,0 +36,0 @@ "sizeof": "^1.0.0"

@@ -9,2 +9,11 @@ var sizeof = require('sizeof');

*/
function find(arr, predicate) {
var i,
len = arr.length;
for (i = 0; i < len; i++) {
if (predicate(arr[i])) {
return i;
}
}
}

@@ -15,7 +24,3 @@ function sortByLessPopular(a, b) {

function sortByOldest(a, b) {
return a.ts < b.ts;
}
function removeByKey(key) {
function byKey(key) {
return function (item) {

@@ -45,20 +50,32 @@ return item.key === key;

Cache.prototype.push = function cache_push(args, output) {
var lru;
var k = this.getCacheKey(args);
var lru, oldestIndex,
k = this.getCacheKey(args);
if (k in this._cache) return;
if(this._LRU.size() === this._maxLen) {
// remove from LRU heap
lru = this._LRU.pop();
// remove from cache
delete this._cache[lru.key];
this._oldest.remove(removeByKey(lru.key));
// remove from stale objects cache
oldestIndex = find(this._oldest, byKey(lru.key));
if (oldestIndex) {
this._oldest.splice(oldestIndex, 1);
}
}
// add to cache
this._cache[k] = output;
this._LRU.push({key: k, times: 0});
this._oldest.push({
key: k,
ts: Date.now()
});
if (this._maxLen !== Infinity) {
// add to LRU heap
this._LRU.push({key: k, times: 0});
}
if (this._maxAge !== Infinity) {
// add to stale objects cache
this._oldest.push({
key: k,
ts: Date.now()
});
}
};

@@ -68,15 +85,19 @@

// remove old entries
var oldest;
var now = Date.now();
var key, i, oldestIndex,
maxAge = this._maxAge,
now = Date.now();
while (this._oldest.size()) {
oldest = this._oldest.pop();
if (oldest.ts + this._maxAge < now) {
delete this._cache[oldest.key];
this._LRU.remove(removeByKey(oldest.key));
if (this._maxAge === Infinity) return;
var oldestIndex = find(this._oldest, function (oldest) {
return oldest.ts + maxAge >= now;
});
if (oldestIndex) {
for(i = 0; i < oldestIndex; i++){
key = this._oldest[i].key;
delete this._cache[key];
this._LRU.remove(byKey(key));
}
else {
this._oldest.push(oldest);
break;
}
this._oldest.splice(0, i);
}

@@ -88,3 +109,3 @@ };

this._LRU = new Heap(sortByLessPopular);
this._oldest = new Heap(sortByOldest);
this._oldest = [];
};

@@ -105,5 +126,8 @@

hit = this._cache[key]; // cache hit!
lru = this._LRU.remove(removeByKey(key));
lru.times++;
this._LRU.push(lru);
if (this._maxLen !== Infinity) {
lru = this._LRU.remove(byKey(key));
lru.times++;
this._LRU.push(lru);
}
}

@@ -127,5 +151,5 @@ }

Cache.prototype.len = function cache_len() {
return this._LRU.size();
return Object.keys(this._cache).length;
};
module.exports = Cache;
memoize-cache
=============
A configurable cache support for memoized functions. It is lightweight so it can run in the browser without any problem.
A configurable cache support for functions (https://www.npmjs.com/package/async-deco). It contains 2 different implementations.
A note about the API
====================
This is an in-memory cache implementation. But the interface is designed to work with an external storage support (db, etc). The only 2 required public methods are: "push" and "query".
* ram-cache: is a lightweight yet complete implementation of an in-ram cache. Suitable for using it in the browser
* cache: it uses a cache manager object to store data in memory/redis/mongodb/file system (https://www.npmjs.com/package/cache-manager)
Creating an instance
--------------------
ram-cache
=========
The constructor takes an option object with 3 optional attributes:

@@ -31,2 +31,25 @@ * key: a function used to extract the cache key (used in the push and query method for storing, retrieving the cached value). The key returned should be a string or it will be converted to JSON and then md5. Default: a function returning a fixed key.

cache
=====
The constructor takes an cache-manager object and an optional "key" function. The function will be used to extract the cache key (used in the push and query method for storing, retrieving the cached value). The key returned should be a string or it will be converted to JSON and then md5. Default: a function returning a fixed key.
Example:
```js
var
var Cache = require('memoize-cache/cache'); // or require('memoize-cache').cache;
var cacheManager = require('cache-manager'); // npm install cache-manager
// using the id property of the first argument
// this cache will store maximum 100 items
// every item will be considered stale and purged after 20 seconds.
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 20});
var cache = new Cache(memoryCache, function (config){
return config.id;
});
```
Methods
=======
Pushing a new cached value

@@ -52,2 +75,3 @@ --------------------------

-------------------
This is implemented only on ram-cache.
```js

@@ -59,2 +83,3 @@ cache.reset();

---------------------------------
This is implemented only on ram-cache.
```js

@@ -66,2 +91,3 @@ cache.len();

-----------------------------
This is implemented only on ram-cache.
```js

@@ -68,0 +94,0 @@ cache.size(true); // size is an human readable size

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