catch_cache 0.1.0 → 0.2.0
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/CHANGELOG.md +4 -0
- data/README.md +4 -1
- data/lib/catch_cache/flush.rb +16 -9
- data/lib/catch_cache/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: da32f650f05e83f0f73c6578c45d1db94687fcd6
|
4
|
+
data.tar.gz: 9ec7dbd9adfa577abd9d8d667c9eb76574cd4eae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80f3f552424fd44ead7a6a68f75b07ec207b6744f07c62ebf09200fddbe238d0c4f2a32482672d59c4af72f3008e504be3091ab564de17a8ebce091e5c768ae9
|
7
|
+
data.tar.gz: c5b78339e0e103a1ce07467878ab4c1079a20e013eb1d8586114546d820dcc114f37d1a19e2bb51011bc2699a883cb4a3a906c01f52c1164e4d563b71db49121
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
### To cache objects
|
24
|
+
|
23
25
|
```ruby
|
24
26
|
class ServiceWithAVerySlowQuery
|
25
27
|
include CatchCache::Cache
|
@@ -34,6 +36,7 @@ class ServiceWithAVerySlowQuery
|
|
34
36
|
end
|
35
37
|
```
|
36
38
|
|
39
|
+
### :flush_cache!
|
37
40
|
In your AR model:
|
38
41
|
|
39
42
|
```ruby
|
@@ -46,7 +49,7 @@ class LoanApplication < ActiveRecord::Base
|
|
46
49
|
# the Redis cache with id "lead_timeline_logs_#{lead.id}"
|
47
50
|
# is going to be flushed
|
48
51
|
|
49
|
-
cache_id :lead_timeline_logs, after_commit: -> { lead.id }
|
52
|
+
cache_id :lead_timeline_logs, after_commit: -> { flush_cache!: -> { lead.id } }
|
50
53
|
end
|
51
54
|
```
|
52
55
|
|
data/lib/catch_cache/flush.rb
CHANGED
@@ -8,7 +8,6 @@ module CatchCache
|
|
8
8
|
define_method(:flush_cache!) do
|
9
9
|
key_callbacks = ClassMethods.key_callbacks
|
10
10
|
|
11
|
-
|
12
11
|
key_callbacks.keys.select{|key| key.to_s.split("__").last == self.class.name.underscore }.each do |key|
|
13
12
|
# Get the uniq id defined in the AR model
|
14
13
|
begin
|
@@ -27,7 +26,7 @@ module CatchCache
|
|
27
26
|
define_method(:flush_all!) do
|
28
27
|
redis = Redis.new
|
29
28
|
|
30
|
-
registered_keys = ClassMethods.key_callbacks.keys
|
29
|
+
registered_keys = ClassMethods.key_callbacks.keys.map{|key| key.to_s.split("__").first.to_sym }.uniq
|
31
30
|
removable_keys = redis.keys.select do |key|
|
32
31
|
registered_keys.include?(key.gsub(/\_[0-9]+/, '').to_sym)
|
33
32
|
end
|
@@ -45,16 +44,23 @@ module CatchCache
|
|
45
44
|
# for our caches
|
46
45
|
self.key_callbacks = {}
|
47
46
|
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
47
|
+
# key_callbacks is hash that stores the a redis key with the proc value that
|
48
|
+
# evaluates to a unique id associated with that class. For example:
|
49
|
+
# `central_page_loan_plans` key defines the cache for the loading of loan plans
|
50
|
+
# index page.
|
51
51
|
#
|
52
52
|
# An example of a redis key is
|
53
|
-
# "
|
53
|
+
# "central_page_loan_plans_<uniq_id>"
|
54
|
+
#
|
55
|
+
# Sample args for the cache_id are:
|
56
|
+
# cache_id :central_page_loan_plans, after_commit: { flush_cache!: -> { loan_plan.id } }
|
57
|
+
# cache_id :central_page_loan_plans, after_commit: :flush_all!
|
54
58
|
def cache_id(*args)
|
55
59
|
options = args.last if args.last.is_a?(Hash)
|
56
60
|
|
57
|
-
|
61
|
+
value_args = options.values.first
|
62
|
+
proc_value = value_args.is_a?(Hash) ? value_args[:flush_cache!] : value_args
|
63
|
+
key_callbacks["#{args.first}__#{self.name.underscore}".to_sym] = proc_value
|
58
64
|
register_callbacks_for(options) if options.present?
|
59
65
|
end
|
60
66
|
|
@@ -62,11 +68,12 @@ module CatchCache
|
|
62
68
|
|
63
69
|
def register_callbacks_for(options)
|
64
70
|
options.each do |callback, callable|
|
65
|
-
case
|
71
|
+
case callable
|
66
72
|
when Symbol
|
67
73
|
send(callback, callable) if respond_to?(callback)
|
68
74
|
else # It must be Proc or lambda
|
69
|
-
|
75
|
+
callable = callable.keys.first
|
76
|
+
send(callback, callable)
|
70
77
|
end
|
71
78
|
end
|
72
79
|
end
|
data/lib/catch_cache/version.rb
CHANGED