multi_cache 0.1.1 → 0.1.2
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/lib/multi_cache.rb +45 -47
- data/lib/multi_cache/version.rb +1 -1
- data/multi_cache-0.1.1.gem +0 -0
- data/multi_cache.gemspec +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 10a0489c08d03d2a52fd956ce51701cd21caaba3
|
|
4
|
+
data.tar.gz: 678eeeb73f29422cea57545f8d1bee98754543f9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab03e772a21ee54221b7ade431c0ed4c152ab19f929dfbc6c9912ac57cfbeeeffc486c3cd19613e24c75558e3ec17b54ebc3340d313ff1f52dbf694ec4279885
|
|
7
|
+
data.tar.gz: b142534604bd6770494b15390f3cd12333d7327a835a2b357921960c23419468348fe617344fb16c59f404ffd1b74bd3a4e6752bd6455614d0b66247138f7e80
|
data/lib/multi_cache.rb
CHANGED
|
@@ -44,9 +44,11 @@ module MultiCache
|
|
|
44
44
|
#
|
|
45
45
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
46
46
|
|
|
47
|
-
CACHE_KEY_MASTER_PREFIX
|
|
48
|
-
CACHE_KEY_SEPARATOR
|
|
49
|
-
|
|
47
|
+
CACHE_KEY_MASTER_PREFIX = 'MultiCache'
|
|
48
|
+
CACHE_KEY_SEPARATOR = ':'
|
|
49
|
+
# Three months in seconds
|
|
50
|
+
MULTICACHE_DEFAULT_TTL = 7776000
|
|
51
|
+
@@multicache_redis_name = nil
|
|
50
52
|
|
|
51
53
|
def self.configure
|
|
52
54
|
raise ArgumentError, "requires a block" unless block_given?
|
|
@@ -66,40 +68,47 @@ module MultiCache
|
|
|
66
68
|
|
|
67
69
|
included do
|
|
68
70
|
|
|
69
|
-
after_save
|
|
71
|
+
after_save :destroy_obj_cache
|
|
70
72
|
after_destroy :destroy_obj_cache
|
|
71
73
|
|
|
72
|
-
def self.get_cached(id_or_obj,
|
|
74
|
+
def self.get_cached(id_or_obj, cache_category)
|
|
73
75
|
id_and_obj = get_id_and_obj(id_or_obj)
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
validate_cache_category(cache_category)
|
|
76
78
|
|
|
77
|
-
cache_key = obj_cache_key(id_and_obj[:id]
|
|
78
|
-
|
|
79
|
+
cache_key = obj_cache_key(id_and_obj[:id])
|
|
80
|
+
cached_json = MultiCache.get_redis.hget(cache_key, cache_category)
|
|
79
81
|
|
|
80
|
-
if
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
end
|
|
87
|
-
cached = MultiCache.get_redis.hgetall(cache_key)
|
|
82
|
+
if cached_json.nil?
|
|
83
|
+
# Not found in cache
|
|
84
|
+
cached_hash = gen_cache_content(id_or_obj, cache_category)
|
|
85
|
+
self.write_to_cache(cached_hash, cache_key, cache_category)
|
|
86
|
+
else
|
|
87
|
+
cached_hash = JSON.parse(cached_json)
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
-
parse_cache_content(
|
|
90
|
+
parse_cache_content(cached_hash, cache_category)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.write_to_cache(cached_hash, cache_key, cache_category)
|
|
94
|
+
raise "the output of GEN_CACHE_CONTENT must be a hash" if !(cached_hash.is_a? Hash)
|
|
95
|
+
if !cached_hash.nil?
|
|
96
|
+
created = MultiCache.get_redis.hset(cache_key, cache_category, cached_hash.to_json)
|
|
97
|
+
MultiCache.get_redis.expire(cache_key, MULTICACHE_DEFAULT_TTL) if created
|
|
98
|
+
end
|
|
91
99
|
end
|
|
92
100
|
|
|
93
101
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
94
102
|
# Cache key determination
|
|
95
103
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
96
|
-
def obj_cache_key(
|
|
97
|
-
self.class.obj_cache_key(
|
|
104
|
+
def obj_cache_key(id = self.id)
|
|
105
|
+
self.class.obj_cache_key(id)
|
|
98
106
|
end
|
|
99
107
|
|
|
100
|
-
def self.obj_cache_key(id
|
|
108
|
+
def self.obj_cache_key(id)
|
|
101
109
|
# Do not change ordering since we match keys using this
|
|
102
|
-
|
|
110
|
+
raise ArgumentError.new 'Key can not be blank' if id.blank?
|
|
111
|
+
[fixed_cache_prefix(id)].join(CACHE_KEY_SEPARATOR)
|
|
103
112
|
end
|
|
104
113
|
|
|
105
114
|
def self.fixed_cache_prefix(id = nil)
|
|
@@ -110,26 +119,16 @@ module MultiCache
|
|
|
110
119
|
|
|
111
120
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
112
121
|
# Cache destruction
|
|
113
|
-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
123
|
+
|
|
124
|
+
def destroy_obj_cache(category = nil)
|
|
125
|
+
self.class.destroy_obj_cache(self.id, category)
|
|
116
126
|
end
|
|
117
127
|
|
|
118
|
-
def self.destroy_obj_cache(id)
|
|
128
|
+
def self.destroy_obj_cache(id, category = nil)
|
|
119
129
|
# Delete cache for one object only
|
|
120
130
|
prefix = self.fixed_cache_prefix(id)
|
|
121
|
-
MultiCache.del_from_redis(prefix)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def self.destroy_class_cache
|
|
125
|
-
# Destroy cache for all objects of this class
|
|
126
|
-
prefix = self.fixed_cache_prefix
|
|
127
|
-
MultiCache.del_from_redis(prefix)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def self.destroy_cache_keys
|
|
131
|
-
# Destroy cache for all MultiCache
|
|
132
|
-
MultiCache.del_from_redis(CACHE_KEY_MASTER_PREFIX)
|
|
131
|
+
MultiCache.del_from_redis(prefix, category)
|
|
133
132
|
end
|
|
134
133
|
|
|
135
134
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
|
@@ -152,20 +151,19 @@ module MultiCache
|
|
|
152
151
|
id_and_obj
|
|
153
152
|
end
|
|
154
153
|
|
|
155
|
-
def self.
|
|
156
|
-
if !(multi_cache_prefixes.include?
|
|
157
|
-
raise "#{self} Class: cache
|
|
158
|
-
|
|
154
|
+
def self.validate_cache_category(cache_category)
|
|
155
|
+
if !(multi_cache_prefixes.include? cache_category)
|
|
156
|
+
raise "#{self} Class: cache category '#{cache_category}' " +
|
|
157
|
+
"must be among #{multi_cache_prefixes}"
|
|
159
158
|
end
|
|
160
159
|
end
|
|
161
160
|
end
|
|
162
161
|
|
|
163
|
-
def self.del_from_redis(
|
|
164
|
-
|
|
165
|
-
MultiCache.get_redis.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
end
|
|
162
|
+
def self.del_from_redis(prefix, category)
|
|
163
|
+
if category.nil?
|
|
164
|
+
MultiCache.get_redis.del(prefix)
|
|
165
|
+
else
|
|
166
|
+
MultiCache.get_redis.hdel(prefix, Array.wrap(category).compact)
|
|
169
167
|
end
|
|
170
168
|
end
|
|
171
169
|
end
|
data/lib/multi_cache/version.rb
CHANGED
|
Binary file
|
data/multi_cache.gemspec
CHANGED
|
@@ -5,8 +5,8 @@ require 'multi_cache/version'
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "multi_cache"
|
|
8
|
-
spec.version = '0.1.
|
|
9
|
-
spec.authors = ["Akshay Rao"]
|
|
8
|
+
spec.version = '0.1.2'
|
|
9
|
+
spec.authors = ["Akshay Rao", "Sonu Kumar"]
|
|
10
10
|
spec.email = ["14akshayrao@gmail.com"]
|
|
11
11
|
|
|
12
12
|
spec.summary = %q{Framework to help you easily manage caches under multiple keys}
|
metadata
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: multi_cache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Akshay Rao
|
|
8
|
+
- Sonu Kumar
|
|
8
9
|
autorequire:
|
|
9
10
|
bindir: exe
|
|
10
11
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
|
12
13
|
dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
|
14
15
|
name: bundler
|
|
@@ -84,6 +85,7 @@ files:
|
|
|
84
85
|
- lib/multi_cache.rb
|
|
85
86
|
- lib/multi_cache/version.rb
|
|
86
87
|
- multi_cache-0.1.0.gem
|
|
88
|
+
- multi_cache-0.1.1.gem
|
|
87
89
|
- multi_cache.gemspec
|
|
88
90
|
homepage: http://www.avanti.in
|
|
89
91
|
licenses:
|