Skip to content

A helper class to expire keys from a sorted set in Redis, useful for timeseries caches

Notifications You must be signed in to change notification settings

nextorigin/redis-sorted-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redis-sorted-cache

Build Status Coverage Status Dependency Status devDependency Status Downloads Greenkeeper badge

A helper class to expire keys from a sorted set in Redis, useful for timeseries caches

NPM

Installation

npm install --save redis-sorted-cache

Usage

Store a JSON object (timeseries event) in Redis and later retrieve all events within the cache TTL

From https://github.com/nextorigin/godot2/.../src/godot/reactor/redis-cache.coffee#L31

    SortedCache   = require "redis-sorted-cache"
    @cache        = new SortedCache {redis: @client, name: @id, @ttl}

  save: (redis, data, callback) ->
    ideally = errify callback

    data  = flatten data
    saved = []
    for key, val of data when key isnt "ttl"
      val = JSON.stringify val if key in ["tags"]
      saved.push key, val

    key    = "godot:#{@id}:#{data.host}:#{data.service}:#{data.time}"
    expire = @ttl or data.ttl
    ttl    = if @changeTtl then @ttl else data.ttl

    multi  = redis.multi()
    multi.hmset key, "ttl", ttl, saved...
         .EXPIRE key, expire
    await multi.exec ideally defer()
    @cache.addToSet key, data.time, callback

  load: =>
    ideally = errify @error

    await @cache.keys ideally defer keys
    multi = @client.multi()
    multi.hgetall key for key in keys
    await multi.exec ideally defer datas

    for data in datas
      data = unflatten data
      continue unless data
      types     = Producer::types
      data[key] = JSON.parse data[key] for key, type of types when data[key] and typeof data[key] isnt type
      @push data

    return

License

MIT

About

A helper class to expire keys from a sorted set in Redis, useful for timeseries caches

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published