simple_cacheable 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,4 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - jruby-head
4
+ - jruby-19mode
5
+ env: DB=sqlite
6
+ service:
7
+ - memcached
data/Gemfile CHANGED
@@ -5,9 +5,11 @@ gemspec
5
5
 
6
6
  platforms :ruby do
7
7
  gem "sqlite3-ruby"
8
+ gem "memcached"
8
9
  end
9
10
 
10
11
  platforms :jruby do
11
12
  gem "activerecord-jdbc-adapter"
12
13
  gem "activerecord-jdbcsqlite3-adapter"
14
+ gem "jruby-memcached"
13
15
  end
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/cacheable.gemspec CHANGED
@@ -15,7 +15,6 @@ Gem::Specification.new do |s|
15
15
  s.add_dependency("rails", ">= 3.0.0")
16
16
  s.add_development_dependency("rspec")
17
17
  s.add_development_dependency("mocha")
18
- s.add_development_dependency("memcached")
19
18
 
20
19
  s.files = `git ls-files`.split("\n")
21
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/lib/cacheable.rb CHANGED
@@ -28,6 +28,8 @@ module Cacheable
28
28
 
29
29
  class_eval <<-EOF
30
30
  after_commit :expire_attribute_cache, :on => :update
31
+ after_commit :expire_all_attribute_cache, :on => :update
32
+
31
33
  EOF
32
34
 
33
35
  attributes.each do |attribute|
@@ -39,6 +41,14 @@ module Cacheable
39
41
  self.find_by_#{attribute}(value)
40
42
  end
41
43
  end
44
+
45
+ def self.find_cached_all_by_#{attribute}(value)
46
+ self.cached_indices["#{attribute}"] ||= []
47
+ self.cached_indices["#{attribute}"] << value
48
+ Rails.cache.fetch all_attribute_cache_key("#{attribute}", value) do
49
+ self.find_all_by_#{attribute}(value)
50
+ end
51
+ end
42
52
  EOF
43
53
  end
44
54
  end
@@ -102,7 +112,11 @@ module Cacheable
102
112
  end
103
113
 
104
114
  def attribute_cache_key(attribute, value)
105
- "#{name.tableize}/attribute/#{attribute}/#{URI.escape(value)}"
115
+ "#{name.tableize}/attribute/#{attribute}/#{URI.escape(value.to_s)}"
116
+ end
117
+
118
+ def all_attribute_cache_key(attribute, value)
119
+ "#{name.tableize}/attribute/#{attribute}/all/#{URI.escape(value.to_s)}"
106
120
  end
107
121
  end
108
122
  end
@@ -125,6 +139,13 @@ module Cacheable
125
139
  end
126
140
  end
127
141
 
142
+ def expire_all_attribute_cache
143
+ self.class.cached_indices.each do |attribute, values|
144
+ value = self.send(attribute)
145
+ Rails.cache.delete self.class.all_attribute_cache_key(attribute, value)
146
+ end
147
+ end
148
+
128
149
  def expire_method_cache
129
150
  self.class.cached_methods.each do |meth|
130
151
  Rails.cache.delete method_cache_key(meth)
@@ -1,3 +1,3 @@
1
1
  module Cacheable
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -51,8 +51,31 @@ describe Cacheable do
51
51
  new_user = User.create(:login => "user space")
52
52
  User.find_cached_by_login("user space").should == new_user
53
53
  end
54
+
55
+ it "should handle fixed numbers" do
56
+ Post.find_cached_by_user_id(@user.id).should == @post1
57
+ Rails.cache.read("posts/attribute/user_id/#{@user.id}").should == @post1
58
+ end
59
+
60
+ context "find_all" do
61
+ it "should not cache Post.find_all_by_user_id" do
62
+ Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should be_nil
63
+ end
64
+
65
+ it "should cache by Post.find_cached_all_by_user_id" do
66
+ Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
67
+ Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should == [@post1, @post2]
68
+ end
69
+
70
+ it "should get cached by Post.find_cached_all_by_user_id multiple times" do
71
+ Post.find_cached_all_by_user_id(@user.id)
72
+ Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
73
+ end
74
+
75
+ end
54
76
  end
55
77
 
78
+
56
79
  context "with_method" do
57
80
  it "should not cache User.last_post" do
58
81
  Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
data/spec/models/post.rb CHANGED
@@ -6,6 +6,7 @@ class Post < ActiveRecord::Base
6
6
 
7
7
  model_cache do
8
8
  with_key
9
+ with_attribute :user_id
9
10
  with_association :user, :comments
10
11
  end
11
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-25 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: memcached
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
62
  description: a simple cache implementation based on activerecord
79
63
  email:
80
64
  - flyerhzm@gmail.com
@@ -112,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
96
  version: '0'
113
97
  segments:
114
98
  - 0
115
- hash: 2779579293480982592
99
+ hash: 3153492415612619050
116
100
  required_rubygems_version: !ruby/object:Gem::Requirement
117
101
  none: false
118
102
  requirements:
@@ -121,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
105
  version: '0'
122
106
  segments:
123
107
  - 0
124
- hash: 2779579293480982592
108
+ hash: 3153492415612619050
125
109
  requirements: []
126
110
  rubyforge_project:
127
111
  rubygems_version: 1.8.24