0.1.9 • Published 2 years ago

cache-list v0.1.9

Weekly downloads
10
License
MIT
Repository
github
Last release
2 years ago

Cache List

Install

$ npm install cache-list

How to use

import { MemoryCache } from 'cache-list';

const cache = new MemoryCache({
    defaultDuration: 600, // 10 min
});

const data = [
    {
        name: 'Tom',
        age: 35
    },
    {
        name: 'Emma',
        age: 29
    }
];

cache.set('users', data, 3600); // 60 min

if (cache.exists('users')) {
    return cache.get('users');
}

List:

Properties

Methods

  • buildKey - Builds a normalized cache key from a given key.
  • exists - Checks whether a specified key exists in the cache.
  • get - Retrieves a value from cache with a specified key.
  • multiGet - Retrieves multiple values from cache with the specified keys.
  • set - Stores a value identified by a key into cache.
  • multiSet - Stores multiple items in cache. Each item contains a value identified by a key.
  • add - Stores a value identified by a key into cache if the cache does not contain this key.
  • multiAdd - Stores multiple items in cache. Each item contains a value identified by a key.
  • delete - Deletes a value with the specified key from cache.
  • flush - Deletes all values from cache.

Property Details

defaultDuration

Default duration in seconds before a cache entry will expire. Default value is 0, meaning infinity. This value is used by set() if the duration is not explicitly given.

keyPrefix

A string prefixed to every cache key so that it is unique globally in the whole cache storage. It is recommended that you set a unique cache key prefix for each application if the same cache storage is being used by different applications.

To ensure interoperability, only alphanumeric characters should be used.

serialization

If this property is set false, data will be directly sent to and retrieved from the cache component without any serialization or deserialization.

Method Details

buildKey

Builds a normalized cache key from a given key.

If the given key is a string containing alphanumeric characters only and no more than 32 characters, then the key will be returned back prefixed with keyPrefix. Otherwise, a normalized key is generated by serializing the given key, applying MD5 hashing, and prefixing with keyPrefix.

buildKey(key)
keymixedThe key to be normalized
returnstringThe generated cache key

exists

Checks whether a specified key exists in the cache.

This can be faster than getting the value from the cache if the data is big.

exists(key)
keymixedA key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key.
returnbooleanTrue if a value exists in cache, false if the value is not in the cache or expired.

get

Retrieves a value from cache with a specified key.

get(key)
keymixedA key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key.
returnmixedThe value stored in cache, false if the value is not in the cache, expired, or the dependency associated with the cached data has changed.

multiGet

Retrieves multiple values from cache with the specified keys.

Some caches allow retrieving multiple cached values at the same time, which may improve the performance. In case a cache does not support this feature natively, this method will try to simulate it.

multiGet(key)
keystring[]List of string keys identifying the cached values
returnmixedList of cached values corresponding to the specified keys. The array is returned in terms of (key, value) pairs. If a value is not cached or expired, the corresponding array value will be false.

set

Stores a value identified by a key into cache.

If the cache already contains such a key, the existing value and expiration time will be replaced with the new ones, respectively.

set(key, value, duration)
keymixedA key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key.
valuemixedThe value to be cached
durationintegerThe number of seconds in which the cached value will expire. 0 means never expire. If not set, default defaultDuration value is used.
returnstringWhether the value is successfully stored into cache

multiSet

Stores multiple items in cache. Each item contains a value identified by a key.

If the cache already contains such a key, the existing value and expiration time will be replaced with the new ones, respectively.

multiSet(items, duration)
itemsmixedThe items to be cached, as key-value pairs.
durationintegerDefault number of seconds in which the cached values will expire. 0 means never expire.
returnstringArray of failed keys

add

Stores a value identified by a key into cache if the cache does not contain this key.

Nothing will be done if the cache already contains the key.

add(key, value, duration)
keymixedA key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key.
valuemixedThe value to be cached
durationintegerThe number of seconds in which the cached value will expire. 0 means never expire. If not set, default defaultDuration§ value is used.
returnstringWhether the value is successfully stored into cache

multiAdd

Stores multiple items in cache. Each item contains a value identified by a key.

If the cache already contains such a key, the existing value and expiration time will be preserved.

multiAdd(items, duration)
itemsmixedThe items to be cached, as key-value pairs.
durationintegerDefault number of seconds in which the cached values will expire. 0 means never expire.
returnstringArray of failed keys

delete

Deletes a value with the specified key from cache.

delete(key)
keymixedA key identifying the value to be deleted from cache. This can be a simple string or a complex data structure consisting of factors representing the key.
returnbooleanIf no error happens during deletion

flush

Deletes all values from cache.

Be careful of performing this operation if the cache is shared among multiple applications.

flush()
returnbooleanWhether the flush operation was successful.

MemoryCache

Use memory as a storage.

RedisCache

Use Redis as a storage.

Properties

  • isSharedDatabase boolean - If you use a separated database to store a cache, set keyPrefix
  • clientOptions object - Config for redis connections. You can find a full list of redis client options here.
const cache = new RedisCache();

const cache = new RedisCache({
    clientOptions: {
        db: 2
    }
});

const cache = new RedisCache(
    {
        isSharedDatabase: true,
        keyPrefix: 'active_users',
        clientOptions: {
            db: 1
        }
    }
);

Redis Client Options

PropertyDefaultDescription
host127.0.0.1IP address of the Redis server
port6379Port of the Redis server
pathnullThe UNIX socket string of the Redis server
urlnullThe URL of the Redis server. Format: [redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] (More info avaliable at IANA).
familyIPv4You can force using IPv6 if you set the family to 'IPv6'. See Node.js net or dns modules on how to use the family type.
dbnullIf set, client will run Redis select command on connect.
passwordnullIf set, client will run Redis auth command on connect. Alias auth_pass Note Node Redis < 2.5 must use auth_pass
socket_keepalivetrueIf set to true, the keep-alive functionality is enabled on the underlying socket.
socket_initial_delay0Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis.
enable_offline_queuetrueBy default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection has been established. Setting enable_offline_queue to false will disable this feature and the callback will be executed immediately with an error, or an error will be emitted if no callback is specified.
retry_unfulfilled_commandsfalseIf set to true, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. incr). This is especially useful if you use blocking commands.
disable_resubscribingfalseIf set to true, a client won't resubscribe after disconnecting.
0.1.8

2 years ago

0.1.9

2 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.1.0

7 years ago