cache_associations 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/cache_associations.rb +7 -1
- data/lib/cache_associations/version.rb +1 -1
- 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: 0990e10af89e0c1b3e90b5679ff9ced7a8afcdd9
|
4
|
+
data.tar.gz: 848feee285e44e564ef53c0ca97b0c52a75abb04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38c0a5abaa5176976e17633127ad9b1fed5a94052d033ee73b5c2628120115d1f3241799b714102f1e066b3642ed000d0af60db7685aa443301c82ca7857190b
|
7
|
+
data.tar.gz: eaad8c3f149c6595fe79be5dc93658102dcfa77cb660c428ebb171e921cb024b430b975e6495bde9cae0bc8e92dd6d1d2fbeb04dff9f7e0624e77a7dc835e556
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# CacheAssociations
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/cache_associations.svg)](https://badge.fury.io/rb/cache_associations)
|
3
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/FX-HAO/cache_associations.svg)](https://gemnasium.com/github.com/FX-HAO/cache_associations)
|
2
4
|
|
3
5
|
CacheAssociations is a simple cache implementation to make Low-Level caching collaborate with Association caching.
|
4
6
|
|
@@ -38,7 +40,7 @@ end
|
|
38
40
|
```
|
39
41
|
|
40
42
|
The `cache_association` defines a method `cached_profile` to fetch data from the cache store.
|
41
|
-
And `cache_association` accepts
|
43
|
+
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.
|
42
44
|
|
43
45
|
```ruby
|
44
46
|
irb> u = User.take
|
@@ -48,6 +50,20 @@ irb> u.profile.reload # refetch from the database
|
|
48
50
|
Profile Load (1.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
49
51
|
```
|
50
52
|
|
53
|
+
You may customize what the data you cached, the cached method accepts an optional block that determines how to cache data.
|
54
|
+
But it will break the original association, so you should be careful with this feature.
|
55
|
+
|
56
|
+
```
|
57
|
+
irb> u = User.take
|
58
|
+
irb> u.cached_profile do "gotcha" end
|
59
|
+
=> "gotcha"
|
60
|
+
irb> u.profile # it breaks
|
61
|
+
(Object doesn't support #inspect)
|
62
|
+
=>
|
63
|
+
irb> u.profile.reload # restore
|
64
|
+
Profile Load (1.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
65
|
+
```
|
66
|
+
|
51
67
|
## Development
|
52
68
|
|
53
69
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/cache_associations.rb
CHANGED
@@ -14,7 +14,13 @@ module CacheAssociations
|
|
14
14
|
define_method("cached_#{name}") do |*args, &block|
|
15
15
|
cache_name = cache_name_block.nil? ? [self.class.name, id, name, updated_at.to_i] : instance_exec(&cache_name_block)
|
16
16
|
cache = Rails.cache.fetch(cache_name) do
|
17
|
-
|
17
|
+
break instance_exec(*args, &block) if !block.nil?
|
18
|
+
|
19
|
+
if reflection.collection?
|
20
|
+
send("#{name}", *args).to_a
|
21
|
+
else
|
22
|
+
send("#{name}", *args)
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
20
26
|
if association_instance_get(name).nil?
|
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.1.
|
4
|
+
version: 0.1.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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|