Neatly Positioned Magazines


    1.3.0 • Public • Published


    Generic n-way Set Associative Cache for node.js.

    Build Status npm version Coverage Status


    npm install sacjs


    var Cache = require('sacjs');
    var cache = new Cache({ assoc: 4, size: 10000, algorithm: 'lru' });
    cache.put(key, data);
    // ...
    var data = cache.get(key);
    // ...
    // ...

    key used to store data in the cache can be any scalar or object/array. You can supply serialize option that will be used to convert keys to strings, otherwise json-stable-stringify will be used. Serialized keys are hashed using Dan Bernstein's algorithm.


    Options object is passed to cache constructor.

    • assoc - cache associativity level (the number of slots per set). For any given key an item can be stored in any of the slots in the set. Higher associativity improves hit ratio but reduces cache performance.

    • size - cache size (the number of sets of slots the cache will store). The total number of items the cache can store is assoc * size

    • algorithm - eviction algorithm. Can be:

      • 'lru' (least recently used)
      • 'mru' (most recently used)
      • 'lfu' (least frequently used)
      • 'mfu' (most frequently used)
      • object with three functions: created, accessed (passed the slot) and evict (passed the set - the hash of slots). Implementations of eviction algorithms are in algorithms.js and cache.spec.js.
    • serialize - custom key serialization algorithm. Should return the same string for the same key every time it is called. By default, json-stable-stringify is used. Pass false to skip serializing (if all keys are strings or numbers).

    Cache statistics

    Cache usage statistics are available in stat property of the cache instance. The stat object has the following properties:

    • misses - the number of misses
    • hits - the number of hits
    • stored - the number of items that were stored in free slots
    • replaced - the number of item that were replaced (different value for the same key)
    • evicted - the number of items that were evicted


    npm i sacjs

    DownloadsWeekly Downloads






    Last publish


    • avatar