Skip to content

Instantly share code, notes, and snippets.

@takeru
Created April 15, 2010 22:40
Show Gist options
  • Select an option

  • Save takeru/367759 to your computer and use it in GitHub Desktop.

Select an option

Save takeru/367759 to your computer and use it in GitHub Desktop.
# add to config/environments/production.rb or development.rb
require "appengine_mem_cache_store.rb"
config.cache_store = AppEngine::Memcache::RailsStore.new(:namespace=>"cache_store")
config.action_controller.perform_caching = true
require 'appengine-apis/memcache'
module AppEngine
class Memcache
class RailsStore < ActiveSupport::Cache::Store
def initialize(opts={})
@data = AppEngine::Memcache.new(opts)
#extend ActiveSupport::Cache::Strategy::LocalCache
end
# Reads multiple keys from the cache.
def read_multi(*keys)
@data.get_multi keys
end
def read(key, options = nil) # :nodoc:
super
@data.get(key)
rescue MemcacheError => e
logger.error("MemcacheError (#{e}): #{e.message}")
nil
end
# Writes a value to the cache.
#
# Possible options:
# - +:unless_exist+ - set to true if you don't want to update the cache
# if the key is already set.
# - +:expires_in+ - the number of seconds that this value may stay in
# the cache. See ActiveSupport::Cache::Store#write for an example.
def write(key, value, options = nil)
super
method = options && options[:unless_exist] ? :add : :set
response = @data.send(method, key, value, expires_in(options))
response == true
rescue MemcacheError => e
logger.error("MemcacheError (#{e}): #{e.message}")
false
end
def delete(key, options = nil) # :nodoc:
super
response = @data.delete(key, expires_in(options))
response == true
rescue MemcacheError => e
logger.error("MemcacheError (#{e}): #{e.message}")
false
end
def exist?(key, options = nil) # :nodoc:
# Doesn't call super, cause exist? in memcache is in fact a read
# But who cares? Reading is very fast anyway
# Local cache is checked first, if it doesn't know then memcache itself is read from
!read(key, options).nil?
end
def increment(key, amount = 1) # :nodoc:
log("incrementing", key, amount)
response = @data.incr(key, amount)
response
rescue MemcacheError
nil
end
def decrement(key, amount = 1) # :nodoc:
log("decrement", key, amount)
response = @data.decr(key, amount)
response
rescue MemcacheError
nil
end
def delete_matched(matcher, options = nil) # :nodoc:
# don't do any local caching at present, just pass
# through and let the error happen
super
raise "Not supported by Memcache"
end
def clear
@data.flush_all
end
def stats
@data.stats
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment