JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q31344F
  • License ISC

An in-memory cache with a little more options for control and flexibility.

Package Exports

  • cache-flex

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cache-flex) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Cache flex

An in-memory cache with extra options to fix value types and set constants in cache, for more consistency.

Methods

.set(key, value, options)

  • Returns a promise that resolves with the set value.
  • key should be a string.
  • value can be anything except for undefined, null or NaN.
  • options (object)
    • makeConstant (boolean): if set to true, does not allow the value to be updated.
    • fixType (boolean): if set to true, does not allow the type of the value to be changed.
    • ttl (number) (time in milliseconds): if specified, removes the value after the given interval.
    • expiryCallback(value) (function): if provided with valid ttl, this function gets called right after a value is expired with the value.
  • .setSync(...) is the synchronous version with the same parameters, returns the value.

.setFixType(key, value)

  • fixes the type for the value against the given key, and can be only updated with a value with same type as inital type of value.
  • Returns a promise that resolves with the set value.
  • .setFixTypeSync(...) is the synchronous version with the same parameters, returns the value.

.setConstant(key, value)

  • sets the value as a constant i.e. the value cannot be changed later on.
  • Returns a promise that resolves with the set value.
  • .setConstantSync(...) is the synchronous version with the same parameters, returns the value.

.get(key)

  • Returns a promise that resolves with the value stored against the key.
  • .getSync(...) is the synchronous version with the same parameters, returns the value.

.update(key, value)

  • Updates existing value against key with value.
  • Returns a promise that resolves with the updated value.

.setOrUpdate(key, value)

  • Updates or sets value against key with value.
  • Returns a promise that resolves with the updated or set value.

Usage

const cache = require('cache-flex');

cache.setSync('someKey', 'someValue')
let value = cache.getSync('someKey');

cache.set('someKey', {a: 'some value'})
  .then(value => {
    // 'value' is {a: 'some value'}
    let result = cache.get('someKey')
      .then(result => {
        // 'value' is {a: 'some value'}
      })
      .catch(err => {
        // handle error with 'get'
      });
  })
  .catch(err => {
    // handle error with 'set'
  });