second_level_cache 1.6.0 → 1.6.1
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 +7 -0
- data/CHANGELOG.md +4 -0
- data/README.md +97 -20
- data/lib/second_level_cache/active_record.rb +1 -1
- data/lib/second_level_cache/version.rb +1 -1
- data/lib/second_level_cache.rb +1 -1
- data/second_level_cache.gemspec +12 -3
- data/test/require_test.rb +14 -0
- metadata +36 -31
- data/.gitignore +0 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e4f99ac985dd67cff8d2f43791e8c10d3b3dd16c
|
4
|
+
data.tar.gz: 01ba1af838f68cb8b0013668e17a42a8d1c8801e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ae1f44bcc04c99d7c7a356c165eba029ffa8039a9e02270a5ef28132e66411c2fe5e5ac16d707c90831716e358a180d6d50f87032109c33c19197d3c035bbd5
|
7
|
+
data.tar.gz: 8276594d2e921ab5aad1d38b254e3ab1f0d6b21c5839c5f6c4c694a914baf1942704c5415330044122534ee603377b4ed6de5fa57b76a9492d6e51a8aaf5ce4c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
# SecondLevelCache
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/second_level_cache)
|
4
|
+
[](https://gemnasium.com/csdn-dev/second_level_cache)
|
5
|
+
[](https://travis-ci.org/csdn-dev/second_level_cache)
|
6
|
+
[](https://codeclimate.com/github/csdn-dev/second_level_cache)
|
7
|
+
|
3
8
|
SecondLevelCache is a write-through and read-through caching library inspired by Cache Money and cache_fu, support only Rails3 and ActiveRecord.
|
4
9
|
|
5
10
|
Read-Through: Queries by ID, like `current_user.articles.find(params[:id])`, will first look in cache store and then look in the database for the results of that query. If there is a cache miss, it will populate the cache.
|
6
11
|
|
7
12
|
Write-Through: As objects are created, updated, and deleted, all of the caches are automatically kept up-to-date and coherent.
|
8
13
|
|
9
|
-
## Risk
|
10
|
-
|
11
|
-
SecondLevelCache is not fully test and verify in production enviroment right now. Use it at your own risk.
|
12
14
|
|
13
15
|
## Install
|
14
16
|
|
@@ -49,40 +51,115 @@ user = User.find 1
|
|
49
51
|
user.second_level_cache_key # We will get the key looks like "slc/user/1/0"
|
50
52
|
```
|
51
53
|
|
54
|
+
Expires cache:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
user = User.find(1)
|
58
|
+
user.expire_second_level_cache
|
59
|
+
```
|
60
|
+
or expires cache using class method:
|
61
|
+
```ruby
|
62
|
+
User.expire_second_level_cache(1)
|
63
|
+
```
|
64
|
+
|
52
65
|
Disable SecondLevelCache:
|
53
66
|
|
54
67
|
```ruby
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
68
|
+
User.without_second_level_cache do
|
69
|
+
user = User.find 1
|
70
|
+
# ...
|
71
|
+
end
|
59
72
|
```
|
60
73
|
|
61
74
|
Only `SELECT *` query will be cached:
|
62
75
|
|
63
76
|
```ruby
|
64
|
-
|
65
|
-
|
77
|
+
# this query will NOT be cached
|
78
|
+
User.select("id, name").find(1)
|
66
79
|
```
|
67
80
|
|
68
81
|
Notice:
|
69
82
|
|
70
83
|
* SecondLevelCache cache by model name and id, so only find_one query will work.
|
71
|
-
*
|
84
|
+
* Only equal conditions query WILL get cache; and SQL string query like `User.where("name = 'Hooopo'").find(1)` WILL NOT work.
|
85
|
+
* SecondLevelCache sync cache after transaction commit:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# user and account's write_second_level_cache operation will invoke after the logger.
|
89
|
+
ActiveRecord::Base.transaction do
|
90
|
+
user.save
|
91
|
+
account.save
|
92
|
+
Rails.logger.info "info"
|
93
|
+
end # <- Cache write
|
94
|
+
|
95
|
+
# if you want to do something after user and account's write_second_level_cache operation, do this way:
|
96
|
+
ActiveRecord::Base.transaction do
|
97
|
+
user.save
|
98
|
+
account.save
|
99
|
+
end # <- Cache write
|
100
|
+
Rails.logger.info "info"
|
101
|
+
```
|
102
|
+
|
103
|
+
## Configure
|
104
|
+
|
105
|
+
In production env, we recommend to use [Dalli](https://github.com/mperham/dalli) as Rails cache store.
|
106
|
+
```ruby
|
107
|
+
config.cache_store = [:dalli_store, APP_CONFIG["memcached_host"], {:namespace => "ns", :compress => true}]
|
108
|
+
```
|
72
109
|
|
73
|
-
##
|
110
|
+
## Tips:
|
74
111
|
|
75
|
-
|
76
|
-
|
77
|
-
cache_key_prefix: Avoid cache key conflict with other application, Default is 'slc'
|
112
|
+
* When you want to clear only second level cache apart from other cache for example fragment cache in cache store,
|
113
|
+
you can only change the `cache_key_prefix`:
|
78
114
|
|
79
|
-
|
115
|
+
```ruby
|
116
|
+
SecondLevelCache.configure.cache_key_prefix = "slc1"
|
117
|
+
```
|
118
|
+
* When schema of your model changed, just change the `version` of the speical model, avoding clear all the cache.
|
80
119
|
|
81
120
|
```ruby
|
82
|
-
|
83
|
-
|
84
|
-
config.cache_store = ActiveSupport::Cache::MemoryStore.new
|
85
|
-
config.logger = Logger.new($stdout)
|
86
|
-
config.cache_key_prefix = 'domain'
|
121
|
+
class User < ActiveRecord::Base
|
122
|
+
acts_as_cached(:version => 2, :expires_in => 1.week)
|
87
123
|
end
|
88
124
|
```
|
125
|
+
|
126
|
+
* It provides a great feature, not hits db when fetching record via unique key(not primary key).
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
# this will fetch from cache
|
130
|
+
user = User.fetch_by_uniq_key("hooopo", :nick_name)
|
131
|
+
|
132
|
+
# this also fetch from cache
|
133
|
+
user = User.fetch_by_uniq_key!("hooopo", :nick_name) # this will raise `ActiveRecord::RecordNotFound` Exception when nick name not exists.
|
134
|
+
```
|
135
|
+
|
136
|
+
## Contributors
|
137
|
+
|
138
|
+
* [chloerei](https://github.com/chloerei)
|
139
|
+
* [reyesyang](https://github.com/reyesyang)
|
140
|
+
* [hooopo](https://github.com/hooopo)
|
141
|
+
* [sishen](https://github.com/sishen)
|
142
|
+
|
143
|
+
## License
|
144
|
+
|
145
|
+
MIT License
|
146
|
+
|
147
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
148
|
+
a copy of this software and associated documentation files (the
|
149
|
+
"Software"), to deal in the Software without restriction, including
|
150
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
151
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
152
|
+
permit persons to whom the Software is furnished to do so, subject to
|
153
|
+
the following conditions:
|
154
|
+
|
155
|
+
The above copyright notice and this permission notice shall be
|
156
|
+
included in all copies or substantial portions of the Software.
|
157
|
+
|
158
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
159
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
160
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
161
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
162
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
163
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
164
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
165
|
+
|
@@ -9,7 +9,7 @@ require 'second_level_cache/active_record/has_one_association'
|
|
9
9
|
ActiveRecord::Base.send(:include, SecondLevelCache::Mixin)
|
10
10
|
ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Base)
|
11
11
|
ActiveRecord::Base.send(:extend, SecondLevelCache::ActiveRecord::FetchByUniqKey)
|
12
|
-
ActiveRecord::
|
12
|
+
ActiveRecord::Relation.send(:include, SecondLevelCache::ActiveRecord::FinderMethods)
|
13
13
|
ActiveRecord::Base.send(:include, SecondLevelCache::ActiveRecord::Persistence)
|
14
14
|
ActiveRecord::Associations::BelongsToAssociation.send(:include, SecondLevelCache::ActiveRecord::Associations::BelongsToAssociation)
|
15
15
|
ActiveRecord::Associations::HasOneAssociation.send(:include, SecondLevelCache::ActiveRecord::Associations::HasOneAssociation)
|
data/lib/second_level_cache.rb
CHANGED
@@ -21,7 +21,7 @@ module SecondLevelCache
|
|
21
21
|
def acts_as_cached(options = {})
|
22
22
|
@second_level_cache_enabled = true
|
23
23
|
@second_level_cache_options = options
|
24
|
-
@second_level_cache_options[:expires_in] ||= 1.
|
24
|
+
@second_level_cache_options[:expires_in] ||= 1.week
|
25
25
|
@second_level_cache_options[:version] ||= 0
|
26
26
|
end
|
27
27
|
|
data/second_level_cache.gemspec
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require File.expand_path('../lib/second_level_cache/version', __FILE__)
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
5
|
|
4
6
|
Gem::Specification.new do |gem|
|
5
7
|
gem.authors = ["wangxz"]
|
@@ -15,9 +17,16 @@ Gem::Specification.new do |gem|
|
|
15
17
|
|
16
18
|
gem.homepage = "https://github.com/csdn-dev/second_level_cache"
|
17
19
|
|
18
|
-
gem.
|
19
|
-
|
20
|
-
|
20
|
+
gem.files = Dir.glob("lib/**/*.rb") + [
|
21
|
+
"README.md",
|
22
|
+
"Rakefile",
|
23
|
+
"Gemfile",
|
24
|
+
"init.rb",
|
25
|
+
"CHANGELOG.md",
|
26
|
+
"second_level_cache.gemspec"
|
27
|
+
]
|
28
|
+
gem.test_files = Dir.glob("test/**/*.rb")
|
29
|
+
gem.executables = gem.files.grep(%r{^bin/})
|
21
30
|
gem.name = "second_level_cache"
|
22
31
|
gem.require_paths = ["lib"]
|
23
32
|
gem.version = SecondLevelCache::VERSION
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
class RequireTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
ActiveRecord::Relation
|
7
|
+
require 'active_record/test_helper'
|
8
|
+
@user = User.create :name => 'Dingding Ye', :email => 'yedingding@gmail.com'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_find_the_user
|
12
|
+
assert_equal @user, User.find(@user.id)
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: second_level_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
5
|
-
prerelease:
|
4
|
+
version: 1.6.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- wangxz
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-03-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: activerecord
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,33 +41,29 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: sqlite3
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rake
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
description: Write Through and Read Through caching library inspired by CacheMoney
|
@@ -83,24 +74,23 @@ executables: []
|
|
83
74
|
extensions: []
|
84
75
|
extra_rdoc_files: []
|
85
76
|
files:
|
86
|
-
- .gitignore
|
87
|
-
- CHANGELOG.md
|
88
|
-
- Gemfile
|
89
|
-
- README.md
|
90
|
-
- Rakefile
|
91
|
-
- init.rb
|
92
|
-
- lib/second_level_cache.rb
|
93
|
-
- lib/second_level_cache/active_record.rb
|
94
77
|
- lib/second_level_cache/active_record/base.rb
|
95
78
|
- lib/second_level_cache/active_record/belongs_to_association.rb
|
96
79
|
- lib/second_level_cache/active_record/fetch_by_uniq_key.rb
|
97
80
|
- lib/second_level_cache/active_record/finder_methods.rb
|
98
81
|
- lib/second_level_cache/active_record/has_one_association.rb
|
99
82
|
- lib/second_level_cache/active_record/persistence.rb
|
83
|
+
- lib/second_level_cache/active_record.rb
|
100
84
|
- lib/second_level_cache/arel/wheres.rb
|
101
85
|
- lib/second_level_cache/config.rb
|
102
86
|
- lib/second_level_cache/record_marshal.rb
|
103
87
|
- lib/second_level_cache/version.rb
|
88
|
+
- lib/second_level_cache.rb
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- Gemfile
|
92
|
+
- init.rb
|
93
|
+
- CHANGELOG.md
|
104
94
|
- second_level_cache.gemspec
|
105
95
|
- test/active_record/base_test.rb
|
106
96
|
- test/active_record/belongs_to_association_test.rb
|
@@ -115,34 +105,49 @@ files:
|
|
115
105
|
- test/active_record/second_level_cache_test.rb
|
116
106
|
- test/active_record/test_helper.rb
|
117
107
|
- test/record_marshal_test.rb
|
108
|
+
- test/require_test.rb
|
118
109
|
- test/test_helper.rb
|
119
110
|
homepage: https://github.com/csdn-dev/second_level_cache
|
120
111
|
licenses: []
|
112
|
+
metadata: {}
|
121
113
|
post_install_message:
|
122
114
|
rdoc_options: []
|
123
115
|
require_paths:
|
124
116
|
- lib
|
125
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
-
none: false
|
127
118
|
requirements:
|
128
|
-
- -
|
119
|
+
- - '>='
|
129
120
|
- !ruby/object:Gem::Version
|
130
121
|
version: '0'
|
131
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
-
none: false
|
133
123
|
requirements:
|
134
|
-
- -
|
124
|
+
- - '>='
|
135
125
|
- !ruby/object:Gem::Version
|
136
126
|
version: '0'
|
137
127
|
requirements: []
|
138
128
|
rubyforge_project:
|
139
|
-
rubygems_version:
|
129
|
+
rubygems_version: 2.0.0
|
140
130
|
signing_key:
|
141
|
-
specification_version:
|
142
|
-
summary:
|
131
|
+
specification_version: 4
|
132
|
+
summary: 'SecondLevelCache is a write-through and read-through caching library inspired
|
143
133
|
by Cache Money and cache_fu, support only Rails3 and ActiveRecord. Read-Through:
|
144
134
|
Queries by ID, like current_user.articles.find(params[:id]), will first look in
|
145
135
|
cache store and then look in the database for the results of that query. If there
|
146
136
|
is a cache miss, it will populate the cache. Write-Through: As objects are created,
|
147
137
|
updated, and deleted, all of the caches are automatically kept up-to-date and coherent.'
|
148
|
-
test_files:
|
138
|
+
test_files:
|
139
|
+
- test/active_record/base_test.rb
|
140
|
+
- test/active_record/belongs_to_association_test.rb
|
141
|
+
- test/active_record/finder_methods_test.rb
|
142
|
+
- test/active_record/model/book.rb
|
143
|
+
- test/active_record/model/image.rb
|
144
|
+
- test/active_record/model/post.rb
|
145
|
+
- test/active_record/model/topic.rb
|
146
|
+
- test/active_record/model/user.rb
|
147
|
+
- test/active_record/persistence_test.rb
|
148
|
+
- test/active_record/polymorphic_association_test.rb
|
149
|
+
- test/active_record/second_level_cache_test.rb
|
150
|
+
- test/active_record/test_helper.rb
|
151
|
+
- test/record_marshal_test.rb
|
152
|
+
- test/require_test.rb
|
153
|
+
- test/test_helper.rb
|