-
Notifications
You must be signed in to change notification settings - Fork 5
/
pantry-redis.coffee
42 lines (33 loc) · 1.18 KB
/
pantry-redis.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Log = require 'coloured-log'
redis = require 'redis'
module.exports = class RedisStorage
constructor: (port = 6379, host = 'localhost', options = {}, verbosity = 'ERROR') ->
# configure the log
@log = new Log(verbosity)
# connect to redis server
@client = redis.createClient(port, host)
@client.on 'error', (err) =>
@log.error err
@client.on 'ready', =>
@log.info "Connected to Host: #{host}, Port: #{port}"
# optionally send password
@client.auth options.auth if options.auth?
@log.notice "New redis storage created"
# retrieve a specific resource
get: (key, callback) ->
@client.get key, (err, results) ->
@log.error err if err
callback err, if err then null else JSON.parse(results)
# save a specific resource
put: (resource, callback) ->
@client.set resource.options.key, JSON.stringify(resource), (err, results) =>
if err?
@log.error err
callback(err, results) if callback?
else
#expire item from cache when spoiled (no need to wait)
@client.expire resource.options.key, resource.options.maxLife, ->
# TODO: log error here
callback(err, results) if callback?
#allow chaining, mostly for testing
return @