Skip to content

Commit 4021283

Browse files
committed
[doc] Installation, Usage example, License
1 parent e232395 commit 4021283

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,62 @@ A helper class to expire keys from a sorted set in Redis, useful for timeseries
1010

1111
[![NPM][npm-stats]][npm-package]
1212

13+
## Installation
14+
```sh
15+
npm install --save redis-sorted-cache
16+
```
17+
18+
## Usage
19+
20+
##### Store a JSON object (timeseries event) in Redis and later retrieve all events within the cache TTL
21+
22+
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)
23+
24+
```coffee
25+
SortedCache = require "redis-sorted-cache"
26+
@cache = new SortedCache {redis: @client, name: @id, @ttl}
27+
28+
save: (redis, data, callback) ->
29+
ideally = errify callback
30+
31+
data = flatten data
32+
saved = []
33+
for key, val of data when key isnt "ttl"
34+
val = JSON.stringify val if key in ["tags"]
35+
saved.push key, val
36+
37+
key = "godot:#{@id}:#{data.host}:#{data.service}:#{data.time}"
38+
expire = @ttl or data.ttl
39+
ttl = if @changeTtl then @ttl else data.ttl
40+
41+
multi = redis.multi()
42+
multi.hmset key, "ttl", ttl, saved...
43+
.EXPIRE key, expire
44+
await multi.exec ideally defer()
45+
@cache.addToSet key, data.time, callback
46+
47+
load: =>
48+
ideally = errify @error
49+
50+
await @cache.keys ideally defer keys
51+
multi = @client.multi()
52+
multi.hgetall key for key in keys
53+
await multi.exec ideally defer datas
54+
55+
for data in datas
56+
data = unflatten data
57+
continue unless data
58+
types = Producer::types
59+
data[key] = JSON.parse data[key] for key, type of types when data[key] and typeof data[key] isnt type
60+
@push data
61+
62+
return
63+
64+
```
65+
66+
## License
67+
68+
MIT
1369

1470
[ci-nextorigin]: https://img.shields.io/travis/nextorigin/redis-sorted-cache/master.svg?style=flat-square
1571
[travis-ci]: https://travis-ci.org/nextorigin/redis-sorted-cache

0 commit comments

Comments
 (0)