cacheify 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,10 +1,10 @@
1
1
  = Cacheify
2
2
 
3
- Simple caching of methods calls.
3
+ Easy caching of methods calls.
4
4
 
5
- Cacheify uses ActiveSupport::Cache::Store to perform caching so, depending on
6
- what you choose, it may cache to memory, disk, memcache or
7
- whatever is supported by the cache store.
5
+ Cacheify uses ActiveSupport::Cache::Store to do caching so, depending on
6
+ what you choose, it may cache to memory, disk, memcache or whatever is
7
+ supported by the cache store.
8
8
 
9
9
  == Installation
10
10
 
@@ -17,23 +17,33 @@ Mixin the Cacheify module, use the +cacheify+ method.
17
17
  require 'cacheify'
18
18
 
19
19
  class Foo
20
- include Cacheify
21
- cacheify :bar, :expires_in => 1.hour
22
-
23
20
  def bar
24
21
  end
22
+
23
+ include Cacheify
24
+ cacheify :bar, :expires_in => 1.hour
25
25
  end
26
26
 
27
27
  And you're +foo+ instance calls to +bar+ will get cached.
28
28
 
29
29
  Foo.new.bar # will hit cache if cached, or get cached otherwise
30
30
 
31
- To enable cacheify on existing class just +extend+:
31
+ Caveat: You have to call +cacheify+ *after* defining +bar+.
32
+
33
+ To enable cacheify on the existing class just +extend+:
32
34
 
33
35
  Thing.extend Cacheify
34
36
  Thing.caches :very_expensive_operation
35
37
 
36
- If you use Cacheify <b>a lot</b>, you may even do:
38
+ To cache a method on a specific object:
39
+
40
+ boo = Thing.new
41
+ boo.extend Cacheify
42
+ boo.caches :very_expensive_operation
43
+
44
+ And only that instance of +Thing+ will have its +very_expensive_operation+ cached.
45
+
46
+ If you use Cacheify <b>a lot</b>, you may do:
37
47
 
38
48
  Object.extend Cacheify
39
49
 
@@ -50,7 +60,7 @@ The arguments are the same as in ActiveSupport::Cache#lookup_store[http://api.ru
50
60
  == More info
51
61
 
52
62
  * ActiveSupport::Cache[http://api.rubyonrails.org/classes/ActiveSupport/Cache.html] is what's used for caching
53
- * Why cacheify? {Introductory blog post}[http://blog.hakeraj.com].
63
+ * Why cacheify? {Introductory blog post}[http://blog.hakeraj.com/easy-caching-of-methods-with-cacheify].
54
64
 
55
65
  == Note on Patches/Pull Requests
56
66
 
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ begin
9
9
  gem.description = %Q{Cacheify can cache method calls to memory, disk, memcache or whatever is supported by the ActiveSupport::Cache::Store.}
10
10
  gem.email = "sasa@hakeraj.com"
11
11
  gem.homepage = "http://github.com/fox/cacheify"
12
- gem.authors = ["Saša_Branković"]
12
+ gem.authors = ["Saša Branković"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
14
  gem.add_dependency "activesupport"
15
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/cacheify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cacheify}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Sa\305\241a_Brankovi\304\207"]
12
- s.date = %q{2010-03-12}
11
+ s.authors = ["Sa\305\241a Brankovi\304\207"]
12
+ s.date = %q{2010-03-16}
13
13
  s.description = %q{Cacheify can cache method calls to memory, disk, memcache or whatever is supported by the ActiveSupport::Cache::Store.}
14
14
  s.email = %q{sasa@hakeraj.com}
15
15
  s.extra_rdoc_files = [
data/lib/cacheify.rb CHANGED
@@ -30,20 +30,25 @@ module Cacheify
30
30
  # === Example
31
31
  # cache_method :big_calculation, :expires_in => 5.minutes
32
32
  def cacheify(*symbols)
33
- options = symbols.extract_options!
33
+ (self.is_a?(Class) ? self : metaclass).class_eval do
34
+ options = symbols.extract_options!
34
35
 
35
- symbols.each do |method|
36
- non_cached_method = "_non_cached_#{method}".to_sym
37
- return if respond_to? non_cached_method # cached already, skip it
38
-
39
- alias_method non_cached_method, method
40
-
41
- define_method(method) do |*args, &block|
42
- cache_name = Digest::MD5.hexdigest(args.inspect)
43
- marshalled_result = Cacheify.cache.fetch(cache_name, options) do
44
- send(non_cached_method, *args, &block)
36
+ symbols.each do |method|
37
+ non_cached_method = "_non_cached_#{method}".to_sym
38
+ return if method_defined?(non_cached_method) # cached already, skip it
39
+ alias_method non_cached_method, method
40
+
41
+ define_method(method) do |*args, &block|
42
+ cache_name = Digest::MD5.hexdigest "#{self.class.name}#{method}#{args.inspect}"
43
+ marshalled_result = Cacheify.cache.fetch(cache_name, options) do
44
+ send(non_cached_method, *args, &block)
45
+ end
45
46
  end
46
47
  end
47
48
  end
48
49
  end
50
+
51
+ def self.included(obj)
52
+ obj.extend Cacheify
53
+ end
49
54
  end
@@ -1,28 +1,70 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- # Example class we'll use in specs
3
+ # Example classes we'll use in specs
4
+
4
5
  class Foo
5
6
  def bar(something = nil)
6
7
  "#{rand(99999999)}#{something}"
7
8
  end
9
+
10
+ def rab(something = nil)
11
+ "#{rand(99999999)}#{something}"
12
+ end
13
+ end
14
+
15
+ class Hoo
16
+ def bar(something = nil)
17
+ "#{rand(99999999)}#{something}"
18
+ end
19
+ end
20
+
21
+ class Baz
22
+ def boo
23
+ "#{rand(99999999)}"
24
+ end
8
25
  end
9
26
 
10
27
  describe "Cacheify" do
11
28
  before :all do
12
29
  Foo.extend Cacheify
13
- Foo.cacheify :bar
30
+ Hoo.extend Cacheify
31
+
32
+ Foo.cacheify :bar, :rab
33
+ Hoo.cacheify :bar
34
+
35
+ foo = Foo.new
36
+ hoo = Hoo.new
14
37
 
15
- @foo = Foo.new
16
- @first_call = @foo.bar("hello")
38
+ @foo_bar_1_call = foo.bar("hello")
39
+ @foo_bar_2_call = foo.bar("hello")
40
+ @foo_bar_diff_args_1_call = foo.bar("howdy")
41
+ @foo_rab_1_call = foo.rab("hello")
42
+ @hoo_bar_1_call = hoo.bar("hello")
43
+ @hoo_bar_2_call = hoo.bar("hello")
17
44
  end
18
45
 
19
- context "calling method twice on a cachified object" do
46
+ it "should return cached result second time" do
47
+ @foo_bar_1_call.should == @foo_bar_2_call
48
+ end
49
+
50
+ specify "cache should be scoped to class, method name and args" do
51
+ @hoo_bar_1_call.should_not == @foo_bar_1_call
52
+ @foo_rab_1_call.should_not == @foo_bar_1_call
53
+ @foo_bar_diff_args_1_call.should_not == @foo_bar_1_call
54
+ # @hoo_bar_1_call.should == @hoo_bar_2_call
55
+ end
56
+
57
+ context "Caching of instantiated object" do
20
58
  before do
21
- @second_call = @foo.bar("hello")
59
+ baz = Baz.new
60
+ baz.extend Cacheify
61
+ baz.cacheify :boo
62
+ @baz_boo_1_call = baz.boo
63
+ @baz_boo_2_call = baz.boo
22
64
  end
23
-
24
- it "should return cached result second time" do
25
- @first_call.should == @second_call
65
+
66
+ it do
67
+ @baz_boo_1_call.should == @baz_boo_2_call
26
68
  end
27
69
  end
28
70
  end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,10 @@ require 'cacheify'
4
4
  require 'spec'
5
5
  require 'spec/autorun'
6
6
 
7
+
8
+ Cacheify.cache_store = :memory_store
9
+
10
+
7
11
  Spec::Runner.configure do |config|
8
12
 
9
13
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cacheify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - "Sa\xC5\xA1a_Brankovi\xC4\x87"
7
+ - "Sa\xC5\xA1a Brankovi\xC4\x87"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-12 00:00:00 +01:00
12
+ date: 2010-03-16 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency