Harbor Documentation

Harbor::Cache::Redis

Constants

  • TRACKER_KEY_NAME

Public Class Methods

new(connection, name = nil)

      # File lib/harbor/cache/redis.rb, line 13
13:   def initialize(connection, name = nil)
14:     if connection.is_a?(Redis) || connection.is_a?(Redis::Distributed)
15:       @redis = connection
16:     else
17:       @redis = Redis::Directory.new(connection).get("cache", name)
18:     end
19:   end

Public Instance Methods

[](key)

Alias for get

bump(key)

      # File lib/harbor/cache/redis.rb, line 69
69:   def bump(key)
70:     if item = get(key)
71:       delete(key)
72:       item.bump
73:       put(key, item.ttl, item.maximum_age, item.content, item.cached_at)
74:     end
75:   end

delete(key)

      # File lib/harbor/cache/redis.rb, line 48
48:   def delete(key)
49:     @redis.del(key)
50:     @redis.srem(TRACKER_KEY_NAME, key)
51:   end

delete_matching(key_regex)

      # File lib/harbor/cache/redis.rb, line 53
53:   def delete_matching(key_regex)
54:     if (matches = keys_matching(key_regex)).empty?
55:       nil
56:     else
57:       matches.each do |match|
58:         @redis.srem(TRACKER_KEY_NAME, match)
59:       end
60:       @redis.srem(TRACKER_KEY_NAME, *matches)
61:       @redis.del(*matches)
62:     end
63:   end

get(key)

      # File lib/harbor/cache/redis.rb, line 21
21:   def get(key)
22:     if (value = @redis.get(key))
23:       item = load(key, value)
24: 
25:       if item.expired?
26:         @redis.srem(TRACKER_KEY_NAME, key)
27:         nil
28:       else
29:         @redis.expire(key, item.ttl)
30:         item
31:       end
32:     else
33:       nil
34:     end
35:   end
Also aliased as: []

keys_matching(key_regex)

      # File lib/harbor/cache/redis.rb, line 65
65:   def keys_matching(key_regex)
66:     @redis.smembers(TRACKER_KEY_NAME).select { |key| key =~ key_regex }
67:   end

load(key, data)

      # File lib/harbor/cache/redis.rb, line 77
77:   def load(key, data)
78:     value = YAML::load(data)
79:     Harbor::Cache::Item.new(key, value["ttl"], value["maximum_age"], value["content"], value["cached_at"], value["expires_at"])
80:   end

put(key, ttl, maximum_age, content, cached_at)

      # File lib/harbor/cache/redis.rb, line 39
39:   def put(key, ttl, maximum_age, content, cached_at)
40:     item = Harbor::Cache::Item.new(key, ttl, maximum_age, content, cached_at)
41:     data = { "ttl" => item.ttl, "maximum_age" => item.maximum_age, "content" => item.content, "cached_at" => item.cached_at, "expires_at" => item.expires_at }
42:     @redis.set(key, YAML::dump(data))
43:     @redis.expire(key, ttl)
44:     @redis.sadd(TRACKER_KEY_NAME, key)
45:     item
46:   end