second_level_cache 2.1.9 → 2.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6a4e2dad0ed044548798d38f155a9c9f153408e
4
- data.tar.gz: 4ffb77cc2f17e40d53f3943686f87427c60cd626
3
+ metadata.gz: 162eeb4c9ac6637270a5057e684b336573101aab
4
+ data.tar.gz: 87a40f18f2b5ff2e946fef7d9a63a7e060da7636
5
5
  SHA512:
6
- metadata.gz: 2b60fb3e4e71c77541c568c9b6b29c1840bfe4838b7ef50c7c754b78464ddd41ba2133a80964590d2f259019add8bca1bf5d221d14f6473f3fe69893d27ed92f
7
- data.tar.gz: de72573534de9d3acd65d5716adfce3e8a26885bd9c94e9808bca70bc371efadbe3c9f46d38c2e0aea6e16d5afbcdb6e9fd9852d47b714e983cdbc491fec4361
6
+ metadata.gz: 8086b2e29a478df84b0cc71313b9ae3a37afa4b0d76cb45ba8f82507867d3b24942d8ef82aeee480674010992029ac974b7114172171c2bfb994ad0d64aa9950
7
+ data.tar.gz: e42a65a6bd42be458c6b389a7b69b8389c667b9f174416a8da590fb7752ab376a457f218bfe60177ef18b2979249f4fe98d4acb62342ecd842b1c5eb5c1daaf9
@@ -1,3 +1,7 @@
1
+ 2.1.10
2
+ * read multi from cache for find(ids)
3
+
4
+ -----
1
5
  2.0.0.rc1
2
6
  -----
3
7
  * ActiveRecord 4 ready!
@@ -6,10 +6,9 @@ module SecondLevelCache
6
6
 
7
7
  included do
8
8
  alias_method_chain :find_one, :second_level_cache
9
+ alias_method_chain :find_some, :second_level_cache
9
10
  end
10
11
 
11
- # TODO find_some
12
- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L289-L309
13
12
  #
14
13
  # Cacheable:
15
14
  #
@@ -39,6 +38,52 @@ module SecondLevelCache
39
38
  record
40
39
  end
41
40
 
41
+ def find_some_with_second_level_cache(ids)
42
+ return find_some_without_second_level_cache(id) unless second_level_cache_enabled?
43
+ return find_some_without_second_level_cache(id) unless select_all_column?
44
+
45
+ if cachable?
46
+ result = multi_read_from_cache(ids)
47
+ else
48
+ result = where(:id => ids).all
49
+ end
50
+
51
+ expected_size =
52
+ if limit_value && ids.size > limit_value
53
+ limit_value
54
+ else
55
+ ids.size
56
+ end
57
+
58
+ # 11 ids with limit 3, offset 9 should give 2 results.
59
+ if offset_value && (ids.size - offset_value < expected_size)
60
+ expected_size = ids.size - offset_value
61
+ end
62
+
63
+ if result.size == expected_size
64
+ result
65
+ else
66
+ raise_record_not_found_exception!(ids, result.size, expected_size)
67
+ end
68
+ end
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
+
42
87
  private
43
88
 
44
89
  def cachable?
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "2.1.9"
3
+ VERSION = "2.1.10"
4
4
  end
@@ -4,6 +4,7 @@ require 'test_helper'
4
4
  class FinderMethodsTest < ActiveSupport::TestCase
5
5
  def setup
6
6
  @user = User.create :name => 'csdn', :email => 'test@csdn.com'
7
+ @other_user = User.create :name => 'shopper+', :email => 'test@shopperplus.com'
7
8
  end
8
9
 
9
10
  def test_should_find_without_cache
@@ -41,4 +42,34 @@ class FinderMethodsTest < ActiveSupport::TestCase
41
42
  end
42
43
  refute_equal @user.name, @from_db.name
43
44
  end
45
+
46
+ def test_find_some_record
47
+ @users = User.find(@user.id, @other_user.id)
48
+ assert_equal 2, @users.size
49
+ end
50
+
51
+ def test_missing_id_will_raise_for_find_some
52
+ assert_raises(ActiveRecord::RecordNotFound) do
53
+ @users = User.find(@user.id, User.last.id + 10000)
54
+ end
55
+ end
56
+
57
+ def test_filter_works_fine_for_find_some
58
+ assert_raises(ActiveRecord::RecordNotFound) do
59
+ @users = User.where("name is null").find(@user.id, @other_user.id)
60
+ end
61
+ end
62
+
63
+ def test_half_in_cache_for_find_some
64
+ @user.expire_second_level_cache
65
+ @users = User.find(@user.id, @other_user.id)
66
+ assert_equal 2, @users.size
67
+ end
68
+
69
+ def test_no_record_in_cache_for_find_some
70
+ @user.expire_second_level_cache
71
+ @other_user.expire_second_level_cache
72
+ @users = User.find(@user.id, @other_user.id)
73
+ assert_equal 2, @users.size
74
+ end
44
75
  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.9
4
+ version: 2.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-16 00:00:00.000000000 Z
11
+ date: 2016-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  version: '0'
174
174
  requirements: []
175
175
  rubyforge_project:
176
- rubygems_version: 2.2.2
176
+ rubygems_version: 2.5.1
177
177
  signing_key:
178
178
  specification_version: 4
179
179
  summary: 'SecondLevelCache is a write-through and read-through caching library inspired