npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@plgworks/cache

v1.0.3

Published

Cache is the central cache implementation for several modules.

Downloads

3

Readme

Cache

Latest version

Cache NPM implements wrapper over multiple caching engines - Memcached, Redis and In-memory (use with single threaded process in development mode only). The decision of which caching engine to use is governed while creating the Cache NPM object.

Why Cache?

Core packages of different caching systems do not have a common interface, i.e. they have the same functionality implemented with different method signatures. Thus changing from one cache system to another becomes difficult as all the usages need to be revisited. This NPM package solves the problem by providing common wrapper methods for memcached, redis and in-memory caching systems.

Prerequisites

Required caching engine for the use case must be installed and up. Refer memcached and redis installation guide.

Installion

npm install @plgworks/cache --save

Initialize

const Cache = require('@plgworks/cache');

const configStrategy = {}; // Refer the next section for detailed documentation on configStrategy
const cache = Cache.getInstance(configStrategy);

const cacheImplementer = cache.cacheInstance;

Note: To print detailed logs, add CACHE_DEBUG_ENABLED = '1' in your env variables.

Config Strategy

configStrategy is a mandatory parameter which specifies the configuration strategy to be used for a particular cache engine.

An example of the configStrategy is:

const configStrategy = {
  cache: {
    engine: "none/redis/memcached",
    host: "",
    port: "",
    password: "",
    enableTsl: "",
    defaultTtl: 10000,
    consistentBehavior: "",
    servers:[],
    namespace: ""
  }
};
  • engine: redis, memcached are different types of caching engine. For in-memory cache engine parameter will be none.
  • host: Host of the redis caching engine.
  • port: Port on which redis caching engine is running.
  • password: Redis caching engine password.
  • enableTsl: This field is used to enable tsl.
  • defaultTtl: Default cache expiry time in sec.
  • consistentBehavior: This field is required to create cache instance key.
  • servers: servers is an array of memcached servers.
  • namespace: It is in-memory cache namespace.

Redis Example

Following is an example of redis engine config strategy to be used in initializing Cache.

const configStrategy = {
  cache: {
    engine: "redis",
    host: "localhost",
    port: "6830",
    password: "dsdsdsd",
    enableTsl: "0",
    defaultTtl: 36000,
    consistentBehavior: "1"
  }
}

Memcache Example

Following is an example of memcache engine config strategy to be used in initializing Cache.

const configStrategy = {
  cache: {
    engine: "memcached",
    servers: ["127.0.0.1:11211"],
    defaultTtl: 36000,
    consistentBehavior: "1"
  }
}

In-memory Example

Following is an example of in-memory engine config strategy to be used in initializing Cache.

const configStrategy = {
  cache: {
    engine: "none",
    namespace: "A",
    defaultTtl: "36000",
    consistentBehavior: "1"
  }
}

cacheImplementer methods

Irrespective of the caching engine, the methods exposed in cacheImplementer have the consistent signature.

Store and retrieve data in cache using set and get:

const resolvePromise = function(cacheResponse){
                           if (cacheResponse.isSuccess()) {
                             console.log(cacheResponse.data.response);
                           } else {
                             console.log(cacheResponse);
                           }
                         };

cacheImplementer.set('testKey', 'testValue', 5000).then(resolvePromise);

cacheImplementer.get('testKey').then(resolvePromise);

Manage objects in cache using setObject and getObject:

cacheImplementer.setObject('testObjKey', {dataK1: 'a', dataK2: 'b'}).then(resolvePromise);
cacheImplementer.getObject('testObjKey').then(resolvePromise);

Retrieve multiple cache data using multiGet:

NOTE: Redis returns null from multiGet for objects, even if a value is set in the cache. The other caching implementers match this behaviour.

cacheImplementer.set('testKeyOne', 'One').then(console.log);
cacheImplementer.set('testKeyTwo', 'Two').then(console.log);
cacheImplementer.multiGet(['testKeyOne', 'testKeyTwo']).then(resolvePromise);

Delete cache using del:

cacheImplementer.set('testKey', 'testValue').then(console.log);
cacheImplementer.del('testKey').then(resolvePromise);

Manage counters in cache using increment and decrement:

cacheImplementer.set('testCounterKey', 1).then(console.log);
cacheImplementer.increment('testCounterKey', 10).then(resolvePromise);
cacheImplementer.decrement('testCounterKey', 5).then(resolvePromise);

Change the cache expiry time using touch:

cacheImplementer.set('testKey', "testData").then(console.log);
cacheImplementer.touch('testKey', 10).then(resolvePromise);

Running test cases

Set environment variables of particular cache engine for which you want to run the tests.

  • Redis
source test/env/redis.sh
  • Memcached
source test/env/memcached.sh
  • In-memory
source test/env/inMemory.sh

Cache engines must be running on the specified ports.

  • Redis (6380,6381)
redis-server --port 6380
  • Memcached (11212,11213,11214,11215)
memcached -p 11212 -d

Run tests

./node_modules/.bin/mocha --recursive "./test/*.js"