Class: ActiveSupport::Cache::LocalCache
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::LocalCache
- Defined in:
- lib/local_cache.rb
Instance Method Summary collapse
- #delete(name, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(key) ⇒ Boolean
- #get(key) ⇒ Object
- #get_i(key) ⇒ Object
- #get_multi(keys, raw = false) ⇒ Object
- #increment(key, val = 1) ⇒ Object
-
#initialize(size = 1000, default_expires_in = 60) ⇒ LocalCache
constructor
A new instance of LocalCache.
- #put(key, val, seconds_to_store = 999999, raw = false) ⇒ Object
- #read(name, options = nil) ⇒ Object
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(size = 1000, default_expires_in = 60) ⇒ LocalCache
Returns a new instance of LocalCache.
7 8 9 10 11 12 13 14 |
# File 'lib/local_cache.rb', line 7 def initialize(size=1000, default_expires_in=60) puts 'Creating new LocalCache' @size = size @default_expires_in = default_expires_in # we initialize an empty hash @cache = {} @cache_list = [] end |
Instance Method Details
#delete(name, options = nil) ⇒ Object
91 92 93 94 |
# File 'lib/local_cache.rb', line 91 def delete(name, = nil) super @cache.delete(name) end |
#delete_matched(matcher, options = nil) ⇒ Object
96 97 98 99 |
# File 'lib/local_cache.rb', line 96 def delete_matched(matcher, = nil) super raise "delete_matched not supported by LocalCache" end |
#exist?(key) ⇒ Boolean
52 53 54 |
# File 'lib/local_cache.rb', line 52 def exist?(key) return !get(key).nil? end |
#get(key) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/local_cache.rb', line 16 def get(key) # 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
46 47 48 49 50 |
# File 'lib/local_cache.rb', line 46 def get_i(key) val = get(key) return nil if val.nil? return val.to_i end |
#get_multi(keys, raw = false) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/local_cache.rb', line 37 def get_multi(keys, raw=false) ret = {} keys.each do |k| val = get(k) ret[k] = val unless val.nil? end ret end |
#increment(key, val = 1) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/local_cache.rb', line 56 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, seconds_to_store = 999999, raw = false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/local_cache.rb', line 66 def put(key, val, seconds_to_store=999999, raw=false) seconds_to_store = seconds_to_store || @default_expires_in #puts 'seconds=' + seconds_to_store.to_s @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 = nil) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/local_cache.rb', line 77 def read(name, = nil) # puts 'read from localcache' super ret = get(name) # puts 'ret.frozen=' + ret.frozen?.to_s return ret end |
#write(name, value, options = nil) ⇒ Object
85 86 87 88 89 |
# File 'lib/local_cache.rb', line 85 def write(name, value, = nil) super put(name, value, .nil? ? nil : [:expires_in]) # puts 'write.frozen=' + value.frozen?.to_s end |