module App::Cache
Constants
- DEFAULT_MAX_AGE
Attributes
store[RW]
Public Class Methods
cached(key, max_age = DEFAULT_MAX_AGE) { || ... }
click to toggle source
# 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()
click to toggle source
# File lib/app/app/cache.rb, line 34 def self.clear store.flushdb end
marshal(value)
click to toggle source
# File lib/app/app/cache.rb, line 67 def self.marshal(value) Base64.encode64 Marshal.dump(value) end
setup()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/app/app/cache.rb, line 63 def self.unmarshal(marshalled) Marshal.load Base64.decode64(marshalled) if marshalled end