second_level_cache 2.1.16 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +4 -8
  3. data/Gemfile +1 -1
  4. data/README.md +14 -13
  5. data/Rakefile +2 -2
  6. data/lib/second_level_cache/active_record/base.rb +9 -13
  7. data/lib/second_level_cache/active_record/belongs_to_association.rb +8 -12
  8. data/lib/second_level_cache/active_record/core.rb +6 -10
  9. data/lib/second_level_cache/active_record/fetch_by_uniq_key.rb +29 -17
  10. data/lib/second_level_cache/active_record/finder_methods.rb +23 -48
  11. data/lib/second_level_cache/active_record/has_one_association.rb +18 -19
  12. data/lib/second_level_cache/active_record/persistence.rb +6 -17
  13. data/lib/second_level_cache/active_record/preloader.rb +16 -17
  14. data/lib/second_level_cache/active_record/railtie.rb +13 -10
  15. data/lib/second_level_cache/active_record.rb +5 -8
  16. data/lib/second_level_cache/config.rb +14 -15
  17. data/lib/second_level_cache/record_marshal.rb +20 -35
  18. data/lib/second_level_cache/version.rb +2 -2
  19. data/lib/second_level_cache.rb +18 -24
  20. data/second_level_cache.gemspec +19 -18
  21. data/test/active_record_test_case_helper.rb +15 -12
  22. data/test/base_test.rb +2 -3
  23. data/test/belongs_to_association_test.rb +2 -3
  24. data/test/fetch_by_uniq_key_test.rb +14 -15
  25. data/test/finder_methods_test.rb +5 -44
  26. data/test/has_one_association_test.rb +5 -10
  27. data/test/model/account.rb +1 -2
  28. data/test/model/animal.rb +0 -1
  29. data/test/model/book.rb +4 -5
  30. data/test/model/image.rb +2 -4
  31. data/test/model/post.rb +3 -4
  32. data/test/model/topic.rb +2 -3
  33. data/test/model/user.rb +11 -14
  34. data/test/persistence_test.rb +3 -4
  35. data/test/polymorphic_association_test.rb +1 -2
  36. data/test/preloader_test.rb +2 -3
  37. data/test/record_marshal_test.rb +12 -15
  38. data/test/require_test.rb +2 -3
  39. data/test/second_level_cache_test.rb +2 -3
  40. data/test/single_table_inheritance_test.rb +0 -1
  41. data/test/test_helper.rb +13 -13
  42. metadata +49 -37
  43. data/lib/second_level_cache/active_record/multi_read_from_cache.rb +0 -21
  44. data/test/model/multi_read_from_cache_test.rb +0 -19
data/test/model/user.rb CHANGED
@@ -1,21 +1,20 @@
1
- # -*- encoding : utf-8 -*-
2
- ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
1
+ ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
3
2
  t.text :options
4
3
  t.text :json_options
5
- t.string :name, :unique => true
4
+ t.string :name, unique: true
6
5
  t.string :email
7
- t.integer :books_count, :default => 0
8
- t.integer :images_count, :default => 0
6
+ t.integer :books_count, default: 0
7
+ t.integer :images_count, default: 0
9
8
  t.timestamps null: false
10
9
  end
11
10
 
12
- ActiveRecord::Base.connection.create_table(:forked_user_links, :force => true) do |t|
11
+ ActiveRecord::Base.connection.create_table(:forked_user_links, force: true) do |t|
13
12
  t.integer :forked_to_user_id
14
13
  t.integer :forked_from_user_id
15
14
  t.timestamps null: false
16
15
  end
17
16
 
18
- ActiveRecord::Base.connection.create_table(:namespaces, :force => true) do |t|
17
+ ActiveRecord::Base.connection.create_table(:namespaces, force: true) do |t|
19
18
  t.integer :user_id
20
19
  t.string :kind
21
20
  t.string :name
@@ -23,23 +22,21 @@ ActiveRecord::Base.connection.create_table(:namespaces, :force => true) do |t|
23
22
  end
24
23
 
25
24
  class User < ActiveRecord::Base
26
- CacheVersion = 3
25
+ CACHE_VERSION = 3
27
26
  serialize :options, Array
28
- if ::ActiveRecord::VERSION::STRING >= '4.1.0'
29
- serialize :json_options, JSON
30
- end
31
- acts_as_cached(:version => CacheVersion, :expires_in => 3.day)
27
+ serialize :json_options, JSON if ::ActiveRecord::VERSION::STRING >= '4.1.0'
28
+ acts_as_cached(version: CACHE_VERSION, expires_in: 3.days)
32
29
  has_one :account
33
30
  has_one :forked_user_link, foreign_key: 'forked_to_user_id'
34
31
  has_one :forked_from_user, through: :forked_user_link
35
32
  has_many :namespaces
36
33
  has_one :namespace, -> { where(kind: nil) }
37
34
  has_many :books
38
- has_many :images, :as => :imagable
35
+ has_many :images, as: :imagable
39
36
  end
40
37
 
41
38
  class Namespace < ActiveRecord::Base
42
- acts_as_cached(:version => 1, :expires_in => 3.day)
39
+ acts_as_cached version: 1, expires_in: 3.days
43
40
 
44
41
  belongs_to :user
45
42
  end
@@ -1,10 +1,9 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class PersistenceTest < ActiveSupport::TestCase
5
4
  def setup
6
- @user = User.create :name => 'csdn', :email => 'test@csdn.com'
7
- @topic = Topic.create :title => "csdn"
5
+ @user = User.create name: 'csdn', email: 'test@csdn.com'
6
+ @topic = Topic.create title: 'csdn'
8
7
  end
9
8
 
10
9
  def test_should_reload_object
@@ -27,7 +26,7 @@ class PersistenceTest < ActiveSupport::TestCase
27
26
  end
28
27
 
29
28
  def test_should_update_cache_after_update_column
30
- @user.update_column :name, "new_name"
29
+ @user.update_column :name, 'new_name'
31
30
  new_user = User.find @user.id
32
31
  assert_equal new_user, @user
33
32
  end
@@ -1,9 +1,8 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class PolymorphicAssociationTest < ActiveSupport::TestCase
5
4
  def setup
6
- @user = User.create :name => 'csdn', :email => 'test@csdn.com'
5
+ @user = User.create name: 'csdn', email: 'test@csdn.com'
7
6
  end
8
7
 
9
8
  def test_should_get_cache_when_use_polymorphic_association
@@ -1,4 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class PreloaderTest < ActiveSupport::TestCase
@@ -17,7 +16,7 @@ class PreloaderTest < ActiveSupport::TestCase
17
16
  assert_equal topics, results.map(&:topic)
18
17
  end
19
18
 
20
- def test_belongs_to_when_read_multi_missed_from_cache_AR_will_fetch_missed_records_from_db
19
+ def test_belongs_to_when_read_multi_missed_from_cache_ar_will_fetch_missed_records_from_db
21
20
  topics = [
22
21
  Topic.create(title: 'title1', body: 'body1'),
23
22
  Topic.create(title: 'title2', body: 'body2'),
@@ -29,7 +28,7 @@ class PreloaderTest < ActiveSupport::TestCase
29
28
 
30
29
  results = nil
31
30
  assert_queries(2) do
32
- assert_sql(/IN\s+\(#{expired_topic.id}\)/m) do
31
+ assert_sql(/WHERE\s\"topics\"\.\"id\"\s=\s#{expired_topic.id}/m) do
33
32
  results = Post.includes(:topic).order('id ASC').to_a
34
33
  end
35
34
  end
@@ -1,45 +1,42 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class RecordMarshalTest < ActiveSupport::TestCase
5
4
  def setup
6
5
  if ::ActiveRecord::VERSION::STRING >= '4.1.0'
7
- @json_options = { "name" => 'Test', "age" => 18 }
8
- @user = User.create :name => 'csdn',
9
- :email => 'test@csdn.com',
10
- :options => [1,2],
11
- :json_options => @json_options
6
+ @json_options = { 'name' => 'Test', 'age' => 18 }
7
+ @user = User.create name: 'csdn',
8
+ email: 'test@csdn.com',
9
+ options: [1, 2],
10
+ json_options: @json_options
12
11
  else
13
- @user = User.create :name => 'csdn',
14
- :email => 'test@csdn.com',
15
- :options => [1,2]
12
+ @user = User.create name: 'csdn',
13
+ email: 'test@csdn.com',
14
+ options: [1, 2]
16
15
  end
17
16
  end
18
17
 
19
18
  def test_should_dump_active_record_object
20
19
  dumped = RecordMarshal.dump(@user)
21
20
  assert dumped.is_a?(Array)
22
- assert_equal "User", dumped[0]
21
+ assert_equal 'User', dumped[0]
23
22
  assert_equal @user.attributes, dumped[1]
24
23
  end
25
24
 
26
-
27
25
  def test_should_load_active_record_object
28
26
  @user.write_second_level_cache
29
27
  assert_equal @user, User.read_second_level_cache(@user.id)
30
28
  assert_equal Array, User.read_second_level_cache(@user.id).options.class
31
29
  assert_equal Array, User.read_second_level_cache(@user.id).reload.options.class
32
30
  assert_equal User.read_second_level_cache(@user.id).changed?, false
33
- assert_equal [1,2], User.read_second_level_cache(@user.id).options
31
+ assert_equal [1, 2], User.read_second_level_cache(@user.id).options
34
32
  if ::ActiveRecord::VERSION::STRING >= '4.1.0'
35
33
  result = User.read_second_level_cache(@user.id)
36
- assert_equal @json_options["name"], result.json_options["name"]
34
+ assert_equal @json_options['name'], result.json_options['name']
37
35
  assert_equal @json_options, result.json_options
38
36
  end
39
37
  assert User.read_second_level_cache(@user.id).persisted?
40
38
  end
41
39
 
42
-
43
40
  def test_should_load_nil
44
41
  @user.expire_second_level_cache
45
42
  assert_nil User.read_second_level_cache(@user.id)
@@ -48,6 +45,6 @@ class RecordMarshalTest < ActiveSupport::TestCase
48
45
  def test_should_load_active_record_object_without_association_cache
49
46
  @user.books
50
47
  @user.write_second_level_cache
51
- assert_empty User.read_second_level_cache(@user.id).association_cache
48
+ assert_equal false, User.read_second_level_cache(@user.id).association_cached?('id')
52
49
  end
53
50
  end
data/test/require_test.rb CHANGED
@@ -1,12 +1,11 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
  require 'active_record'
4
3
 
5
4
  class RequireTest < ActiveSupport::TestCase
6
5
  def setup
7
- ActiveRecord::Relation
6
+ ActiveRecord::Relation.new(nil, nil, nil)
8
7
  require 'test_helper'
9
- @user = User.create :name => 'Dingding Ye', :email => 'yedingding@gmail.com'
8
+ @user = User.create name: 'Dingding Ye', email: 'yedingding@gmail.com'
10
9
  end
11
10
 
12
11
  def test_should_find_the_user
@@ -1,13 +1,12 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class SecondLevelCacheTest < ActiveSupport::TestCase
5
4
  def setup
6
- @user = User.create :name => 'csdn', :email => 'test@csdn.com'
5
+ @user = User.create name: 'csdn', email: 'test@csdn.com'
7
6
  end
8
7
 
9
8
  def test_should_get_cache_key
10
- assert_equal "slc/users/#{@user.id}/#{User::CacheVersion}", @user.second_level_cache_key
9
+ assert_equal "slc/users/#{@user.id}/#{User::CACHE_VERSION}", @user.second_level_cache_key
11
10
  end
12
11
 
13
12
  def test_should_write_and_read_cache
@@ -1,4 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'test_helper'
3
2
 
4
3
  class SingleTableInheritanceTest < ActiveSupport::TestCase
data/test/test_helper.rb CHANGED
@@ -1,4 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'bundler/setup'
3
2
  require 'minitest/autorun'
4
3
  require 'active_support/test_case'
@@ -6,7 +5,6 @@ require 'active_record_test_case_helper'
6
5
  require 'database_cleaner'
7
6
  require 'active_record'
8
7
 
9
- ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=)
10
8
  ActiveSupport.test_order = :sorted if ActiveSupport.respond_to?(:test_order=)
11
9
 
12
10
  require 'second_level_cache'
@@ -28,16 +26,18 @@ SecondLevelCache.configure do |config|
28
26
  end
29
27
 
30
28
  SecondLevelCache.logger.level = Logger::ERROR
31
- ActiveSupport::Cache::MemoryStore.logger = SecondLevelCache::Config.logger
32
- ActiveRecord::Base.logger = SecondLevelCache::Config.logger
33
-
34
- class ActiveSupport::TestCase
35
- setup do
36
- SecondLevelCache.cache_store.clear
37
- DatabaseCleaner.start
38
- end
39
-
40
- teardown do
41
- DatabaseCleaner.clean
29
+ ActiveSupport::Cache::MemoryStore.logger = SecondLevelCache.logger
30
+ ActiveRecord::Base.logger = SecondLevelCache.logger
31
+
32
+ module ActiveSupport
33
+ class TestCase
34
+ setup do
35
+ SecondLevelCache.cache_store.clear
36
+ DatabaseCleaner.start
37
+ end
38
+
39
+ teardown do
40
+ DatabaseCleaner.clean
41
+ end
42
42
  end
43
43
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: second_level_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.16
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-18 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 5.0.0.beta1
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.0'
22
+ version: 5.1.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">"
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 4.0.0
29
+ version: 5.0.0.beta1
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.0'
32
+ version: 5.1.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activerecord
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 4.0.0
39
+ version: 5.0.0.beta1
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '5.0'
42
+ version: 5.1.0
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ">"
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 4.0.0
49
+ version: 5.0.0.beta1
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '5.0'
52
+ version: 5.1.0
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: sqlite3
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - "~>"
57
+ - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 1.3.6
59
+ version: '0'
60
60
  type: :development
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - "~>"
64
+ - - ">="
65
65
  - !ruby/object:Gem::Version
66
- version: 1.3.6
66
+ version: '0'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: rake
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +106,20 @@ dependencies:
106
106
  - - "~>"
107
107
  - !ruby/object:Gem::Version
108
108
  version: 1.3.0
109
+ - !ruby/object:Gem::Dependency
110
+ name: rubocop
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: 0.36.0
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: 0.36.0
109
123
  description: Write Through and Read Through caching library inspired by CacheMoney
110
124
  and cache_fu, support ActiveRecord 4.
111
125
  email:
@@ -126,7 +140,6 @@ files:
126
140
  - lib/second_level_cache/active_record/fetch_by_uniq_key.rb
127
141
  - lib/second_level_cache/active_record/finder_methods.rb
128
142
  - lib/second_level_cache/active_record/has_one_association.rb
129
- - lib/second_level_cache/active_record/multi_read_from_cache.rb
130
143
  - lib/second_level_cache/active_record/persistence.rb
131
144
  - lib/second_level_cache/active_record/preloader.rb
132
145
  - lib/second_level_cache/active_record/railtie.rb
@@ -144,7 +157,6 @@ files:
144
157
  - test/model/animal.rb
145
158
  - test/model/book.rb
146
159
  - test/model/image.rb
147
- - test/model/multi_read_from_cache_test.rb
148
160
  - test/model/post.rb
149
161
  - test/model/topic.rb
150
162
  - test/model/user.rb
@@ -174,7 +186,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
186
  - !ruby/object:Gem::Version
175
187
  version: '0'
176
188
  requirements: []
177
- rubygems_version: 3.0.3
189
+ rubyforge_project:
190
+ rubygems_version: 2.2.2
178
191
  signing_key:
179
192
  specification_version: 4
180
193
  summary: 'SecondLevelCache is a write-through and read-through caching library inspired
@@ -184,25 +197,24 @@ summary: 'SecondLevelCache is a write-through and read-through caching library i
184
197
  is a cache miss, it will populate the cache. Write-Through: As objects are created,
185
198
  updated, and deleted, all of the caches are automatically kept up-to-date and coherent.'
186
199
  test_files:
187
- - test/polymorphic_association_test.rb
188
- - test/require_test.rb
189
- - test/preloader_test.rb
190
- - test/finder_methods_test.rb
200
+ - test/active_record_test_case_helper.rb
201
+ - test/base_test.rb
191
202
  - test/belongs_to_association_test.rb
192
- - test/second_level_cache_test.rb
193
- - test/record_marshal_test.rb
194
- - test/persistence_test.rb
195
- - test/single_table_inheritance_test.rb
196
- - test/model/image.rb
203
+ - test/fetch_by_uniq_key_test.rb
204
+ - test/finder_methods_test.rb
205
+ - test/has_one_association_test.rb
197
206
  - test/model/account.rb
198
- - test/model/book.rb
199
- - test/model/topic.rb
200
207
  - test/model/animal.rb
201
- - test/model/multi_read_from_cache_test.rb
208
+ - test/model/book.rb
209
+ - test/model/image.rb
202
210
  - test/model/post.rb
211
+ - test/model/topic.rb
203
212
  - test/model/user.rb
204
- - test/fetch_by_uniq_key_test.rb
213
+ - test/persistence_test.rb
214
+ - test/polymorphic_association_test.rb
215
+ - test/preloader_test.rb
216
+ - test/record_marshal_test.rb
217
+ - test/require_test.rb
218
+ - test/second_level_cache_test.rb
219
+ - test/single_table_inheritance_test.rb
205
220
  - test/test_helper.rb
206
- - test/has_one_association_test.rb
207
- - test/base_test.rb
208
- - test/active_record_test_case_helper.rb
@@ -1,21 +0,0 @@
1
- module SecondLevelCache
2
- module ActiveRecord
3
- module MultiReadFromCache
4
- def multi_read_from_cache(ids)
5
- map_cache_keys = ids.map{|id| second_level_cache_key(id)}
6
- records_from_cache = ::SecondLevelCache.cache_store.read_multi(*map_cache_keys)
7
- hitted_ids = records_from_cache.map{|key, _| key.split("/")[2].to_i}
8
- missed_ids = ids.map{|x| x.to_i} - hitted_ids
9
-
10
- ::SecondLevelCache::Config.logger.info "missed ids -> #{missed_ids.inspect} | hitted ids -> #{hitted_ids.inspect}"
11
-
12
- if missed_ids.empty?
13
- RecordMarshal.load_multi(records_from_cache.values)
14
- else
15
- records_from_db = where(:id => missed_ids)
16
- records_from_db.map{|record| record.write_second_level_cache ; record} + RecordMarshal.load_multi(records_from_cache.values)
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,19 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'test_helper'
3
-
4
- class MultiReadFromCacheTest < ActiveSupport::TestCase
5
- def setup
6
- @user = User.create :name => 'hooopo', :email => 'hoooopo@gmail.com'
7
- @other_user = User.create :name => 'hoooopo', :email => "hooooopo@gmail.com"
8
- end
9
-
10
- def test_multi_read_from_cache
11
- result = User.multi_read_from_cache([@user.id, @other_user.id])
12
- assert_equal 2, result.size
13
- end
14
-
15
- def test_multi_read_not_exist_id_from_cache
16
- result = User.multi_read_from_cache([@user.id, @other_user.id + 100])
17
- assert_equal 1, result.size
18
- end
19
- end