Class: FreshRedis::Key
- Inherits:
-
Object
- Object
- FreshRedis::Key
- Defined in:
- lib/fresh_redis/key.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :freshness => 60 * 60, # 1 hour :granularity => 1 * 60 # 1 minute }
Instance Attribute Summary collapse
-
#freshness ⇒ Object
readonly
Returns the value of attribute freshness.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(base_key, t, freshness, granularity) ⇒ Key
constructor
A new instance of Key.
- #redis_key ⇒ Object
- #timestamp_buckets ⇒ Object
Constructor Details
#initialize(base_key, t, freshness, granularity) ⇒ Key
Returns a new instance of Key.
24 25 26 27 28 29 |
# File 'lib/fresh_redis/key.rb', line 24 def initialize(base_key, t, freshness, granularity) @base_key = base_key @t = t @freshness = freshness @granularity = granularity end |
Instance Attribute Details
#freshness ⇒ Object (readonly)
Returns the value of attribute freshness.
22 23 24 |
# File 'lib/fresh_redis/key.rb', line 22 def freshness @freshness end |
Class Method Details
.build(*args) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fresh_redis/key.rb', line 9 def self.build(*args) raise "Don't know how to build FreshRedis::Key for #{args.inspect}" unless args[0] return args[0] if Key === args[0] # early exit if we've already got a key base_key = args[0] = DEFAULT_OPTIONS.merge(args[1] || {}) [:t] ||= Time.now.to_i self.new(base_key, [:t], [:freshness], [:granularity]) end |
Instance Method Details
#==(other) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/fresh_redis/key.rb', line 41 def ==(other) same = true same &= Key === other same &= @base_key == other.instance_variable_get(:@base_key) same &= @t == other.instance_variable_get(:@t) same &= @freshness == other.instance_variable_get(:@freshness) same &= @granularity == other.instance_variable_get(:@granularity) same end |
#redis_key ⇒ Object
31 32 33 |
# File 'lib/fresh_redis/key.rb', line 31 def redis_key [@base_key, normalize_time(@t, @granularity)].join(":") end |
#timestamp_buckets ⇒ Object
35 36 37 38 39 |
# File 'lib/fresh_redis/key.rb', line 35 def from = normalize_time(@t - @freshness, @granularity) to = normalize_time(@t, @granularity) (from..to).step(@granularity).map{|| [@base_key, ].join(":") } end |