Class: LocalCache

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/local_cache.rb

Direct Known Subclasses

ActiveSupport::Cache::LocalCache

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LocalCache

Initialize a new LocalCache.

Options:

:size => 1000 - max number of objects to store
:ttl => 60 - default ttl (can be overridden on any PUT/WRITE)


11
12
13
14
15
16
17
18
19
# File 'lib/local_cache.rb', line 11

def initialize(options={})
    puts 'Creating new LocalCache'
    @size = options[:size] || 1000
    @default_expires_in = options[:ttl] || 60
    # we initialize an empty hash
    @cache = {}
    # and a list for pushing objects out
    @cache_list = []
end

Instance Method Details

#delete(name, options = {}) ⇒ Object



95
96
97
98
# File 'lib/local_cache.rb', line 95

def delete(name, options={})
#        super
    @cache.delete(name)
end

#delete_matched(matcher, options = {}) ⇒ Object



100
101
102
103
# File 'lib/local_cache.rb', line 100

def delete_matched(matcher, options={})
#        super
    raise "delete_matched not supported by LocalCache"
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/local_cache.rb', line 57

def exist?(key)
    return !get(key).nil?
end

#get(key, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/local_cache.rb', line 21

def get(key, options={})
    # if the API URL exists as a key in cache, we just return it
    # we also make sure the data is fresh
    #puts 'looking in cache for: ' + key.to_s
    if @cache.has_key? key
        expires = @cache[key][0]
        #puts 'checking expired=' + key + ' at ' + expires.to_s + ' and now=' + Time.now.to_s
        if expires - Time.now > 0
#            puts 'returning from cache ' + @cache[key][1].inspect
            # todo: implement LRU ordering
            return @cache[key][1]
        else
            #puts 'expired'
            delete(key)
        end
    else
#          puts 'cache does not contain ' + key
    end
    return nil
end

#get_i(key) ⇒ Object



51
52
53
54
55
# File 'lib/local_cache.rb', line 51

def get_i(key)
    val = get(key)
    return nil if val.nil?
    return val.to_i
end

#get_multi(keys, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/local_cache.rb', line 42

def get_multi(keys, options={})
    ret = {}
    keys.each do |k|
        val = get(k)
        ret[k] = val unless val.nil?
    end
    ret
end

#increment(key, val = 1) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/local_cache.rb', line 61

def increment(key, val=1)
    ret = get(key)
    if ret.is_a?(Fixnum)
        ret += val
    else
        ret = val
        put(key, ret)
    end
end

#put(key, val, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/local_cache.rb', line 71

def put(key, val, options={})
    seconds_to_store = options[:expires_in] || options[:ttl] || @default_expires_in
    @cache[key] = [Time.now+seconds_to_store, val]
    @cache_list << key
    while @cache.size > @size && @cache_list.size > 0
        to_remove = @cache_list.pop
        @cache.delete(to_remove) unless to_remove.nil?
    end
end

#read(name, options = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/local_cache.rb', line 81

def read(name, options={})
#        puts 'read from localcache'
#        super
    ret = get(name, options)
#        puts 'ret.frozen=' + ret.frozen?.to_s
    return ret
end

#write(name, value, options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/local_cache.rb', line 89

def write(name, value, options={})
#        super
    put(name, value, options)
#        puts 'write.frozen=' + value.frozen?.to_s
end