catch_cache 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +26 -9
- data/lib/catch_cache/flush.rb +12 -8
- data/lib/catch_cache/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 662566bebd0e5976fabadbd420e8232dc6d59fb1
|
4
|
+
data.tar.gz: d41e1ac4a87b44583b1600d3626785847bfe3c27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8adfa928da3c75f48c3f59d22fecf7c9f3efe369d3446a5c9c7c4974b8264a0166b917c1e61ab827db9a696b16ef7dee856ff5e3f618491047dfa009226f19ef
|
7
|
+
data.tar.gz: 192116052ba1636f97f6fccb46044c376cf9edaba122e95f7be1f49e13edf8dbecf080a624e1fe74f801f8785392478893786e52d279c8029db99cd71d8c1590
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# CatchCache
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
An easy way to manage caching and flushing of Ruby objects. Especially useful when you are speeding a very slow API or page.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,17 +20,36 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
class ServiceWithAVerySlowQuery
|
25
|
+
include CatchCache::Cache
|
26
|
+
|
27
|
+
def query
|
28
|
+
lead = get_some_lead
|
29
|
+
catch_then_cache("lead_timeline_logs_#{lead.id}") do
|
30
|
+
# Your very slow query which
|
31
|
+
# returns a bunch of Ruby objects
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
26
36
|
|
27
|
-
|
37
|
+
In your AR model:
|
28
38
|
|
29
|
-
|
39
|
+
```ruby
|
40
|
+
In your AR model:
|
30
41
|
|
31
|
-
|
42
|
+
class LoanApplication < ActiveRecord::Base
|
43
|
+
include CatchCache::Flush
|
32
44
|
|
33
|
-
|
45
|
+
belongs_to :lead
|
34
46
|
|
35
|
-
|
47
|
+
# Everytime the :after_commit AR callback is called,
|
48
|
+
# the Redis cache with id "lead_timeline_logs_#{lead.id}"
|
49
|
+
# is going to be flushed
|
50
|
+
cache_id :lead_timeline_logs, -> { lead.id }
|
51
|
+
end
|
52
|
+
```
|
36
53
|
|
37
54
|
## License
|
38
55
|
|
data/lib/catch_cache/flush.rb
CHANGED
@@ -9,14 +9,18 @@ module CatchCache
|
|
9
9
|
define_method(:flush_cache!) do
|
10
10
|
key_callbacks = ClassMethods.key_callbacks
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
begin
|
13
|
+
key_callbacks.keys.each do |key|
|
14
|
+
# Get the uniq id defined in the AR model
|
15
|
+
uniq_id = instance_exec(&key_callbacks[key])
|
16
|
+
# Build the redis cache key
|
17
|
+
cache_key = "#{key.to_s}_#{uniq_id}"
|
18
|
+
redis = Redis.new
|
19
|
+
# Flush the key by setting it to nil
|
20
|
+
redis.set(cache_key, nil)
|
21
|
+
end
|
22
|
+
rescue NameError => e
|
23
|
+
# Nothing was flushed because of an error"
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
data/lib/catch_cache/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: catch_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neil Marion dela Cruz
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- ".gitignore"
|
77
77
|
- ".rspec"
|
78
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|