class MethodCache::LocalCache

Public Class Methods

new() click to toggle source
# File lib/method_cache/local_cache.rb, line 3
def initialize
  clear
end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value, expiry)
Alias for: set
cached_at(key) click to toggle source
# File lib/method_cache/local_cache.rb, line 51
def cached_at(key)
  @cached_at[key]
end
clear() click to toggle source
# File lib/method_cache/local_cache.rb, line 7
def clear
  @data       = {}
  @cached_at  = {}
  @expires_at = {}
end
count(key) click to toggle source
# File lib/method_cache/local_cache.rb, line 21
def count(key)
  get(key).to_i
end
decr(key, amount) click to toggle source
# File lib/method_cache/local_cache.rb, line 43
def decr(key, amount)
  incr(key, -amount)
end
delete(key) click to toggle source
# File lib/method_cache/local_cache.rb, line 33
def delete(key)
  @cached_at.delete(key)
  @expires_at.delete(key)
  @data.delete(key)
end
expires_at(key) click to toggle source
# File lib/method_cache/local_cache.rb, line 47
def expires_at(key)
  @expires_at[key]
end
get(key) click to toggle source
# File lib/method_cache/local_cache.rb, line 13
def get(key)
  if expires = expires_at(key)
    delete(key) if expires <= Time.now
  end
  @data[key]
end
Also aliased as: []
incr(key, amount) click to toggle source
# File lib/method_cache/local_cache.rb, line 39
def incr(key, amount)
  @data[key] = count(key) + amount
end
set(key, value, expiry) click to toggle source
# File lib/method_cache/local_cache.rb, line 25
def set(key, value, expiry)
  @cached_at[key]  = Time.now
  @expires_at[key] = expiry_to_time(expiry)
  @data[key]       = value
end
Also aliased as: []=, write
write(key, value, expiry)
Alias for: set

Private Instance Methods

expiry_to_time(expiry) click to toggle source
# File lib/method_cache/local_cache.rb, line 57
def expiry_to_time(expiry)
  expiry = Time.at(expiry) if expiry > 60*60*24*30
  if expiry.kind_of?(Time)
    expiry
  else
    expiry = expiry.to_i
    expiry == 0 ? nil : Time.now + expiry
  end
end