class SgtnClient::Core::Cache

Constants

Entry

Public Class Methods

clear() click to toggle source
# File lib/sgtn-client/core/cache.rb, line 68
def self.clear
    @mutex.synchronize do
        if @@data == nil
            return nil
        end
        SgtnClient.logger.debug "[Cache][clear]clear cache!"
        @@data = Hash.new
    end
end
delete(key) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 58
def self.delete(key)
    @mutex.synchronize do
        if @@data == nil
            return nil
        end
        SgtnClient.logger.debug "[Cache][delete]delete cache for key: " + key
        @@data.delete key
    end
end
get(key) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 30
def self.get(key)
    if @@data == nil
        return nil, nil
    end
    SgtnClient.logger.debug "[Cache][get]get cache for key: " + key
    invalidate(key)
end
has(key) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 38
def self.has(key)
    if @@data == nil
        return nil
    end
    SgtnClient.logger.debug "[Cache][has]check if the cache has key: #{(@@data.has_key? key)}"
    @@data.has_key? key
end
initialize(disabled=false, opts={}) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 10
def self.initialize(disabled=false, opts={})
    @@opts = opts
    @mutex = Mutex.new
    if disabled == false
        @@data = Hash.new
        SgtnClient.logger.debug "[Cache][initialize]cache is enabled!"
    else
        @@data = nil
        SgtnClient.logger.debug "[Cache][initialize]cache is disabled!"
    end
end
invalidate(key) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 78
def self.invalidate(key)
    @mutex.synchronize do
        if @@data == nil
            return nil, nil
        end
        SgtnClient.logger.debug "[Cache][invalidate]invalidate expired cache......"
        now = Time.now
        if has(key)
            v = @@data[key]
            expired = false
            SgtnClient.logger.debug "[Cache][invalidate]check cache: key=#{key}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
            if v[:expiry] < now
                SgtnClient.logger.debug "[Cache][invalidate]before deleting the cache: data=#{@@data}"
                @@data.delete(key)
                SgtnClient.logger.debug "[Cache][invalidate]after deleting the cache: data=#{@@data}"
                expired = true
            end
            return expired, v[:value]
        else
            return nil, nil
        end
    end
end
keys() click to toggle source
# File lib/sgtn-client/core/cache.rb, line 22
def self.keys
    if @@data == nil
        return nil
    end
    SgtnClient.logger.debug "[Cache][keys]get cache keys"
    @@data.keys
end
put(key, value, ttl=nil) click to toggle source
# File lib/sgtn-client/core/cache.rb, line 46
def self.put(key, value, ttl=nil)
    @mutex.synchronize do
        if @@data == nil || value == nil
            return nil
        end
        ttl ||= @@opts[:ttl]
        # hours from new
        SgtnClient.logger.debug "[Cache][put]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
end