New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cache-js

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-js

Cache container for nodejs and regular Javascript application.

latest
Source
npmnpm
Version
0.0.7
Version published
Weekly downloads
13
1200%
Maintainers
1
Weekly downloads
 
Created
Source

cachejs

Cache container for nodejs and regular Javascript application.

NPM Version NPM Downloads Build Status

Why

CacheJS is a Cache container, it is designed to be a common cache interface, storage independent.

Installation and usage

In node application context:

npm install cachejs
var cachejs = require('cachejs');
var container = cachejs.Container();

var obj = { ... };
container.set('objid', obj, { lifetime: 5000 }); // store in cache for 5 sec.
...
var cachedObj = container.get('objid'); // retrieve from cache

In regular Javascript application context:

bower install cachejs
<script src="bower_components/cachejs/cache.js"></script>
<script src="bower_components/cachejs/storage/memory.js"></script>
<script src="bower_components/cachejs/cacheitem.js"></script>
<script src="bower_components/cachejs/container.js"></script>
<script>
var cache = new cachejs.Container();

// Same example than in NodeJS context.
</script>

All cachejs objects are located in CacheJS namespace of window.

API

Container

Container is a generic cache container model, its default store engine is MemoryCacheContainer. See Store Engine section for more information.

Container(options)

Container constructor accepts options object as unique parameter to configure it. Options may be overriden once created using options instance function.

.options(optKey [, optValue])

Get or set an option for this container.

Container options
OptionDefaultDescription
lifetimefalseThe default lifetime for stored objects in ms. Set false evaluated or negative expression for infinite lifetime.
onUpdate-Callback function(objKey, oldVal, newVal) used each time an object is created or updated in container.
onExpiry-Callback function(cacheItem)used each time an object expires in container. May be used to create an auto-refresh process for cacheItem.key value.
storage'memory'Storage engine ```{String
pruningInterval60000Interval (in ms.) container checks for expired cache items

.set(objKey, obj [, options])

Stores obj in cache associated with objKey reference for further retrieving.

options parameter allow to specify some cache feature related to this stored item. This object is the same used to specify container options and overrides already defined container options. See Container options.

Returns the stored cache item.

CacheItem

PropertyDescription
storedAtStorage date (Javascript Date object)
FunctionDescription
isExpiredIndicates whether cache value has expired or not
valueGet or set cache item value.
OptionDescription
lifetimeLifetime of this cache item
onExpiryCallback used when cache item lifetime expires. Default value is container's one.
onUpdateCallback used when cache item value is updated. Default value is container's one.

Example:

var cacheItem = container.set('basket', { // Object stored
    customerEmail: 'john.doe@domain.ext',
    items: [
      { articleId: 45, quantity: 2 },
      { articleId: 12, quantity: 1 }
    ]
  }, {
    lifetime: 24 * 3600 * 1000, // 24h
    onExpiry: function(cacheItem) {
      // cacheItem.lifetime === 24 * 3600 * 1000
      // cacheItem.onExpiry === this
      
      // mailSender is a fake for this example
      var basket = cacheItem.getValue()
      mailSender.send(basket.customerEmail,
        'Hi, your basket created at ' + cacheItem.storedAt + ' has just expired.');
    }
  });

// Set option
cacheItem.options('lifetime', 5000);

// Get option
var updateCallback = cacheItem.options('onUpdate');

Nota bene: Some readonly fields are added to CacheItem instance once created: storedAt and cannot be overridden.

.get(objKey)

Retrieves a non-expired value from cache. objKey is the cache key of stored value. To retrieve item independently of its expired state, use getItem instead.

When no value retrieved for given objKey, returns null.

.has(objKey)

Returns true whether key objKey reference a valid (not expired) value in cache, otherwise false .

.getItem(objKey)

Retrieves an object cache item from cache or null.

var item = cache.getItem('basket');
/* item object:
{
  key: 'basket',
  storedAt: 1419153019947, // Ticks (e.g.: new Date().getTime())
  lifetime: 86400000, // 24h
  onUpdate: undefined,
  onExpiry: function(){ ... },
  isExpired: function() { ... }, // Returns true or false
  value: { ... } // cached object
}
*/

.prune()

Remove all expired items from cache container and aises onExpiry of all removed items.

FAQs

Package last updated on 01 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts