Permalink
Browse files

More refactoring; MemCached storage introduced

git-svn-id: http://svn.verbdev.com/rb/caches.rb/trunk@62 f28541f9-331e-0410-82d8-e175c77b79a1
  • Loading branch information...
1 parent ca36e6f commit 8f4c7a4eba37d55fc60862576818c98c538028b8 yrashk committed Apr 1, 2007
Showing with 120 additions and 75 deletions.
  1. +3 −2 Rakefile
  2. +5 −1 init.rb
  3. +107 −69 lib/caches.rb
  4. +4 −2 spec/ar_caches_spec.rb
  5. +1 −1 spec/caches_spec.rb
View
@@ -16,7 +16,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
-PKG_VERSION = "0.3.0"
+PKG_VERSION = "0.4.0"
PKG_NAME = "cachesrb"
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
@@ -36,7 +36,8 @@ spec = Gem::Specification.new do |s|
s.author = "Yurii Rashkovskii"
s.email = "yrashk@verbdev.com"
s.homepage = "http://pad.verbdev.com/cachesrb"
- s.rubyforge_project = "cachesrb"
+ s.rubyforge_project = "cachesrb"
+ s.add_dependency 'activesupport'
end
Rake::GemPackageTask.new(spec) do |p|
View
@@ -3,6 +3,10 @@
begin
Kernel.const_get :ActiveRecord
ActiveRecord::Base.extend Caches
- ActiveRecord::Base.instance_cache_storage CachesStorage::ClassVarById
+ ActiveRecord::Base.instance_cache_storage Caches::Storage::Class
+ ActiveRecord::Base.class_eval do
+ include Caches::Helper::PerID
+ end
+
rescue
end
View
@@ -1,89 +1,115 @@
-class String
- def starts_with?(prefix)
- prefix = prefix.to_s
- self[0, prefix.length] == prefix
- end
-end
+require 'rubygems'
+gem 'activesupport'
+require 'active_support'
-module CachesStorage
- module Instance
+begin
+ require 'memcache'
+rescue
+end
+module Caches
+ module Helper
- protected
+ module Default
+ def cachesrb_method_key(name,*args)
+ "#{name}#{args.hash}"
+ end
- def _propcache
- @propcache
+ def cachesrb_object_key(name)
+ "#{name}"
+ end
end
+ module Class
+ def cachesrb_method_key(name,*args)
+ "#{self.class.name}#{name}#{args.hash}"
+ end
- def _propcache=(v)
- @propcache=v
+ def cachesrb_object_key(name)
+ "#{self.class.name}#{name}"
+ end
end
+ module PerID
- def _call_key(name,*args)
- "#{name}#{args.hash}"
- end
+ protected
- def _object_key(name)
- "#{name}"
- end
+ def cachesrb_method_key(name,*args)
+ "#{name}#{args.hash}_#{self.id}"
+ end
+ def cachesrb_object_key(name)
+ "#{name}_#{self.id}"
+ end
+
+ end
end
- module Global
+ module Storage
+ module Instance
- protected
+ protected
- def _propcache
- $cachesrb_propcache ||= {}
- end
+ attr_accessor :cachesrb_cache
- def _propcache=(v)
- $cachesrb_propcache=v
- end
+ include ::Caches::Helper::Default
- def _call_key(name,*args)
- "#{self.class.name}#{name}#{args.hash}"
end
- def _object_key(name)
- "#{self.class.name}#{name}"
+ module Global
+
+ protected
+
+ def cachesrb_cache
+ $cachesrbcachesrb_cache ||= {}
+ end
+
+ def cachesrb_cache=(v)
+ $cachesrbcachesrb_cache=v
+ end
+
+ include ::Caches::Helper::Class
+
end
- end
+ module Class
- module ClassVarById
+ protected
+ def cachesrb_cache
+ @@cachesrb_cache ||= {}
+ end
- protected
+ def cachesrb_cache=(v)
+ @@cachesrb_cache=v
+ end
- def _propcache
- @@propcache ||= {}
- end
+ include ::Caches::Helper::Default
- def _propcache=(v)
- @@propcache=v
end
- def _call_key(name,*args)
- "#{name}#{args.hash}_#{self.id}"
- end
+ module MemCached
- def _object_key(name)
- "#{name}_#{self.id}"
- end
+ protected
+ def cachesrb_cache
+ @cache ||= MemCache::new cachesrb_storage_options[:host], cachesrb_storage_options
+ end
- end
-end
+ def cachesrb_cache=(v)
+ @cache ||= MemCache::new cachesrb_storage_options[:host], cachesrb_storage_options
+ @cache=v
+ end
+ include ::Caches::Helper::Class
+
+ end
+ end
-module Caches
def cached_methods
@@cached_methods ||= {}
end
def caches(name,options = {})
- options = { :timeout => 60}.merge options
+ options.reverse_merge! :timeout => 60
sanitized_name = name.to_s.delete('?')
saved_getter = "getter_#{name}"
saved_setter = "setter_#{name}"
@@ -94,7 +120,7 @@ def caches(name,options = {})
alias_method(saved_setter, setter.to_sym) if has_setter
self.cached_methods[name] = options
module_eval do
- include CachesStorage::Instance unless protected_method_defined?(:_propcache)
+ include Storage::Instance unless protected_method_defined?(:cachesrb_cache)
public
@@ -104,32 +130,32 @@ def invalidate_all_caches(*opts)
except = opthash[:except]
if except
except = [except] unless except.kind_of? Enumerable
- self._propcache.each_pair {|k,v| self._propcache[k] = nil unless except.any? {|exception| k.starts_with?(exception.to_s)} }
+ self.cachesrb_cache.each_pair {|k,v| self.cachesrb_cache[k] = nil unless except.any? {|exception| k.starts_with?(exception.to_s)} }
end
else
- self._propcache = {}
+ self.cachesrb_cache = {}
end
end
define_method("invalidate_#{sanitized_name}_cache") do |*args|
- self._propcache ||= {}
- key = _call_key(name,*args)
- self._propcache[key] = nil
+ self.cachesrb_cache ||= {}
+ key = cachesrb_method_key(name,*args)
+ self.cachesrb_cache[key] = nil
end
define_method("invalidate_all_#{sanitized_name}_caches") do
- self._propcache ||= {}
- key = _object_key(name)
- self._propcache.keys.each {|k| self._propcache[k] = nil if k.starts_with?(key) }
+ self.cachesrb_cache ||= {}
+ key = cachesrb_object_key(name)
+ self.cachesrb_cache.keys.each {|k| self.cachesrb_cache[k] = nil if k.starts_with?(key) }
end
define_method("#{name}") do |*args|
- self._propcache ||= {}
- key = _call_key(name,*args)
- cached = self._propcache[key]
+ self.cachesrb_cache ||= {}
+ key = cachesrb_method_key(name,*args)
+ cached = self.cachesrb_cache[key]
unless cached
- self._propcache[key] = { :value => self.send(saved_getter.to_sym,*args), :expires_at => Time.now.to_i + options[:timeout]}
- return self._propcache[key][:value]
+ self.cachesrb_cache[key] = { :value => self.send(saved_getter.to_sym,*args), :expires_at => Time.now.to_i + options[:timeout]}
+ return self.cachesrb_cache[key][:value]
else
unless Time.now.to_i > cached[:expires_at]
cached[:value]
@@ -158,18 +184,30 @@ class <<self
c
end
- def class_cache_storage(storage)
- class_eval %{
+ def class_cache_storage(storage,options={})
+ c = class_eval %{
class <<self
include #{storage}
end
}
+ c.class_eval do
+ protected
+ define_method("cachesrb_storage_options") do
+ options
+ end
+ end
end
-
- def instance_cache_storage(storage)
- class_eval %{
- include #{storage}
+
+ def instance_cache_storage(storage,options={})
+ c = class_eval %{
+ include #{storage}
}
+ c.class_eval do
+ protected
+ define_method("cachesrb_storage_options") do
+ options
+ end
+ end
end
def caches?(name)
View
@@ -4,8 +4,10 @@ module ActiveRecord ; class Base ; end ; end
context "ActiveRecord instance" do
ActiveRecord::Base.extend Caches
- ActiveRecord::Base.instance_cache_storage CachesStorage::ClassVarById
-
+ ActiveRecord::Base.instance_cache_storage Caches::Storage::Class
+ ActiveRecord::Base.class_eval do
+ include Caches::Helper::PerID
+ end
class Model < ActiveRecord::Base
attr_reader :accessor_counter
View
@@ -18,7 +18,7 @@ def self.test
Time.now
end
extend Caches
- class_cache_storage CachesStorage::Global
+ class_cache_storage Caches::Storage::Global # MemCached, :host => 'localhost:11211', :namespace => 'rails'
class_caches :test
end}

0 comments on commit 8f4c7a4

Please sign in to comment.