cache_associations 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/cache_associations.rb +3 -2
- data/lib/cache_associations/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25c9f69129b50855b9f24bd5d22e9b1cbc3a2258
|
4
|
+
data.tar.gz: 383ec8b76bfe25a25bae015bb690c55b450fc4eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e19b8231d349d5d6560fd2229474db3981909acabcaab381b6f736aaedec8a546467b1bf6979001d172258ec3210808ad45c3740c4861780936074ec45d5df62
|
7
|
+
data.tar.gz: df928e1a5a1a2f9265db2177282bd9587fd70576fec7dfa43cb1fb067efc0ae9823af0028eb3f83012416503e5dba94d1606129514cdb6ce2d0127c274672bfc
|
data/README.md
CHANGED
@@ -40,6 +40,8 @@ end
|
|
40
40
|
```
|
41
41
|
|
42
42
|
The `cache_association` defines a method `cached_profile` to fetch data from the cache store.
|
43
|
+
You may also specify additional options via the options argument, it's the same as `ActiveSupport::Cache::Store#fetch`,
|
44
|
+
such as setting `:expires_in` will set an expiration time on the cache.
|
43
45
|
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.
|
44
46
|
|
45
47
|
```ruby
|
data/lib/cache_associations.rb
CHANGED
@@ -6,14 +6,15 @@ module CacheAssociations
|
|
6
6
|
UndefinedAssociationError = Class.new(StandardError)
|
7
7
|
|
8
8
|
class_methods do
|
9
|
-
def cache_association(name, &cache_name_block)
|
9
|
+
def cache_association(name, **options, &cache_name_block)
|
10
10
|
unless reflection = reflect_on_association(name)
|
11
11
|
raise UndefinedAssociationError, "Undefined asscociation #{name}"
|
12
12
|
end
|
13
|
+
options = Rails.cache.options.merge(options)
|
13
14
|
|
14
15
|
define_method("cached_#{name}") do |*args, &block|
|
15
16
|
cache_name = cache_name_block.nil? ? [self.class.name, id, name, updated_at.to_i] : instance_exec(&cache_name_block)
|
16
|
-
cache = Rails.cache.fetch(cache_name) do
|
17
|
+
cache = Rails.cache.fetch(cache_name, **options) do
|
17
18
|
break instance_exec(*args, &block) if !block.nil?
|
18
19
|
|
19
20
|
if reflection.collection?
|