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 +4 -4
- data/lib/cache_utils/controller.rb +13 -16
- data/lib/cache_utils/model.rb +2 -1
- data/lib/cache_utils/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: 34deb953ce24f42f7218a3e8a58fc93c30dbb1dc
|
4
|
+
data.tar.gz: 076f808220c13e88e4c36301b0443d9624de5d70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/cache_utils/model.rb
CHANGED
@@ -17,7 +17,8 @@ module CacheUtils
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def purge_cache
|
20
|
-
|
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
|
data/lib/cache_utils/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2017-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|