Module: App::Cache
Defined Under Namespace
Classes: SqliteStore
Constant Summary collapse
- DEFAULT_MAX_AGE =
4 hours.
4 * 3600
Instance Attribute Summary collapse
-
#store ⇒ Object
Returns the value of attribute store.
Class Method Summary collapse
- .cached(key, max_age = DEFAULT_MAX_AGE, &block) ⇒ Object
- .clear ⇒ Object
- .marshal(value) ⇒ Object
- .setup ⇒ Object
- .uid(key) ⇒ Object
- .unmarshal(marshalled) ⇒ Object
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
31 32 33 |
# File 'lib/app/app/cache.rb', line 31 def store @store end |
Class Method Details
.cached(key, max_age = DEFAULT_MAX_AGE, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/app/app/cache.rb', line 48 def self.cached(key, max_age = DEFAULT_MAX_AGE, &block) cache_id = uid(key) return yield if !store || !max_age || !cache_id if marshalled = store.get(cache_id) unmarshal(marshalled) else yield.tap { |v| store.set(cache_id, marshal(v)) store.expire(cache_id, max_age) } end end |
.clear ⇒ Object
34 35 36 |
# File 'lib/app/app/cache.rb', line 34 def self.clear store.flushdb end |
.marshal(value) ⇒ Object
67 68 69 |
# File 'lib/app/app/cache.rb', line 67 def self.marshal(value) Base64.encode64 Marshal.dump(value) end |
.setup ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/app/app/cache.rb', line 71 def self.setup cache_url = App.config[:cache] || begin App.logger.warn "No :cache configuration, fallback to #{SqliteStore.db_path}" SqliteStore.db_path end uri = URI.parse(cache_url) self.store = case uri.scheme when "redis" require "redis" Redis.connect(:url => cache_url) when nil SqliteStore.new end rescue LoadError App.logger.warn "LoadError: #{$!}" nil end |
.uid(key) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/app/app/cache.rb', line 38 def self.uid(key) case key when String, Hash then key.uid64 when Fixnum then key else App.logger.warn "Don't know how to deal with non-uid key #{key}" nil end end |
.unmarshal(marshalled) ⇒ Object
63 64 65 |
# File 'lib/app/app/cache.rb', line 63 def self.unmarshal(marshalled) Marshal.load Base64.decode64(marshalled) if marshalled end |