class SgtnCldr::Core::Cache

Constants

Entry

Public Class Methods

clear() click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 61
def self.clear
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Clear cache!"
    $data = Hash.new
end
delete(key) click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 53
def self.delete(key)
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Delete cache for key: " + key
    $data.delete key
end
get(key) click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 26
def self.get(key)
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Get cache for key: " + key
    invalidate
    $data[key][:value] if has(key)
end
has(key) click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 35
def self.has(key)
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Has cache for key: " + key
    $data.has_key? key
end
initialize(disabled=false, opts={}) click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 7
def self.initialize(disabled=false, opts={})
    $opts = opts
    SgtnCldr.logger.debug "Initialize cache......"
    if disabled == false
        $data = Hash.new
        SgtnCldr.logger.debug "Cache is enabled!"
    else
        SgtnCldr.logger.debug "Cache is disabled!"
    end
end
invalidate() click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 69
def self.invalidate
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Invalidating expired cache......"
    now = Time.now
    $data.each {
        |k, v|
            SgtnCldr.logger.debug "Checking cache: key=#{k}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
        }
    $data.delete_if {|k, v| v[:expiry] < now}
end
keys() click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 18
def self.keys
    if $data == nil
        return nil
    end
    SgtnCldr.logger.debug "Get cache keys"
    $data.keys
end
put(key, value, ttl=nil) click to toggle source
# File lib/sgtn-cldr/core/cache.rb, line 43
def self.put(key, value, ttl=nil)
    if $data == nil
        return nil
    end
    ttl ||= @opts[:ttl]
    # hours from new
    SgtnCldr.logger.debug "Put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
    $data[key] = Entry.new(Time.now + ttl*60, value)
end