mondrian_redis_segment_cache 0.1.1-java → 0.2.0-java

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: 3b62e31e45c942bb6005ee4fdd6b15c55f11374f
4
- data.tar.gz: bb0ef55a4e73c6ad13f73a0aaf2d8350f573132f
3
+ metadata.gz: 896185519afb0d52f37a79e5025257bdf5dfbc5c
4
+ data.tar.gz: 1bb17d24bf9ff9b6e93a43ab6eea7b3d819bc937
5
5
  SHA512:
6
- metadata.gz: 5ba18069f409a17f43d7f19d514534b89205af1ac5c68fe5dffc58687079805a047e8d4d0ca4dfc1cfe4d66beb82212f0d41f448383c8c32236c0b619bff0e0f
7
- data.tar.gz: fc48d6c75ec2c253aca7aa4ba39428858fef4dd08afee56c6a2c9f290b1a5d7b8e0012b4fff7b2a59b2ca5ab4d04a9a0d775aec05b54067d22f2e6e38b4b9e97
6
+ metadata.gz: 800116baba8a0b27f294f247543e81a970f5d73926e68e98ae3e03c48f77dd3aa59667b3ccf49d7d0ea3feb68d13988fa67e8f883d429e02181f320e247ae412
7
+ data.tar.gz: 33828588996fb1ac33f1d7d598de0b8949c8ffe7f5845e1823556ab9c2db3bbcca0de964e65b8f52f9a6b43f845ae11341a9a44c82f51f4688c1b2209d9b868d
data/README.md CHANGED
@@ -65,8 +65,9 @@ Cache expiry is handled by the options `:ttl` and `:expires_at`
65
65
  If you want a static ttl (time to live) then each key that is inserted will be set to expire after the ttl completes. This is
66
66
  not always optimal for an analytics cache and you may want all keys to expire at the same time (potentially on a daily basis).
67
67
 
68
- If you want all keys to expire at the same time you should use `:expires_at` in the options hash. This should be the hour that
69
- you want all keys to expire on. 1 being 1am, 2 being 2am, 15 being 3pm and so on.
68
+ If you want all keys to expire at the same time you should use `:expires_at` in the options hash. This should either be the hour
69
+ that you want all keys to expire on, or a `Time` object with a custom time. If you use `:expires_at` as an hour of expiration then
70
+ 1 is 1am, 2 is 2am, 15 is 3pm and so on.
70
71
 
71
72
 
72
73
  ## Contributing
@@ -7,12 +7,16 @@ module MondrianRedisSegmentCache
7
7
  include Java::MondrianSpi::SegmentCache
8
8
 
9
9
  attr_reader :created_listener_connection,
10
- :deleted_listener_connection,
11
- :evicted_listener_connection,
12
- :expired_listener_connection,
13
- :listeners,
14
- :mondrian_redis,
15
- :options
10
+ :deleted_listener_connection,
11
+ :evicted_listener_connection,
12
+ :expired_listener_connection,
13
+ :created_listener_thread,
14
+ :deleted_listener_thread,
15
+ :evicted_listener_thread,
16
+ :expired_listener_thread,
17
+ :listeners,
18
+ :mondrian_redis,
19
+ :options
16
20
 
17
21
  SEGMENT_HEADERS_SET_KEY = "MONDRIAN_SEGMENT_HEADERS_SET"
18
22
 
@@ -111,12 +115,16 @@ module MondrianRedisSegmentCache
111
115
  created_event = ::MondrianRedisSegmentCache::CreatedEvent.new(segment_header)
112
116
  listener.handle(created_event)
113
117
  end
118
+
119
+ check_listener_threads
114
120
  end
115
121
 
116
122
  def publish_created_to_listeners(message)
117
123
  listeners.each do |listener|
118
124
  publish_created_to_listener(message, listener)
119
125
  end
126
+
127
+ check_listener_threads
120
128
  end
121
129
 
122
130
  def publish_deleted_to_listener(message, listener)
@@ -126,6 +134,8 @@ module MondrianRedisSegmentCache
126
134
  deleted_event = ::MondrianRedisSegmentCache::DeletedEvent.new(segment_header)
127
135
  listener.handle(deleted_event)
128
136
  end
137
+
138
+ check_listener_threads
129
139
  end
130
140
 
131
141
  def publish_deleted_to_listeners(message)
@@ -137,6 +147,8 @@ module MondrianRedisSegmentCache
137
147
  if mondrian_redis.sismember(SEGMENT_HEADERS_SET_KEY, message)
138
148
  mondrian_redis.srem(SEGMENT_HEADERS_SET_KEY, message)
139
149
  end
150
+
151
+ check_listener_threads
140
152
  end
141
153
  alias_method :publish_evicted_to_listeners, :publish_deleted_to_listeners
142
154
  alias_method :publish_expired_to_listeners, :publish_deleted_to_listeners
@@ -175,11 +187,14 @@ module MondrianRedisSegmentCache
175
187
 
176
188
  def shutdown!
177
189
  # Ouch, why so harsh?
178
- @redis_events_threads.map(&:kill)
190
+ created_listener_thread.kill if created_listener_thread && created_listener_thread.alive?
191
+ deleted_listener_thread.kill if deleted_listener_thread && deleted_listener_thread.alive?
192
+ evicted_listener_thread.kill if evicted_listener_thread && evicted_listener_thread.alive?
193
+ expired_listener_thread.kill if expired_listener_thread && expired_listener_thread.alive?
179
194
  end
180
195
 
181
196
  def start
182
- register_for_redis_events
197
+ check_listener_threads
183
198
  end
184
199
 
185
200
  def supportsRichIndex()
@@ -202,6 +217,13 @@ module MondrianRedisSegmentCache
202
217
  ##
203
218
  # Private Instance Methods
204
219
  #
220
+ def check_listener_threads
221
+ register_created_listener if !created_listener_thread.respond_to?(:alive?) || !created_listener_thread.alive?
222
+ register_deleted_listener if !deleted_listener_thread.respond_to?(:alive?) || !deleted_listener_thread.alive?
223
+ register_expired_listener if !expired_listener_thread.respond_to?(:alive?) || !expired_listener_thread.alive?
224
+ register_evicted_listener if !evicted_listener_thread.respond_to?(:alive?) || !evicted_listener_thread.alive?
225
+ end
226
+
205
227
  def client_options
206
228
  # Redis 3.0.4 does not have options where 3.1 does
207
229
  unless mondrian_redis.client.respond_to?(:options)
@@ -219,7 +241,14 @@ module MondrianRedisSegmentCache
219
241
  return options[:ttl] if options[:ttl]
220
242
 
221
243
  now = Time.now
222
- difference_from_now = now.to_i - Time.new(now.year, now.month, now.day, options[:expires_at]).to_i
244
+
245
+ if options[:expires_at].is_a?(::Time)
246
+ expires_at = options[:expires_at]
247
+ else
248
+ expires_at = ::Time.new(now.year, now.month, now.day, options[:expires_at])
249
+ end
250
+
251
+ difference_from_now = now.to_i - expires_at.to_i
223
252
 
224
253
  until difference_from_now > 0 do
225
254
  difference_from_now = difference_from_now + 86_400 # already passed today, move to time tomorrow
@@ -241,48 +270,47 @@ module MondrianRedisSegmentCache
241
270
  end
242
271
  end
243
272
 
244
- def register_for_redis_events
245
- return if @listeners_registered
246
-
247
- # Not the best multi-threaded code, but its something that "works" for now and we will
248
- # worry about "best" later
249
- @redis_events_threads = []
250
-
251
- @redis_events_threads << Thread.new(created_listener_connection, self) do |created_redis_connection, mondrian_cache|
273
+ def register_created_listener
274
+ @created_listener_thread = Thread.new(created_listener_connection, self) do |created_redis_connection, mondrian_cache|
252
275
  created_redis_connection.subscribe(mondrian_cache.created_event_key) do |on|
253
276
  on.message do |channel, message|
254
277
  mondrian_cache.publish_created_to_listeners(message)
255
278
  end
256
279
  end
257
280
  end
281
+ end
258
282
 
259
- @redis_events_threads << Thread.new(deleted_listener_connection, self) do |deleted_redis_connection, mondrian_cache|
283
+ def register_deleted_listener
284
+ @deleted_listener_thread = Thread.new(deleted_listener_connection, self) do |deleted_redis_connection, mondrian_cache|
260
285
  deleted_redis_connection.subscribe(mondrian_cache.deleted_event_key) do |on|
261
286
  on.message do |channel, message|
262
287
  mondrian_cache.publish_deleted_to_listeners(message)
263
288
  end
264
289
  end
265
290
  end
291
+ end
266
292
 
267
- @redis_events_threads << Thread.new(expired_listener_connection, self) do |expired_redis_connection, mondrian_cache|
293
+ def register_expired_listener
294
+ @expired_listener_thread = Thread.new(expired_listener_connection, self) do |expired_redis_connection, mondrian_cache|
268
295
  expired_redis_connection.subscribe(mondrian_cache.expired_event_key) do |on|
269
296
  on.message do |channel, message|
270
297
  mondrian_cache.publish_expired_to_listeners(message)
271
298
  end
272
299
  end
273
300
  end
301
+ end
274
302
 
275
- @redis_events_threads << Thread.new(evicted_listener_connection, self) do |evicted_redis_connection, mondrian_cache|
303
+ def register_evicted_listener
304
+ @evicted_listener_thread = Thread.new(evicted_listener_connection, self) do |evicted_redis_connection, mondrian_cache|
276
305
  evicted_redis_connection.subscribe(mondrian_cache.evicted_event_key) do |on|
277
306
  on.message do |channel, message|
278
307
  mondrian_cache.publish_evicted_to_listeners(message)
279
308
  end
280
309
  end
281
310
  end
282
-
283
- @listeners_registered = true
284
311
  end
285
312
 
313
+
286
314
  def segment_body_from_base64(segment_body_base64)
287
315
  return nil unless segment_body_base64
288
316
  return ::Java::MondrianSpi::SegmentBody.from_base64(segment_body_base64)
@@ -1,3 +1,3 @@
1
1
  module MondrianRedisSegmentCache
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mondrian_redis_segment_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: java
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis