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

encache

v1.1.1

Published

overly lightweight inmemory cache for your nodejs server

Downloads

3

Readme

image

npm version NPM License NPM Downloads GitHub commit activity (branch)

A lightweight , Inmemory Async Cache for your Nodejs Server.

  1. Quick replacement for inmemory caches like memcache or redis
  2. No need to setup a cache server
  3. 2 lines of code required to setup the cache

Documentation

1. Downloading and importing the Encache Library

 npm install encache

To check the version of encache installed , use

 npx encache --version

Now to import the cache object , use

//commonjs
const Cache = require('encache')

or

//esmodule import
import Cache from 'encache'

2. Creating the cache instance

by default the cache object reserves 5000 bytes as max memory limit .
User can provide custom memory limit to their cache according to performance needs .
The cache can be initiated by providing a set of options which are not compulsory.

const options = {
  size : 5000,
  policy : "LRU",
  compression "none"
}
const store = new Cache(options)

or

//initiating cache with default values
const store = new Cache({})

3. Set the cache policy

currently five policies are available :

  1. FIFO -> Standard Queue based first in , first out policy . Used as the default policy.
  2. Lazy-TTL -> Lazily checks for the volatile keys based on their time to live.
  3. LRU -> Least recently used keys are evicted first.
  4. Random Eviction -> Randomly Evicts the keys.
  5. No Eviction -> Does not evict any keys if the cache memory limit is being exceeded.
  6. LFU -> Evicts the least frequently used keys.
store.setPolicy('FIFO')

When you set the policy to ttl , you can set the time to live for the

cache elements in milliseconds

 //set the volatility to 1000 ms  
 store.setTTL(1000)

4. Set the cache Compression mode

The cache provides an option to compress the data to improve memory efficiency . It currently has two options

  1. LZ4 -> works well for partially random or non random data .
  2. Default ( No compression)
 store.setCompression('LZ4')

5. Usage of cache methods to manage your data store

  1. The get method is an async method used to fetch the data stored on a key. It returns data.
const fetch = async(key) =>{
 return await store.get(key)
}
  1. The put method is also an async method used to store the {key , value} pair in the cache. It returns nothing.
const insert = async(key ,data) =>{
 await store.put(key , data)
 return;
}
  1. The reset method allows the user to reset the cache .It flushes all the keys and resets the metrics and policy specific data.
store.reset()
  1. The keys method returns a list of all the available keys in the cache store
let keyList = store.keys()
console.log(keyList)

6. Metrics

The following methods are

  1. Hit Ratio: calculated as (number of hits / number of references). returns a floating point value
return store.hitRatio()
  1. Miss Ratio: calculated as 1 - hitRatio returns a floating point value
return store.missRatio()
  1. Memory Consumption: Returns the approximate current size of the cache in bytes returns an Integer value
return store.memoryConsumption()
  1. Fill Rate: The ratio of cache filled . calculated as (current memory/ max memory limit) returns a floating point value
return store.fillRate()
  1. Eviction Rate: The rate at which keys are evicted returns a floating point value
return store.evictionRate()
  1. show: It logs all the metrics in console by default .
store.show()

7. Logger

Encache provides an in-house logger to trace the cache , its performance and key movements in a file logs/app.log . For now the logger has been kept closely to cache's scope only and ,however in later iterations we will make it user centric and more suitable for user's custom logging needs. Currently , you can:

  1. Set the file logging level (FLL) for your cache. Options
  • warn
  • info
  • error
  • debug
  • off
  • all
store.logger.configureFLL(option)

8. Tests

Encache uses JEST testing framework to develop its core unit tests. The code coverage as of now is very low , but is expected to be gradually improved To run the tests , use the following command

  1. without coverage report
npm run test
  1. with coverage report
 npm run test-coverage

##THANK YOU FOR READING PATIENTLY