A helper class to expire keys from a sorted set in Redis, useful for timeseries caches
npm install --save redis-sorted-cache
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
MIT