second_level_cache 2.2.1 → 2.2.2

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: 08afc3105b0fd6181f8c5fa0deeed4c6af159e48
4
- data.tar.gz: 591668829c848cd26c9c7ca7ecb7edda0a5378ad
3
+ metadata.gz: 88a2a566c9af218fbcb4ba47cde3b0dddfcedc2b
4
+ data.tar.gz: 36e45a2c4efa464094022cab997b0ece390d164b
5
5
  SHA512:
6
- metadata.gz: c913f07951f4c923a33580ea6576e03ba4cc30ade72ac4451adaf1b82690e3ffd3f1879ac6fd3f645ea5ec6142b3ec9e52992ecabfb650a89f76e609116cd323
7
- data.tar.gz: cbf1edab3ddabb33d5f3a12f234ee29657d232985df41018aaecbcd2aeb614be09536b5e93c6f8ba0fbd16928cadcbaa2d771c7680411ef7e8c904ddf7f2aa3d
6
+ metadata.gz: 2e501d209a3b2f6d9790fc813a558cea3a944457fdbda0b0d657835e30193a4592585e0a22b8f9e957298cf381e3bc902832ee11eecc69e323432d26ff934597
7
+ data.tar.gz: 0051c2f3c1e1110dc7088ffb2b1451e095a7872291e8efc04e42ecf68bb6fdcbfe7545e0e3d71379902495d488b5f562871dde3c3f0a148600a24db25329ddf2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 2.2.2
2
+ -----
3
+
4
+ * Add `where(id: n).first`, `where(id: n).last` hit cache support. This improve will avoid some gems query database, for example: [devise](https://github.com/plataformatec/devise) `current_user` method.
5
+
1
6
  2.2.1
2
7
  -----
3
8
 
data/README.md CHANGED
@@ -50,6 +50,7 @@ Then it will fetch cached object in this situations:
50
50
  User.find(1)
51
51
  user.articles.find(1)
52
52
  User.where(status: 1).find(1)
53
+ User.where(id: 1).first # or .last
53
54
  article.user
54
55
  ```
55
56
 
@@ -2,7 +2,7 @@ module SecondLevelCache
2
2
  module ActiveRecord
3
3
  module FinderMethods
4
4
  # TODO: find_some
5
- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L289-L309
5
+ # http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_one
6
6
  #
7
7
  # Cacheable:
8
8
  #
@@ -34,6 +34,31 @@ module SecondLevelCache
34
34
  record
35
35
  end
36
36
 
37
+ # http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-first
38
+ #
39
+ # Cacheable:
40
+ #
41
+ # User.where(id: 1).first
42
+ # User.where(id: 1).last
43
+ #
44
+ # Uncacheable:
45
+ #
46
+ # User.where(name: 'foo').first
47
+ # User.where(age: 18).first
48
+ #
49
+ def first(limit = nil)
50
+ return super(limit) if limit.to_i > 1
51
+ # only have primary_key condition in where
52
+ if where_values_hash.length == 1 && where_values_hash.key?(primary_key)
53
+ record = @klass.read_second_level_cache(where_values_hash[primary_key])
54
+ return record if record
55
+ end
56
+
57
+ record = super(limit)
58
+ record.write_second_level_cache if record
59
+ record
60
+ end
61
+
37
62
  private
38
63
 
39
64
  def cachable?
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module SecondLevelCache
3
- VERSION = '2.2.1'.freeze
3
+ VERSION = '2.2.2'.freeze
4
4
  end
@@ -40,4 +40,18 @@ class FinderMethodsTest < ActiveSupport::TestCase
40
40
  end
41
41
  refute_equal @user.name, @from_db.name
42
42
  end
43
+
44
+ def test_where_and_first_should_with_cache
45
+ @user.write_second_level_cache
46
+ assert_no_queries do
47
+ assert_equal @user, User.where(id: @user.id).first
48
+ end
49
+ end
50
+
51
+ def test_where_and_last_should_with_cache
52
+ @user.write_second_level_cache
53
+ assert_no_queries do
54
+ assert_equal @user, User.where(id: @user.id).last
55
+ end
56
+ end
43
57
  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.2.1
4
+ version: 2.2.2
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-17 00:00:00.000000000 Z
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  version: '0'
188
188
  requirements: []
189
189
  rubyforge_project:
190
- rubygems_version: 2.2.2
190
+ rubygems_version: 2.4.5.1
191
191
  signing_key:
192
192
  specification_version: 4
193
193
  summary: 'SecondLevelCache is a write-through and read-through caching library inspired