cache_associations 0.2.0 → 0.2.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 +4 -4
- data/README.md +9 -0
- data/lib/cache_associations/version.rb +1 -1
- data/lib/cache_associations.rb +25 -6
- 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: 9e111469fc100cc1a801fb5e200b959e703f86c7
|
4
|
+
data.tar.gz: 016c2fb36fed32d91e3e5b9c8641b4026277dbb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ded7fbf3e5c2dc204894d75e7825f6025df635273d6fb1bf50a5ab4cf501b27d9f2da5d6861ba6bf8163f0fab06818831074d01caac4c102f9d1f356e378b74
|
7
|
+
data.tar.gz: 59a811bb5464a536916dec9bb40d3edb74ea34ca5f71581390332cdb52a5383957b0627cbe60790078557c2d93a17333c005474ca08f43317f8af6c0916fcd98
|
data/README.md
CHANGED
@@ -32,6 +32,11 @@ class User < ApplicationRecord
|
|
32
32
|
# put your cache name here
|
33
33
|
[self.class.name, id, 'profile', update_at.to_i]
|
34
34
|
end
|
35
|
+
|
36
|
+
def factorial_1000
|
37
|
+
(1..1000).inject(:*)
|
38
|
+
end
|
39
|
+
cache_method :factorial_1000 # use cached_factorial_1000
|
35
40
|
end
|
36
41
|
|
37
42
|
class Profile < ApplicationRecord
|
@@ -44,12 +49,16 @@ You may also specify additional options via the options argument, it's the same
|
|
44
49
|
such as setting `:expires_in` will set an expiration time on the cache.
|
45
50
|
And `cache_association` accepts an optional block to define the cache name, it's `[self.class.name, id, name, updated_at.to_i]` in default.
|
46
51
|
|
52
|
+
|
53
|
+
You can use `cache_method` to cache something else, like attributes and methods.
|
54
|
+
|
47
55
|
```ruby
|
48
56
|
irb> u = User.take
|
49
57
|
irb> u.cached_profile # fetch from the cache store
|
50
58
|
irb> u.profile # fetch the cached version
|
51
59
|
irb> u.profile.reload # refetch from the database
|
52
60
|
Profile Load (1.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
61
|
+
irb> u.clear_caching_on_association(:profile) # clear the cache
|
53
62
|
```
|
54
63
|
|
55
64
|
You may customize what the data you cached, the cached method accepts an optional block that determines how to cache data.
|
data/lib/cache_associations.rb
CHANGED
@@ -10,7 +10,7 @@ module CacheAssociations
|
|
10
10
|
unless reflection = reflect_on_association(name)
|
11
11
|
raise UndefinedAssociationError, "Undefined asscociation #{name}"
|
12
12
|
end
|
13
|
-
|
13
|
+
register_cache_name_block(name, cache_name_block)
|
14
14
|
options = Rails.cache.options.merge(options)
|
15
15
|
|
16
16
|
define_method("cached_#{name}") do |*args, &block|
|
@@ -34,23 +34,42 @@ module CacheAssociations
|
|
34
34
|
cache
|
35
35
|
end
|
36
36
|
end
|
37
|
+
|
38
|
+
def cache_method(name, **options, &cache_name_block)
|
39
|
+
register_cache_name_block(name, cache_name_block)
|
40
|
+
options = Rails.cache.options.merge(options)
|
41
|
+
|
42
|
+
define_method("cached_#{name}") do |*args, &block|
|
43
|
+
cache_name = cache_name_block.nil? ? default_cache_name(name) : instance_exec(&cache_name_block)
|
44
|
+
Rails.cache.fetch(cache_name, **options) do
|
45
|
+
break instance_exec(*args, &block) if !block.nil?
|
46
|
+
|
47
|
+
send("#{name}", *args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
37
51
|
|
38
52
|
def has_cache_association?(name)
|
39
|
-
|
53
|
+
cache_name_blocks.has_key?(name.to_sym)
|
40
54
|
end
|
41
55
|
|
42
56
|
def custom_cache_name?(name)
|
43
|
-
!!
|
57
|
+
!!cache_name_blocks[name.to_sym]
|
44
58
|
end
|
45
59
|
|
46
60
|
def cache_name_block(name)
|
47
|
-
|
61
|
+
cache_name_blocks[name.to_sym]
|
48
62
|
end
|
49
63
|
|
50
64
|
private
|
51
65
|
|
52
|
-
def
|
53
|
-
|
66
|
+
def register_cache_name_block(name, cache_name_block)
|
67
|
+
warn "#{name} has been cached before." if has_cache_association?(name)
|
68
|
+
cache_name_blocks[name] = cache_name_block
|
69
|
+
end
|
70
|
+
|
71
|
+
def cache_name_blocks
|
72
|
+
@cache_name_blocks ||= {}
|
54
73
|
end
|
55
74
|
end
|
56
75
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fuxin Hao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|