second_level_cache 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06b57d2d883c83e10d5b5b927b1666e92a84c6b1
4
- data.tar.gz: 80c99a97b2b2753f98b886a6f4d50de61e6352ba
3
+ metadata.gz: 3c1235017053004991468a6a2eb8661d8e9d82e7
4
+ data.tar.gz: db06d8212c45306ba2b895765a2aa90a30e29569
5
5
  SHA512:
6
- metadata.gz: eec1e049ea36e70c0d9c707efdb1aeda373c327b6b2be1082076619373c5d6b9bc782797dedd2cec2d4c52e947363282d4f1d3f3a43d12de7f2e2b96aff418f3
7
- data.tar.gz: 9dd02f79e8312da7af052282a89ddca1328209ffe5b6b68f2815fd394bd6cbd9820fc6c4d862d89009324e5ccf2c748bdbc362726cabeb304daa9c0000ccc0ac
6
+ metadata.gz: 467e7aba16374a978b653ca9c4236214ad33d178b10643027b4e2adc45337016b292dc212ae6ce7985d2ac2794765ba9038583decaa000183e7ef081846b6038
7
+ data.tar.gz: b66b78795b29cbabd3dc263c2a22f11681acf937dac404629af4d0b754e948926fea1e4730e1631a4d1b5c8069d2792b6e54d486cd2def0901c956b9cc746829
data/README.md CHANGED
@@ -17,7 +17,7 @@ Write-Through: As objects are created, updated, and deleted, all of the caches a
17
17
  In your gem file:
18
18
 
19
19
  ```ruby
20
- gem "second_level_cache", "~> 2.1.0"
20
+ gem "second_level_cache", "~> 2.1.1"
21
21
  ```
22
22
 
23
23
  For ActiveRecord 3:
@@ -55,7 +55,7 @@ module SecondLevelCache
55
55
  end
56
56
 
57
57
  def second_level_cache_key(id)
58
- "#{cache_key_prefix}/#{name.downcase}/#{id}/#{cache_version}"
58
+ "#{cache_key_prefix}/#{table_name.downcase}/#{id}/#{cache_version}"
59
59
  end
60
60
 
61
61
  def read_second_level_cache(id)
@@ -1,15 +1,11 @@
1
1
  # -*- encoding : utf-8 -*-
2
- require 'second_level_cache/arel/wheres'
3
-
4
2
  module SecondLevelCache
5
3
  module ActiveRecord
6
4
  module FinderMethods
7
5
  extend ActiveSupport::Concern
8
6
 
9
7
  included do
10
- class_eval do
11
- alias_method_chain :find_one, :second_level_cache
12
- end
8
+ alias_method_chain :find_one, :second_level_cache
13
9
  end
14
10
 
15
11
  # TODO find_some
@@ -28,46 +24,37 @@ module SecondLevelCache
28
24
  def find_one_with_second_level_cache(id)
29
25
  return find_one_without_second_level_cache(id) unless second_level_cache_enabled?
30
26
  return find_one_without_second_level_cache(id) unless select_all_column?
31
-
27
+
32
28
  id = id.id if ActiveRecord::Base === id
33
29
 
34
30
  if cachable?
35
- if record = @klass.read_second_level_cache(id)
36
- return record
37
- end
38
- end
39
-
40
- if cachable_without_conditions?
41
- if record = @klass.read_second_level_cache(id)
42
- return record if where_match_with_cache?(where_values, record)
31
+ record = @klass.read_second_level_cache(id)
32
+ if record
33
+ return record if where_values.blank? || where_values_match_cache?(record)
43
34
  end
44
35
  end
45
-
36
+
46
37
  record = find_one_without_second_level_cache(id)
47
38
  record.write_second_level_cache
48
39
  record
49
40
  end
50
41
 
51
- private
42
+ private
52
43
 
53
44
  def cachable?
54
- where_values.blank? &&
55
- limit_one? && order_values.blank? &&
56
- includes_values.blank? && preload_values.blank? &&
57
- readonly_value.nil? && joins_values.blank? && !@klass.locking_enabled?
58
- end
59
-
60
- def cachable_without_conditions?
61
45
  limit_one? && order_values.blank? &&
62
46
  includes_values.blank? && preload_values.blank? &&
63
- readonly_value.nil? && joins_values.blank? && !@klass.locking_enabled?
47
+ readonly_value.nil? && joins_values.blank? && !@klass.locking_enabled? &&
48
+ where_values.all? { |where_value| where_value.is_a?(::Arel::Nodes::Equality) }
64
49
  end
65
50
 
66
- def where_match_with_cache?(where_values, cache_record)
67
- condition = SecondLevelCache::Arel::Wheres.new(where_values)
68
- return false unless condition.all_equality?
69
- condition.extract_pairs.all? do |pair|
70
- cache_record.read_attribute(pair[:left]) == pair[:right]
51
+ def where_values_match_cache?(record)
52
+ where_values_hash.all? do |key, value|
53
+ if value.is_a?(Array)
54
+ value.include?(record.read_attribute(key))
55
+ else
56
+ record.read_attribute(key) == value
57
+ end
71
58
  end
72
59
  end
73
60
 
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "2.1.0"
3
+ VERSION = "2.1.1"
4
4
  end
@@ -0,0 +1,14 @@
1
+ # -*- encoding : utf-8 -*-
2
+ ActiveRecord::Base.connection.create_table(:animals, force: true) do |t|
3
+ t.string :type
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+
8
+ class Animal < ActiveRecord::Base
9
+ acts_as_cached
10
+ end
11
+
12
+ class Dog < Animal
13
+ acts_as_cached
14
+ end
@@ -7,7 +7,7 @@ class SecondLevelCacheTest < ActiveSupport::TestCase
7
7
  end
8
8
 
9
9
  def test_should_get_cache_key
10
- assert_equal "slc/user/#{@user.id}/#{User::CacheVersion}", @user.second_level_cache_key
10
+ assert_equal "slc/users/#{@user.id}/#{User::CacheVersion}", @user.second_level_cache_key
11
11
  end
12
12
 
13
13
  def test_should_write_and_read_cache
@@ -0,0 +1,36 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class SingleTableInheritanceTest < ActiveSupport::TestCase
5
+ def test_superclass_find__caches_superclass_record
6
+ animal = Animal.create
7
+ assert_no_queries do
8
+ assert_equal animal, Animal.find(animal.id)
9
+ end
10
+ end
11
+
12
+ def test_superclass_find__caches_subclass_record
13
+ dog = Dog.create
14
+ assert_no_queries do
15
+ assert_equal dog, Animal.find(dog.id)
16
+ end
17
+ end
18
+
19
+ def test_subclass_find__caches_subclass_record
20
+ dog = Dog.create
21
+ dog_id = dog.id
22
+ assert_no_queries do
23
+ newdog = Dog.find(dog_id)
24
+ assert_equal dog, newdog
25
+ end
26
+ end
27
+
28
+ def test_subclass_find__doesnt_find_superclass_record
29
+ animal = Animal.create
30
+ assert_queries(:any) do
31
+ assert_raises ActiveRecord::RecordNotFound do
32
+ Dog.find(animal.id)
33
+ end
34
+ end
35
+ end
36
+ end
data/test/test_helper.rb CHANGED
@@ -16,6 +16,7 @@ require 'model/image'
16
16
  require 'model/topic'
17
17
  require 'model/post'
18
18
  require 'model/account'
19
+ require 'model/animal'
19
20
 
20
21
  DatabaseCleaner[:active_record].strategy = :truncation
21
22
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: second_level_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-23 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -113,7 +113,6 @@ files:
113
113
  - lib/second_level_cache/active_record/has_one_association.rb
114
114
  - lib/second_level_cache/active_record/persistence.rb
115
115
  - lib/second_level_cache/active_record/preloader.rb
116
- - lib/second_level_cache/arel/wheres.rb
117
116
  - lib/second_level_cache/config.rb
118
117
  - lib/second_level_cache/record_marshal.rb
119
118
  - lib/second_level_cache/version.rb
@@ -125,6 +124,7 @@ files:
125
124
  - test/finder_methods_test.rb
126
125
  - test/has_one_association_test.rb
127
126
  - test/model/account.rb
127
+ - test/model/animal.rb
128
128
  - test/model/book.rb
129
129
  - test/model/image.rb
130
130
  - test/model/post.rb
@@ -136,6 +136,7 @@ files:
136
136
  - test/record_marshal_test.rb
137
137
  - test/require_test.rb
138
138
  - test/second_level_cache_test.rb
139
+ - test/single_table_inheritance_test.rb
139
140
  - test/test_helper.rb
140
141
  homepage: https://github.com/csdn-dev/second_level_cache
141
142
  licenses: []
@@ -173,6 +174,7 @@ test_files:
173
174
  - test/finder_methods_test.rb
174
175
  - test/has_one_association_test.rb
175
176
  - test/model/account.rb
177
+ - test/model/animal.rb
176
178
  - test/model/book.rb
177
179
  - test/model/image.rb
178
180
  - test/model/post.rb
@@ -184,4 +186,5 @@ test_files:
184
186
  - test/record_marshal_test.rb
185
187
  - test/require_test.rb
186
188
  - test/second_level_cache_test.rb
189
+ - test/single_table_inheritance_test.rb
187
190
  - test/test_helper.rb
@@ -1,35 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module SecondLevelCache
3
- module Arel
4
- class Wheres
5
- attr_reader :where_values
6
-
7
- def initialize(where_values)
8
- @where_values = where_values
9
- end
10
-
11
- # Determine whether all conditions is equality, for example:
12
- #
13
- # Article.where("user_id = 1").where(:status => 1).find(1)
14
- def all_equality?
15
- where_values.all?{|where_value| where_value.is_a?(::Arel::Nodes::Equality)}
16
- end
17
-
18
- # Extract conditions to pairs, for checking whether cache match the conditions.
19
- def extract_pairs
20
- where_values.map do |where_value|
21
- if where_value.is_a?(String)
22
- left, right = where_value.split(/\s*=\s*/, 2)
23
- right = right.to_i
24
- else
25
- left, right = where_value.left.name, where_value.right
26
- end
27
- {
28
- :left => left,
29
- :right => right
30
- }
31
- end
32
- end
33
- end
34
- end
35
- end