Permalink
Browse files

add memcached gem support

  • Loading branch information...
1 parent 2f7cb0a commit 3be030a826e5e7341e172c56258d115e429d32bc Quake Wang committed May 1, 2008
Showing with 40 additions and 17 deletions.
  1. +9 −4 init.rb
  2. +1 −0 lib/acts_as_cached.rb
  3. +8 −8 lib/acts_as_cached/cache_methods.rb
  4. +5 −5 lib/acts_as_cached/config.rb
  5. +17 −0 lib/acts_as_cached/memcached_rails.rb
View
13 init.rb
@@ -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'
View
@@ -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 = {}
@@ -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
@@ -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) }
@@ -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]
@@ -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
@@ -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.