second_level_cache 2.1.8 → 2.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -9
- data/lib/second_level_cache/active_record/has_one_association.rb +2 -2
- data/lib/second_level_cache/version.rb +1 -1
- data/test/has_one_association_test.rb +10 -0
- data/test/model/user.rb +15 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6a4e2dad0ed044548798d38f155a9c9f153408e
|
4
|
+
data.tar.gz: 4ffb77cc2f17e40d53f3943686f87427c60cd626
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b60fb3e4e71c77541c568c9b6b29c1840bfe4838b7ef50c7c754b78464ddd41ba2133a80964590d2f259019add8bca1bf5d221d14f6473f3fe69893d27ed92f
|
7
|
+
data.tar.gz: de72573534de9d3acd65d5716adfce3e8a26885bd9c94e9808bca70bc371efadbe3c9f46d38c2e0aea6e16d5afbcdb6e9fd9852d47b714e983cdbc491fec4361
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# SecondLevelCache
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/second_level_cache.
|
4
|
-
[![Dependency Status](https://gemnasium.com/hooopo/second_level_cache.
|
5
|
-
[![Build Status](https://travis-ci.org/hooopo/second_level_cache.
|
6
|
-
[![Code Climate](https://codeclimate.com/github/hooopo/second_level_cache.
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/second_level_cache.svg)](http://badge.fury.io/rb/second_level_cache)
|
4
|
+
[![Dependency Status](https://gemnasium.com/hooopo/second_level_cache.svg)](https://gemnasium.com/hooopo/second_level_cache)
|
5
|
+
[![Build Status](https://travis-ci.org/hooopo/second_level_cache.svg?branch=master)](https://travis-ci.org/hooopo/second_level_cache)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/hooopo/second_level_cache.svg)](https://codeclimate.com/github/hooopo/second_level_cache)
|
7
7
|
|
8
8
|
SecondLevelCache is a write-through and read-through caching library inspired by Cache Money and cache_fu, support ActiveRecord 4.
|
9
9
|
|
@@ -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.
|
20
|
+
gem "second_level_cache", "~> 2.1.9"
|
21
21
|
```
|
22
22
|
|
23
23
|
For ActiveRecord 3:
|
@@ -91,13 +91,13 @@ ActiveRecord::Base.transaction do
|
|
91
91
|
user.save
|
92
92
|
account.save
|
93
93
|
Rails.logger.info "info"
|
94
|
-
end # <- Cache write
|
94
|
+
end # <- Cache write
|
95
95
|
|
96
96
|
# if you want to do something after user and account's write_second_level_cache operation, do this way:
|
97
97
|
ActiveRecord::Base.transaction do
|
98
98
|
user.save
|
99
99
|
account.save
|
100
|
-
end # <- Cache write
|
100
|
+
end # <- Cache write
|
101
101
|
Rails.logger.info "info"
|
102
102
|
```
|
103
103
|
|
@@ -114,7 +114,7 @@ In production env, we recommend to use [Dalli](https://github.com/mperham/dalli)
|
|
114
114
|
config.cache_store = [:dalli_store, APP_CONFIG["memcached_host"], {:namespace => "ns", :compress => true}]
|
115
115
|
```
|
116
116
|
|
117
|
-
## Tips:
|
117
|
+
## Tips:
|
118
118
|
|
119
119
|
* When you want to clear only second level cache apart from other cache for example fragment cache in cache store,
|
120
120
|
you can only change the `cache_key_prefix`:
|
@@ -130,7 +130,7 @@ class User < ActiveRecord::Base
|
|
130
130
|
end
|
131
131
|
```
|
132
132
|
|
133
|
-
* It provides a great feature, not hits db when fetching record via unique key(not primary key).
|
133
|
+
* It provides a great feature, not hits db when fetching record via unique key(not primary key).
|
134
134
|
|
135
135
|
```ruby
|
136
136
|
# this will fetch from cache
|
@@ -12,8 +12,8 @@ module SecondLevelCache
|
|
12
12
|
|
13
13
|
def find_target_with_second_level_cache
|
14
14
|
return find_target_without_second_level_cache unless klass.second_level_cache_enabled?
|
15
|
-
return find_target_without_second_level_cache if reflection.options[:through]
|
16
|
-
# TODO: implement cache with has_one through
|
15
|
+
return find_target_without_second_level_cache if reflection.options[:through] || reflection.scope
|
16
|
+
# TODO: implement cache with has_one through, scope
|
17
17
|
if reflection.options[:as]
|
18
18
|
cache_record = klass.fetch_by_uniq_keys({reflection.foreign_key => owner[reflection.active_record_primary_key], reflection.type => owner.class.base_class.name})
|
19
19
|
else
|
@@ -24,4 +24,14 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
24
24
|
# clean_user.forked_from_user
|
25
25
|
# end
|
26
26
|
end
|
27
|
+
|
28
|
+
def test_has_one_with_conditions
|
29
|
+
user = User.create name: 'hooopo', email: 'hoooopo@gmail.com'
|
30
|
+
group_namespace1 = Namespace.create(user_id: user.id, name: 'ruby-china', kind: 'group')
|
31
|
+
user.create_namespace(name: 'hooopo')
|
32
|
+
group_namespace2 = Namespace.create(user_id: user.id, name: 'rails', kind: 'group')
|
33
|
+
assert_not_equal user.namespace, nil
|
34
|
+
clear_user = User.find(user.id)
|
35
|
+
assert_equal clear_user.namespace.name, 'hooopo'
|
36
|
+
end
|
27
37
|
end
|
data/test/model/user.rb
CHANGED
@@ -15,6 +15,13 @@ ActiveRecord::Base.connection.create_table(:forked_user_links, :force => true) d
|
|
15
15
|
t.timestamps null: false
|
16
16
|
end
|
17
17
|
|
18
|
+
ActiveRecord::Base.connection.create_table(:namespaces, :force => true) do |t|
|
19
|
+
t.integer :user_id
|
20
|
+
t.string :kind
|
21
|
+
t.string :name
|
22
|
+
t.timestamps null: false
|
23
|
+
end
|
24
|
+
|
18
25
|
class User < ActiveRecord::Base
|
19
26
|
CacheVersion = 3
|
20
27
|
serialize :options, Array
|
@@ -25,10 +32,18 @@ class User < ActiveRecord::Base
|
|
25
32
|
has_one :account
|
26
33
|
has_one :forked_user_link, foreign_key: 'forked_to_user_id'
|
27
34
|
has_one :forked_from_user, through: :forked_user_link
|
35
|
+
has_many :namespaces
|
36
|
+
has_one :namespace, -> { where(kind: nil) }
|
28
37
|
has_many :books
|
29
38
|
has_many :images, :as => :imagable
|
30
39
|
end
|
31
40
|
|
41
|
+
class Namespace < ActiveRecord::Base
|
42
|
+
acts_as_cached(:version => 1, :expires_in => 3.day)
|
43
|
+
|
44
|
+
belongs_to :user
|
45
|
+
end
|
46
|
+
|
32
47
|
class ForkedUserLink < ActiveRecord::Base
|
33
48
|
belongs_to :forked_from_user, class_name: User
|
34
49
|
belongs_to :forked_to_user, class_name: User
|
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.
|
4
|
+
version: 2.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hooopo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|