-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathredis.coffee
45 lines (36 loc) · 1.58 KB
/
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
43
44
45
common = require './common'
redis = require 'redis'
url = require 'url'
# Transparent frontend for the real @cache
class Cache extends common.CacheServer
constructor: (@cache, @config) ->
if @config.redis_url
params = url.parse @config.redis_url
@client = redis.createClient params.port, params.hostname
@client.auth params.auth.split(':')[1] if params.auth
else
@client = redis.createClient()
@prefix = @config.cache_redis_prefix or 'imgflo_cache'
@expireSeconds = @config.cache_redis_ttl
keyExists: (key, callback) ->
redisKey = @redisPathForKey key
@client.get redisKey, (err, res) =>
return callback err if err
return callback null, res if res
@cache.keyExists key, (err, cached) =>
return callback err if err
return callback null, cached if not cached
@client.set redisKey, cached, 'EX', @expireSeconds, (err) ->
return callback err, cached
putFile: (source, key, callback) ->
# Don't cache, let next keyExists check get it
# This operation is comparitively very rare, avoids duplicate logic
@cache.putFile source, key, callback
handleKeyRequest: (key, request, response) ->
@cache.handleKeyRequest? key, request, response
redisPathForKey: (key) ->
return "#{@prefix}/url/#{key}"
# TEMP: needed as long as we return cache url in POST requests, and not job urls
urlForKey: (key) ->
return @cache.urlForKey key
exports.Cache = Cache