module JunglePath::Cache

Constants

DEFAULT_TIMEOUT

Public Class Methods

[](key) click to toggle source
# File lib/jungle_path/cache.rb, line 5
def self.[] key
        unwrap(@hash[key], key)
end
[]=(key, value) click to toggle source
# File lib/jungle_path/cache.rb, line 8
def self.[]= key, value
        @hash[key] = wrap(value)
end
clear(key) click to toggle source
# File lib/jungle_path/cache.rb, line 17
def self.clear key
        @hash.delete key
end
get(key) click to toggle source
# File lib/jungle_path/cache.rb, line 11
def self.get key
        unwrap(@hash[key], key)
end
set(key, value, timeout=DEFAULT_TIMEOUT) click to toggle source
# File lib/jungle_path/cache.rb, line 14
def self.set key, value, timeout=DEFAULT_TIMEOUT
        @hash[key] = wrap(value, timeout)
end

Private Class Methods

unwrap(item, key) click to toggle source
# File lib/jungle_path/cache.rb, line 24
def self.unwrap item, key
        if item
                if Time.now.utc - item[:timestamp] > item[:timeout]
                        @hash.delete key
                        return nil
                end
                item[:timestamp] = Time.now.utc
                return item[:value]
        end
        item
end
wrap(value, timeout=DEFAULT_TIMEOUT) click to toggle source
# File lib/jungle_path/cache.rb, line 21
def self.wrap value, timeout=DEFAULT_TIMEOUT
        {value: value, timestamp: Time.now.utc, timeout: timeout}
end