Skip to content

Commit

Permalink
add memcached gem support
Browse files Browse the repository at this point in the history
  • Loading branch information
Quake Wang committed May 1, 2008
1 parent 2f7cb0a commit 3be030a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
13 changes: 9 additions & 4 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require 'memcache'
puts "=> You should be using the `memcache-client' gem. You're using RubyMemcache!" if Object.const_defined?(:RubyMemcache)
begin
require 'memcache'
rescue LoadError
end

begin
require 'memcached'
rescue LoadError
end

begin
require 'mem_cache_with_consistent_hashing'
rescue LoadError
else
puts "=> MemCacheWithConsistentHashing being used."
end

require 'acts_as_cached'
Expand Down
1 change: 1 addition & 0 deletions lib/acts_as_cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'acts_as_cached/benchmarking'
require 'acts_as_cached/disabled'
require 'acts_as_cached/local_cache'
require 'acts_as_cached/memcached_rails'

module ActsAsCached
@@config = {}
Expand Down
16 changes: 8 additions & 8 deletions lib/acts_as_cached/cache_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,19 @@ def cache_store(method = nil, *args)

def swallow_or_raise_cache_errors(load_constants = false, &block)
load_constants ? autoload_missing_constants(&block) : yield
rescue NoMethodError, ArgumentError, MemCache::MemCacheError => error
if ActsAsCached.config[:raise_errors]
raise error
else
RAILS_DEFAULT_LOGGER.debug "MemCache Error: #{error.message}" rescue nil
nil
end
rescue TypeError => error
if error.to_s.include? 'Proc'
raise MarshalError, "Most likely an association callback defined with a Proc is triggered, see http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html (Association Callbacks) for details on converting this to a method based callback"
else
raise error
end
rescue Exception => error
if ActsAsCached.config[:raise_errors]
raise error
else
RAILS_DEFAULT_LOGGER.debug "MemCache Error: #{error.message}" rescue nil
nil
end
end

def autoload_missing_constants
Expand Down Expand Up @@ -277,7 +277,7 @@ def set_cache_with_associations
set_cache
end

# Lourens Naudé
# Lourens Naud
def expire_cache_with_associations(*associations_to_sweep)
(Array(cache_options[:include]) + associations_to_sweep).flatten.uniq.compact.each do |assoc|
Array(send(assoc)).compact.each { |item| item.expire_cache if item.respond_to?(:expire_cache) }
Expand Down
10 changes: 5 additions & 5 deletions lib/acts_as_cached/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def setup_memcache(config)
config[:namespace] << "-#{RAILS_ENV}"

silence_warnings do
Object.const_set :CACHE, memcache_klass.new(config)
Object.const_set :SESSION_CACHE, memcache_klass.new(config) if config[:session_servers]
Object.const_set :CACHE, memcache_client(config)
Object.const_set :SESSION_CACHE, memcache_client(config) if config[:session_servers]
end

CACHE.servers = Array(config.delete(:servers))
SESSION_CACHE.servers = Array(config[:session_servers]) if config[:session_servers]

setup_session_store if config[:sessions]
setup_fragment_store! if config[:fragments]
setup_fast_hash! if config[:fast_hash]
Expand All @@ -59,8 +59,8 @@ def setup_memcache(config)
CACHE
end

def memcache_klass
Object.const_defined?(:MemCacheWithConsistentHashing) ? MemCacheWithConsistentHashing : MemCache
def memcache_client(config)
(config[:client] || "MemCache").constantize.new(config)
end

def setup_session_store
Expand Down
17 changes: 17 additions & 0 deletions lib/acts_as_cached/memcached_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Memcached
# A legacy compatibility wrapper for the Memcached class. It has basic compatibility with the <b>memcache-client</b> API.
class Rails < ::Memcached
def initialize(config)
super(config.delete(:servers), config.slice(DEFAULTS.keys))
end

def servers=(servers)

end

def delete(key, expiry = 0)
super(key)
rescue NotFound
end
end
end

0 comments on commit 3be030a

Please sign in to comment.