https://github.com/nextorigin/redis-sorted-cache
A helper class to expire keys from a sorted set in Redis, useful for timeseries caches
https://github.com/nextorigin/redis-sorted-cache
cache nodejs redis sorted-sets timeseries
Last synced: about 2 months ago
JSON representation
A helper class to expire keys from a sorted set in Redis, useful for timeseries caches
- Host: GitHub
- URL: https://github.com/nextorigin/redis-sorted-cache
- Owner: nextorigin
- Created: 2016-10-01T17:36:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-23T12:11:06.000Z (almost 5 years ago)
- Last Synced: 2025-03-23T14:01:47.065Z (about 2 months ago)
- Topics: cache, nodejs, redis, sorted-sets, timeseries
- Language: CoffeeScript
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# redis-sorted-cache
[![Build Status][ci-nextorigin]][travis-ci]
[![Coverage Status][coverage-nextorigin]][coveralls]
[![Dependency Status][dependency]][david]
[![devDependency Status][dev-dependency]][david-dev]
[![Downloads][downloads]][npm-package]
[![Greenkeeper badge][greenkeeper-enabled]][greenkeeper]A helper class to expire keys from a sorted set in Redis, useful for timeseries caches
[![NPM][npm-stats]][npm-package]
## Installation
```sh
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](https://github.com/nextorigin/godot2/blob/be70857187d83ec66188609db2d12e8a9d5209ea/src/godot/reactor/redis-cache.coffee#L31)
```coffee
SortedCache = require "redis-sorted-cache"
@cache = new SortedCache {redis: @client, name: @id, @ttl}save: (redis, data, callback) ->
ideally = errify callbackdata = flatten data
saved = []
for key, val of data when key isnt "ttl"
val = JSON.stringify val if key in ["tags"]
saved.push key, valkey = "godot:#{@id}:#{data.host}:#{data.service}:#{data.time}"
expire = @ttl or data.ttl
ttl = if @changeTtl then @ttl else data.ttlmulti = redis.multi()
multi.hmset key, "ttl", ttl, saved...
.EXPIRE key, expire
await multi.exec ideally defer()
@cache.addToSet key, data.time, callbackload: =>
ideally = errify @errorawait @cache.keys ideally defer keys
multi = @client.multi()
multi.hgetall key for key in keys
await multi.exec ideally defer datasfor 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 datareturn
```
## License
MIT
[ci-nextorigin]: https://img.shields.io/travis/nextorigin/redis-sorted-cache/master.svg?style=flat-square
[travis-ci]: https://travis-ci.org/nextorigin/redis-sorted-cache
[coverage-nextorigin]: https://img.shields.io/coveralls/nextorigin/redis-sorted-cache/master.svg?style=flat-square
[coveralls]: https://coveralls.io/r/nextorigin/redis-sorted-cache
[dependency]: https://img.shields.io/david/nextorigin/redis-sorted-cache.svg?style=flat-square
[david]: https://david-dm.org/nextorigin/redis-sorted-cache
[dev-dependency]: https://img.shields.io/david/dev/nextorigin/redis-sorted-cache.svg?style=flat-square
[david-dev]: https://david-dm.org/nextorigin/redis-sorted-cache?type=dev
[downloads]: https://img.shields.io/npm/dm/redis-sorted-cache.svg?style=flat-square
[npm-package]: https://www.npmjs.org/package/redis-sorted-cache
[npm-stats]: https://nodei.co/npm/redis-sorted-cache.png?downloads=true&downloadRank=true&stars=true
[greenkeeper-enabled]: https://badges.greenkeeper.io/nextorigin/spine-awaitajax.svg
[greenkeeper]: https://greenkeeper.io/