act_as_cached 0.0.1 → 0.0.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.
data/README.md
CHANGED
@@ -6,47 +6,103 @@
|
|
6
6
|
[](https://codeclimate.com/github/ddl1st/act_as_cached)
|
7
7
|
|
8
8
|
|
9
|
-
<tt>ActAsCached</tt> provides a lightweight
|
9
|
+
<tt>ActAsCached</tt> provides a lightweight second level cache.
|
10
|
+
|
11
|
+
## Supports
|
12
|
+
|
13
|
+
* ActiveRecord associations
|
14
|
+
* ActtiveRecord::FinderMethods (primary key only)
|
10
15
|
|
11
16
|
----------------------
|
12
17
|
|
13
18
|
## Install
|
14
19
|
|
15
|
-
gem "act_as_cached", "0.0.
|
20
|
+
gem "act_as_cached", "~> 0.0.2"
|
16
21
|
|
17
22
|
----------------------
|
18
23
|
|
19
24
|
### Options:
|
20
25
|
|
21
|
-
* <tt>:prefix</tt> - Default is
|
26
|
+
* <tt>:prefix</tt> - Default is `aac` .
|
22
27
|
* <tt>:cache_store</tt> - Customize cache store.
|
23
|
-
* <tt>:expires_time</tt> - Set cache expiration time.
|
24
|
-
* <tt>:logger</tt> - Set
|
28
|
+
* <tt>:expires_time</tt> - Set cache expiration time, default is one week.
|
29
|
+
* <tt>:logger</tt> - Set output file with log, default is `Rails.logger` .
|
25
30
|
|
26
31
|
----------------------
|
27
32
|
|
28
33
|
## Usage
|
29
34
|
|
30
35
|
```ruby
|
31
|
-
class Account < ActiveRecord::Base
|
32
|
-
act_as_cached
|
33
|
-
has_many :users
|
34
|
-
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
class Topic < ActiveRecord::Base
|
38
|
+
act_as_cached expires_time: 1.day
|
39
|
+
has_many :comments
|
40
|
+
belongs_to :user
|
41
|
+
end
|
42
|
+
|
43
|
+
class Comment < ActiveRecord::Base
|
44
|
+
act_as_cached expires_time: 2.days
|
45
|
+
belongs_to :topic
|
46
|
+
belongs_to :user
|
47
|
+
end
|
48
|
+
|
49
|
+
Topic.find(:first).comments.find(1).user
|
50
|
+
# Topic Load (0.4ms) SELECT `topics`.* FROM `topics` LIMIT 1
|
51
|
+
# Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`topic_id` = 1 AND `topics`.`id` = 1 LIMIT 1
|
52
|
+
# User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
|
53
|
+
|
54
|
+
=> #<User id: 1 ,name: 'foo'>
|
55
|
+
|
56
|
+
Topic.find(:first)
|
57
|
+
=> #<Topic id: 1 ,title: 'foo'>
|
58
|
+
Comment.find(1)
|
59
|
+
=> #<Comment id: 1, content: "xxxx">
|
60
|
+
Comment.find(1).user
|
61
|
+
=> #<User id: 1, name: 'foo'>
|
62
|
+
|
63
|
+
Topic.find(:first).comments
|
64
|
+
# Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`topic_id` = 1 AND `topics`.`id` = 1 LIMIT 1
|
65
|
+
=> [#<Comment id: 1, content: 'xxxx'>,#<Comment id: 2, content: "ooooooo">]
|
66
|
+
|
67
|
+
Topic.find(:first).comments
|
68
|
+
=> [#<Comment id: 1, content: 'xxxx'>,#<Comment id: 2, content: "ooooooo">]
|
69
|
+
|
70
|
+
Topic.find(1)
|
71
|
+
# Topic Load (0.4ms) SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1
|
72
|
+
=> #<Topic id: 1 ,title: 'foo'>
|
73
|
+
|
74
|
+
|
75
|
+
Topic.find(1).save # After you changes the object which was cached yet expired, the cache of the very object will be destroyed but not its associations.
|
76
|
+
|
77
|
+
Topic.find(1)
|
78
|
+
# Topic Load (0.5ms) SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1
|
79
|
+
=> #<Topic id: 1 title: 'foo'>
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
----------------------
|
84
|
+
|
85
|
+
## Configure
|
86
|
+
|
87
|
+
Global Settings
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
|
91
|
+
ActAsCached.configure do
|
92
|
+
config.logger = ::Rails.logger
|
93
|
+
config.cache_store = ::Rails.cache
|
94
|
+
config.expires_time = 1.week
|
95
|
+
config.prefix = 'aac'
|
96
|
+
end
|
38
97
|
|
39
|
-
Account.find(1).users # Account Load (0.5ms) SELECT `users`.* FROM `users` INNER JOIN `accounts_users` ON `users`.`id` = `accounts_users`.`user_id` WHERE `accounts_users`.`account_id` = 1
|
40
|
-
Account.find(1).users # NO SQL OUTPUT
|
41
|
-
|
42
98
|
```
|
43
99
|
|
44
100
|
----------------------
|
45
101
|
|
46
102
|
|
47
|
-
##
|
103
|
+
## Tips
|
48
104
|
|
49
|
-
* Currently not
|
105
|
+
* Currently not support <tt>ActiveRecord::Relation#find</tt> , If you wrote the `default_scope` in your class, cache won't be enable.
|
50
106
|
|
51
107
|
----------------------
|
52
108
|
|
@@ -54,6 +110,7 @@
|
|
54
110
|
|
55
111
|
* Expires cache
|
56
112
|
* Add cache key rules
|
113
|
+
* Add test
|
57
114
|
|
58
115
|
----------------------
|
59
116
|
|
@@ -15,7 +15,7 @@ module ActAsCached
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def match_keys(object)
|
18
|
-
["all","first","last",nil].collect{|x| [object.cache_name,x].compact.join("/")}.join("|") << "$"
|
18
|
+
["all","first","last",object.to_param,nil].collect{|x| [object.cache_name,x].compact.join("/")}.join("|") << "$"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/act_as_cached/mixin.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: act_as_cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -52,7 +52,6 @@ extra_rdoc_files: []
|
|
52
52
|
files:
|
53
53
|
- lib/act_as_cached/active_record/actobserver.rb
|
54
54
|
- lib/act_as_cached/active_record/association.rb
|
55
|
-
- lib/act_as_cached/active_record/association_reflection.rb
|
56
55
|
- lib/act_as_cached/active_record/collection_association.rb
|
57
56
|
- lib/act_as_cached/active_record/finder_methods.rb
|
58
57
|
- lib/act_as_cached/active_record/has_many_through_association.rb
|