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

advanced-cache

v2.1.0

Published

Advanced redis cache with lock function

Downloads

5

Readme

Advanced cache based on ioredis, redlock and node-cache

Main points to have this module are:

  • reduce load on data storage on cold start
  • unify repetitive actions as: get from cache if not load from db
  • reload cache before it expires

Note: caches value only if result of load function is converted to true: !!result === true

How to use RedisCache

  const advancedCache = require('advanced-cache')
  
  const RedisCache = advancedCache.RedisCache
  const CachePolicy = advancedCache.CachePolicy
  
  const ioRedisOpts = {
    port: 6379,
    host: '127.0.0.1',
    password: 'auth',
    keyPrefix: 'some:'
  }

  const redlockOpts = {
    retryCount: 0 //default
  }
  
  const opts = {
    lockIntervalMs: 1000, //time in ms key is locked to load data from store (default)
    retryIntervalMs: 50  //time failed lock waits before next try (default)
  }

  const cache = new RedisCache(ioRedisOpts, opts, redlockOpts) //opts and redlockOpts are optional and have defaults

  //string value
  const countryCachePolicy = new CachePolicy(['country-code', 5], 24 * 60 * 60)
  cache.stringFetch(countryCachePolicy, loadAsStringFnPromise, loadFnArgs).then(countryCode => {})

  //js object as JSON
  const userCachePolicy = new CachePolicy(['user', 12], 60 * 60)
  cache.serializedFetch(userCachePolicy, loadAsObjectFnPromise, loadFnArgs).then(user => user.fly())

  //js object as hash
  cache.hashFetch(userCachePolicy, loadAsObjectFnPromise, loadFnArgs).then(user => user.fly())

 //when you need direct access to redis client
  cache.redis.mget(['country-code:13', 'user:12']).then(() => {})

How to use MemoryCache

  const advancedCache = require('advanced-cache')

  const MemoryCache = advancedCache.MemoryCache //extended from NodeCache
  const CachePolicy = advancedCache.CachePolicy

  const opts = {useClones: false} //default opts
  const cache = new MemoryCache(opts)
  
  const countryCachePolicy = new CachePolicy(['country-code', 5], 24 * 60 * 60)
  cache.fetch(countryCachePolicy, loadPromise).then(countryCode => {})

How to bypass cache

Sometimes during development it handy just bypass cache and fetch data directly from load function To bypass RedisCache add environment variable ADVANCED_CACHE_BYPASS_REDIS_CACHE equal to 1 To bypass MemoryCache add environment variable ADVANCED_CACHE_BYPASS_MEMORY_CACHE equal to 1 Though it will work only if your NODE_ENV equals development