cache_utils 0.1.7 → 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: 1787cc08190214e66542217e63c0995c5024d905
4
- data.tar.gz: d38caf6a9ab2c40270403cf1b0a8155e26f2aeb2
3
+ metadata.gz: 34deb953ce24f42f7218a3e8a58fc93c30dbb1dc
4
+ data.tar.gz: 076f808220c13e88e4c36301b0443d9624de5d70
5
5
  SHA512:
6
- metadata.gz: 91d4ce4697171ccb3991baa22f8132caa79803cf2e7d724381bfdb39893c5e3d5141573af81baa915e13cbfb5656ef5d973472b11cf609a8d342f88f15ec4d2d
7
- data.tar.gz: d3a5c7f51ec3f3367da643715a2e5c60938c9a91355b2440762f2b956dc1ab91a915795f6e933e9d815043015bb7c026673d993e2138808d6a7bf5939a64b0f7
6
+ metadata.gz: 1888ccefd50db738ae306fa798112bf8dc144a2f043963509b31b006c576490b2d8bbdeed50e4923c0d8b7014b8e97242434179ec670047247813cd36a52a72d
7
+ data.tar.gz: f988f3f9eb0c151847f48aa95002498d7b858dde418d826eb2092de17328774f8738b92fc28fe787a60b73d5887240ac84f7351aa902ed5258a014795e5a5996
@@ -18,12 +18,24 @@ module CacheUtils
18
18
 
19
19
  def cache_if(condition, name = {}, options = nil, &block)
20
20
  if condition
21
- Rails.cache.fetch(name, options, &block)
21
+ fetch_cache(name, options, &block)
22
22
  else
23
23
  yield
24
24
  end
25
25
  end
26
26
 
27
+ def fetch_cache(key, _options = nil)
28
+ cached = $redis.get(key)
29
+ if cached
30
+ Appsignal.increment_counter('cache_hit', 1) if defined?(Appsignal)
31
+ return cached
32
+ end
33
+ Appsignal.increment_counter('cache_miss', 1) if defined?(Appsignal)
34
+ fresh = yield
35
+ $redis.set(key, fresh)
36
+ fresh
37
+ end
38
+
27
39
  def serialize_for_cache(object, options = {})
28
40
  ActiveModelSerializers::SerializableResource.new(object, options)
29
41
  end
@@ -52,18 +64,3 @@ module CacheUtils
52
64
  end
53
65
  end
54
66
  end
55
-
56
- def serialize_hash(hash)
57
- hash.to_a.sort_by { |k, _v| k.to_s }.map do |k, v|
58
- val = if v.is_a?(Hash)
59
- serialize_hash(v)
60
- elsif v.is_a?(Array)
61
- v.map(&:to_s).sort
62
- elsif v.respond_to?(:cache_key)
63
- v.cache_key
64
- else
65
- v.to_s
66
- end
67
- [k, val]
68
- end.flatten.join('.')
69
- end
@@ -17,7 +17,8 @@ module CacheUtils
17
17
  end
18
18
 
19
19
  def purge_cache
20
- Rails.cache.data.del(*(Rails.cache.data.keys(custom_cache_key) + [self.class.cache_key]))
20
+ $redis.del(*($redis.scan_each(custom_cache_key + '*') + [self.class.cache_key]))
21
+ Appsignal.increment_counter('cache_purge', 1) if defined?(Appsignal)
21
22
  end
22
23
  end
23
24
  end
@@ -1,3 +1,3 @@
1
1
  module CacheUtils
2
- VERSION = '0.1.7'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michele Finotto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-07 00:00:00.000000000 Z
11
+ date: 2017-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler