multi_cache 0.1.1 → 0.1.2

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: 9a7177cba4599c246887fff21c95f74bb6824a33
4
- data.tar.gz: 5ee77e35dc0158d261b5274a315f14006c058b7b
3
+ metadata.gz: 10a0489c08d03d2a52fd956ce51701cd21caaba3
4
+ data.tar.gz: 678eeeb73f29422cea57545f8d1bee98754543f9
5
5
  SHA512:
6
- metadata.gz: 630ec8d1b446bbc2d51fc8d7193643d9b3d40e63ecc78689bedb43ef0b73fc7b659d23b16c0a829199fccd211bfcd4f12ccc214ff97749fb2baef14aca6fc605
7
- data.tar.gz: cbabc056f51cee3f6f1a437aa0171df128b671c4f7e269f80728280e5a8c087b374105d21fd9947905778d28e5cfccf6a9265224f412956787806028fbf08afa
6
+ metadata.gz: ab03e772a21ee54221b7ade431c0ed4c152ab19f929dfbc6c9912ac57cfbeeeffc486c3cd19613e24c75558e3ec17b54ebc3340d313ff1f52dbf694ec4279885
7
+ data.tar.gz: b142534604bd6770494b15390f3cd12333d7327a835a2b357921960c23419468348fe617344fb16c59f404ffd1b74bd3a4e6752bd6455614d0b66247138f7e80
@@ -44,9 +44,11 @@ module MultiCache
44
44
  #
45
45
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
46
46
 
47
- CACHE_KEY_MASTER_PREFIX = "MultiCache"
48
- CACHE_KEY_SEPARATOR = "_"
49
- @@multicache_redis_name = nil
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 :destroy_obj_cache
71
+ after_save :destroy_obj_cache
70
72
  after_destroy :destroy_obj_cache
71
73
 
72
- def self.get_cached(id_or_obj, cache_prefix)
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
- validate_cache_prefix(cache_prefix)
77
+ validate_cache_category(cache_category)
76
78
 
77
- cache_key = obj_cache_key(id_and_obj[:id], cache_prefix)
78
- cached = MultiCache.get_redis.hgetall(cache_key)
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 cached.blank?
81
- cached = gen_cache_content(id_or_obj, cache_prefix)
82
-
83
- raise "the output of GEN_CACHE_CONTENT must be a hash" if !(cached.is_a?Hash)
84
- if cached.present?
85
- MultiCache.get_redis.hmset(cache_key, *(cached.to_a.reduce([], :+)))
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(cached, cache_prefix)
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(cache_prefix)
97
- self.class.obj_cache_key(self.id, cache_prefix)
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, custom_prefix)
108
+ def self.obj_cache_key(id)
101
109
  # Do not change ordering since we match keys using this
102
- [fixed_cache_prefix(id), custom_prefix.to_s].join(CACHE_KEY_SEPARATOR)
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
- def destroy_obj_cache(cb_obj = nil)
115
- self.class.destroy_obj_cache(self.id)
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.validate_cache_prefix(cache_prefix)
156
- if !(multi_cache_prefixes.include?cache_prefix)
157
- raise "#{self} Class: cache prefix '#{cache_prefix}' " +
158
- "must be among #{multi_cache_prefixes}"
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(prefix_match)
164
- Thread.new do
165
- MultiCache.get_redis.keys("#{prefix_match}*").each do |prefix|
166
- MultiCache.get_redis.del(prefix)
167
- # TODO: Use scan instead of keys
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
@@ -1,3 +1,3 @@
1
1
  module MultiCache
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
Binary file
@@ -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.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.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: 2017-10-12 00:00:00.000000000 Z
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: