second_level_cache 2.1.10 → 2.1.13

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: 162eeb4c9ac6637270a5057e684b336573101aab
4
- data.tar.gz: 87a40f18f2b5ff2e946fef7d9a63a7e060da7636
3
+ metadata.gz: 87a71a85a2abc58df0d9cc05f15b2a1c36c92339
4
+ data.tar.gz: 152a0f37f8793d8bfbb699cdc2887e51860d1143
5
5
  SHA512:
6
- metadata.gz: 8086b2e29a478df84b0cc71313b9ae3a37afa4b0d76cb45ba8f82507867d3b24942d8ef82aeee480674010992029ac974b7114172171c2bfb994ad0d64aa9950
7
- data.tar.gz: e42a65a6bd42be458c6b389a7b69b8389c667b9f174416a8da590fb7752ab376a457f218bfe60177ef18b2979249f4fe98d4acb62342ecd842b1c5eb5c1daaf9
6
+ metadata.gz: 06a26ebf58a7f3f516c639a5f95abbb95a06dda1b24a8a686718b854f5383e53df75440ec23c2ea16cd5a94a6e7f606d05bfea24f9c547e5be1329efea77dcf9
7
+ data.tar.gz: aa21915860466947a13ab3c792b83a98e40accc785459ad303a9fc03060d4ba70c673554964512b4baa80de1cb6ae2a3ff3a8f56c33bdff92ee0b59398afdda9
data/README.md CHANGED
@@ -141,6 +141,13 @@ post = Post.fetch_by_uniq_keys(user_id: 2, slug: "foo")
141
141
  user = User.fetch_by_uniq_keys!(nick_name: "hooopo") # this will raise `ActiveRecord::RecordNotFound` Exception when nick name not exists.
142
142
  ```
143
143
 
144
+ * multi_read_from_cache
145
+
146
+ ```ruby
147
+ # this will use Rails.cache.multi_read method to fetch record, if miss, then use SQL in query.
148
+ blogs = Blog.multi_read_from_cache([1,2,3])
149
+ ```
150
+
144
151
  * You can use Rails's [Eager Loading](http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations) feature as normal. Even better, second_level_cache will transform the `IN` query into a Rails.cache.multi_read operation. For example:
145
152
 
146
153
  ```ruby
@@ -67,23 +67,6 @@ module SecondLevelCache
67
67
  end
68
68
  end
69
69
 
70
-
71
- def multi_read_from_cache(ids)
72
- map_cache_keys = ids.map{|id| klass.second_level_cache_key(id)}
73
- records_from_cache = ::SecondLevelCache.cache_store.read_multi(*map_cache_keys)
74
- hitted_ids = records_from_cache.map{|key, _| key.split("/")[2].to_i}
75
- missed_ids = ids.map{|x| x.to_i} - hitted_ids
76
-
77
- ::SecondLevelCache::Config.logger.info "missed ids -> #{missed_ids.inspect} | hitted ids -> #{hitted_ids.inspect}"
78
-
79
- if missed_ids.empty?
80
- RecordMarshal.load_multi(records_from_cache.values)
81
- else
82
- records_from_db = where(:id => missed_ids)
83
- records_from_db.map{|record| record.write_second_level_cache ; record} + RecordMarshal.load_multi(records_from_cache.values)
84
- end
85
- end
86
-
87
70
  private
88
71
 
89
72
  def cachable?
@@ -0,0 +1,21 @@
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
@@ -3,6 +3,7 @@ class SecondLevelCache::ActiveRecord::Railtie < Rails::Railtie
3
3
  ActiveRecord::Base.send(:include, SecondLevelCache::Mixin)
4
4
  ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Base)
5
5
  ActiveRecord::Base.send(:extend, SecondLevelCache::ActiveRecord::FetchByUniqKey)
6
+ ActiveRecord::Base.send(:extend, SecondLevelCache::ActiveRecord::MultiReadFromCache)
6
7
 
7
8
  ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Persistence)
8
9
  ActiveRecord::Associations::BelongsToAssociation.send(:include, SecondLevelCache::ActiveRecord::Associations::BelongsToAssociation)
@@ -7,6 +7,7 @@ require 'second_level_cache/active_record/persistence'
7
7
  require 'second_level_cache/active_record/belongs_to_association'
8
8
  require 'second_level_cache/active_record/has_one_association'
9
9
  require 'second_level_cache/active_record/preloader'
10
+ require 'second_level_cache/active_record/multi_read_from_cache'
10
11
 
11
12
  if defined? Rails
12
13
  require 'second_level_cache/active_record/railtie'
@@ -14,6 +15,7 @@ else
14
15
  ActiveRecord::Base.send(:include, SecondLevelCache::Mixin)
15
16
  ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Base)
16
17
  ActiveRecord::Base.send(:extend, SecondLevelCache::ActiveRecord::FetchByUniqKey)
18
+ ActiveRecord::Base.send(:extend, SecondLevelCache::ActiveRecord::MultiReadFromCache)
17
19
 
18
20
  ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Persistence)
19
21
  ActiveRecord::Associations::BelongsToAssociation.send(:include, SecondLevelCache::ActiveRecord::Associations::BelongsToAssociation)
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "2.1.10"
3
+ VERSION = "2.1.13"
4
4
  end
@@ -0,0 +1,19 @@
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
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.10
4
+ version: 2.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-06 00:00:00.000000000 Z
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -126,6 +126,7 @@ files:
126
126
  - lib/second_level_cache/active_record/fetch_by_uniq_key.rb
127
127
  - lib/second_level_cache/active_record/finder_methods.rb
128
128
  - lib/second_level_cache/active_record/has_one_association.rb
129
+ - lib/second_level_cache/active_record/multi_read_from_cache.rb
129
130
  - lib/second_level_cache/active_record/persistence.rb
130
131
  - lib/second_level_cache/active_record/preloader.rb
131
132
  - lib/second_level_cache/active_record/railtie.rb
@@ -143,6 +144,7 @@ files:
143
144
  - test/model/animal.rb
144
145
  - test/model/book.rb
145
146
  - test/model/image.rb
147
+ - test/model/multi_read_from_cache_test.rb
146
148
  - test/model/post.rb
147
149
  - test/model/topic.rb
148
150
  - test/model/user.rb
@@ -193,6 +195,7 @@ test_files:
193
195
  - test/model/animal.rb
194
196
  - test/model/book.rb
195
197
  - test/model/image.rb
198
+ - test/model/multi_read_from_cache_test.rb
196
199
  - test/model/post.rb
197
200
  - test/model/topic.rb
198
201
  - test/model/user.rb