catch_cache 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 260cf4536507513b4873a3f6e4abc651e6928b60
4
- data.tar.gz: 9381ed6c9bc99673011bd8414afd31bebf5ee45f
3
+ metadata.gz: da32f650f05e83f0f73c6578c45d1db94687fcd6
4
+ data.tar.gz: 9ec7dbd9adfa577abd9d8d667c9eb76574cd4eae
5
5
  SHA512:
6
- metadata.gz: 0dac7694d9ccb255df40521f22e57ab811184f3bdea6b4f6539f533169e40ffb07563e0ab47c8e4eebee2def3a1f8d99b2545dcabc03d0d57807afd38fa543c6
7
- data.tar.gz: 4706a9e68beb6609c714b30c12122f0e706f57b04a4ec81e4033577869245b4e5fb3439c2689db04862eb0dc9aba0f51804f7debf20dabfff31c6b46c2c454a1
6
+ metadata.gz: 80f3f552424fd44ead7a6a68f75b07ec207b6744f07c62ebf09200fddbe238d0c4f2a32482672d59c4af72f3008e504be3091ab564de17a8ebce091e5c768ae9
7
+ data.tar.gz: c5b78339e0e103a1ce07467878ab4c1079a20e013eb1d8586114546d820dcc114f37d1a19e2bb51011bc2699a883cb4a3a906c01f52c1164e4d563b71db49121
@@ -1,3 +1,7 @@
1
+ # 0.2.0
2
+
3
+ - Update usage/API because of issues
4
+
1
5
  # 0.1.0
2
6
 
3
7
  - Update usage
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
 
@@ -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
- # a key_callback is a proc that returns
49
- # the unique identifier that will be
50
- # concatinated to the cache name
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
- # "lead_logs_cache_<uniq_id>"
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
- key_callbacks["#{args.first}__#{self.name.underscore}".to_sym] = args.second
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 callback
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
- send(callback, &callable)
75
+ callable = callable.keys.first
76
+ send(callback, callable)
70
77
  end
71
78
  end
72
79
  end
@@ -1,3 +1,3 @@
1
1
  module CatchCache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil Marion dela Cruz