Skip to content

Instantly share code, notes, and snippets.

@grosser
Created August 20, 2009 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grosser/170853 to your computer and use it in GitHub Desktop.
Save grosser/170853 to your computer and use it in GitHub Desktop.
config.after_initialize do
#CACHE
MEMCACHE_SERVER = case RAILS_ENV
when 'development' then 'localhost:11211'
when 'staging' then '127.0.0.1:11211'
when 'production' then '192.168.2.26:11211'
when 'test' then nil
else raise
end
cache = if MEMCACHE_SERVER
ActiveSupport::Cache::MemCacheStore.new(MEMCACHE_SERVER, :c_threshold => 10_000,:compression => true,:debug => false,:namespace => 'xxx',:readonly => false,:urlencode => false)
else
ActiveSupport::Cache::MemoryStore.new
end
ActionController::Base.cache_store = cache #config.cache_store does not apply to view/controller caching
raw_cache = cache.instance_variable_get '@data'
CACHE = if cache.is_a? ActiveSupport::Cache::MemoryStore
TestCache.new(raw_cache)
else
raw_cache
end
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS['cache'] = CACHE
class TestCache
delegate "[]", "[]=", :keys, :delete, :clear, :to=>:@items
def initialize(hash)
@items = hash
end
def set(key, object, ttl = nil)
@items[key] = object
end
def get(key)
@items[key]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment