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

blockbase-cache

v0.1.7

Published

blockbase cache driver for Express

Downloads

2

Readme

Driver Cache for Blockbase

Compatible with Blockbase Framework

Version

0.1.6

TODO :
  • add debug configuration option

How to install ?

$ npm i --save blockbase-cache

Then add to your config/{env}.yml the following (example) instructions depending of your system


#optional
redis:  
    port: 
    host:

#required 
cache: 
    disabled: false

Don't want to read ?

-> Jump to the ./sample.js sample

Cache logic

Redis allows us to store a lot of different types of data.

In our case we use the hashset datatype that is ideal to store data according to :

  • a static default key that never changes : the moduleKey
  • a cache key/value pair that can vary, depending on parameter key/values: the cacheKey
Features
  • The driver allows you to store data simply with a key, or also with varying parameters
  • You can then clear data according to the modulekey or the cachekey
  • Every value expires according to the default Module expire time

Instanciation

The driver is a Cache class to connects to your redis server to store data. It can be instanciated using the following parameters, us :


    let moduleCacheKey = 'models.user'
    let cacheExpire = 60 // seconds - default is 0
    const cache = new Cache(moduleCacheKey, cacheExpire) 
Defaut usage

The static moduleKey allows to store sub-values according to a key that should never change.

  • Example : models.user

The cacheKey can be, for example, the name of the function that has data to cache :

  • Example key : user.list
  • Example value : []
   await app.drivers.cache.set('user.list', userList, opt)

This way, when you call your function user.list without varying parameters, the Cache driver will create a hashset with the key models.user, and with a field user.list that will store your data as the value :

A json comparison of the data stored in the cache would give this :

   {
     "models.user": {
       "user.list": [],
       "user.listByID": [],
       "user.listByName": []
     }
   }

#####Varying parameters :

These are used when you want to store/retrieve data according to parameter that can change.

For example, a user.list function that is called with a limit of 50 results, or 100 results, should not send back the same user list.

The cache driver automatically handles this, so you only have to give the parameters that can change :

   let opt = {limit: 100, offset:0}
   //retrieve your userlist...
   await app.drivers.cache.set('user.list', userList, opt)

Here the cache driver will generate a field name according to your parameters, which gives us for example :

A json comparison of the data stored in the cache would give this :

   {
     "models.user": {
       "user.list.limit:100.offset:0": [],
       "user.list.limit:100.offset:50": [],
       "user.listByID": [],
       "user.listByName": []
     }
   }

So the driver will give you back the correct data depending on your input parameters

Usage

1) Set

Store a value in cache according to 3 params :

  • Cache key : for ex your function name
  • Varying params (optional)
  • The value (required)

For now params can be a an object with one level of keys/values, or can also be an Array

    await app.drivers.cache.set('user.list', userList)
    
    //Or with varying params :
    let opt = {limit: 100, offset: 0}
    let opt = [100, 0]
    
    /*let opt = {limit: 100, offset: {from:0}}*/ //Sub objects not supported yet
    
    await app.drivers.cache.set('user.list', opt, userList)
2) Get

Retrieve a value from cache :

    await app.drivers.cache.get('user.list')
    
    //Or with varying params :
    let opt = {limit: 100, offset: 0} || null
    await app.drivers.cache.get('user.list', opt) 
3) Clear

To clear a cache key, or fully clear the module's cache :

    //To clear a cacheKey's values (even with those with varying params) 
    await app.drivers.cache.clear('user.list')
    
    //To clear all the module's keys :
    await app.drivers.cache.clear()

Be also careful redis is mandatory when you use the cache...

License

(Licence MIT) Coded by Blacksmith

Free Software, Hell Yeah!