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

redis-cachify

v0.4.0

Published

Simple Redis Caching Library

Downloads

10

Readme

Redis Cachify

Simple Redis & Memory Caching Library

Install

npm install redis-cachify --save

Usage

This library requires either existing redis connetions, or redis host and port settings.

e.g.

Cachify = require("redis-cachify")
cachify = Cachify
  redisPort: 6379
  redisHost: "localhost"

# OR

cachify = Cachify
  redisStore: redisClient (in normal mode)
  redisPubsub: redisClient (in pubsub mode)

# Other options (with defaults are):

cachify = Cachify
  redisPort: 6379
  redisHost: "localhost"
  defaultExpiry: 60 # (time in seconds for functions to be cached)
  cacheInProcess: false # (turns on in-process cachcing, see details below)
  redisChannel: "cache" # (the Redis pubsub channel to use)

Once you have a cachify instance you can use it as follows:

cachifiedFunction = cachify(id, myFunction)

This library currently expects all functions that it as applied to, to have the following signature:

myFunction = (data, callback) ->

Callbacks are standard Node-style callbacks that are expected to receive an optional error and then a result.

id can be a string, e.g. - "usd2gbp' or a function, e.g. (data) -> "userToken:" + data.userId

If the id argument is a function, then it will be passed the data argument passed in each time the function is called.

A cachified version of a function can be used in excactly the same way as the function that was passed into it. The difference being that the result will be cached in Redis and returned much sooner. This can be very useful in the following scenario:

Promises

This library can now be used with functions that return promises as follows:

cachifiedFunction = cachify.promise(id, myFunction)

where:

myFuncion = (data) -> new Promise ...

Getting a third party auth toekn that lasts for n minutes

Application logic is vastly simplified as all you need to do is "cachify" the function that goes and gets the token. Any method running in any process (on the same or different machines) can now reliably call that method. The function will return either a fresh token, or a cached token that will automatically expire after n minutes. If 3 methods call the function in the same tick of the event loop, the original function will only be called once - yet the callbacks from all 3 functions will correctly be called

If you make a 10 calls to cachifiedFunction (with the same data), then myFunction will only be called once.

Custom Expiry per Function

Simply pass in the expity time in seconds as the second argument:

cachifiedFunction = cachify(id, 120, myFunction)

In Process Cache

If inProcessCache is set to true then the result of myFunction will be saved both in Redis and in a local memory cache. This can improve performance by preventing unncecesary IO to Redis, but could result in slight race conditions - so it is turned off by default. The race conditions would only occur at the time of a results expiry from the cache. The in process cache could be slighly slower at expiring the data than Redis. This is highly unlikely to cause a problem - but you should be aware of it.

Troubleshooting

This library has a basic test suite and has been used extensively in production. If you are having problems, try running your program with the environment variable DEBUG=cachify This will print a whole load of debugging information to the console.

Todo

  • Enforce Redis 2.6 and use new set method signature with expiry
  • More tests for in process caching