@@ -292,7 +292,8 @@ module Azu
292292 # Stop cleanup task (for testing or shutdown)
293293 def stop_cleanup_task
294294 if fiber = @cleanup_fiber
295- fiber.kill
295+ # In Crystal, we can't forcefully kill fibers, but we can clear the reference
296+ # The fiber will naturally stop when the cleanup interval expires
296297 @cleanup_fiber = nil
297298 end
298299 end
@@ -314,18 +315,15 @@ module Azu
314315 end
315316
316317 def get (key : String ) : String ?
317- start_time = Time .monotonic
318318 with_timeout(" get" , key) { @client .get(key) }
319319 rescue ex
320- duration = Time .monotonic - start_time
321320 Log .for(" Azu::Cache::RedisStore" ).error(exception: ex) {
322- " Redis GET operation failed for key '#{ key } ' after #{ duration.total_milliseconds } ms - #{ classify_redis_error(ex) } "
321+ " Redis GET operation failed for key '#{ key } ' - #{ classify_redis_error(ex) } "
323322 }
324323 nil
325324 end
326325
327326 def set (key : String , value : String , ttl : Time ::Span ? = nil ) : Bool
328- start_time = Time .monotonic
329327 ttl = ttl || @default_ttl
330328 if ttl
331329 with_timeout(" setex" , key) { @client .setex(key, ttl.total_seconds.to_i, value) }
@@ -334,24 +332,23 @@ module Azu
334332 end
335333 true
336334 rescue ex
337- duration = Time .monotonic - start_time
338335 Log .for(" Azu::Cache::RedisStore" ).error(exception: ex) {
339- " Redis SET operation failed for key '#{ key } ' after #{ duration.total_milliseconds } ms - #{ classify_redis_error(ex) } "
336+ " Redis SET operation failed for key '#{ key } ' - #{ classify_redis_error(ex) } "
340337 }
341338 false
342339 end
343340
344341 def delete (key : String ) : Bool
345342 result = with_timeout(" del" , key) { @client .del(key) }
346- result > 0
343+ ( result || 0 ) > 0
347344 rescue ex
348345 Log .for(" Azu::Cache::RedisStore" ).error(exception: ex) { " Failed to delete key: #{ key } " }
349346 false
350347 end
351348
352349 def exists ?(key : String ) : Bool
353350 result = with_timeout(" exists" , key) { @client .exists(key) }
354- result > 0
351+ ( result || 0 ) > 0
355352 rescue ex
356353 Log .for(" Azu::Cache::RedisStore" ).error(exception: ex) { " Failed to check existence of key: #{ key } " }
357354 false
@@ -368,6 +365,8 @@ module Azu
368365 def size : Int32
369366 # Get database info as hash
370367 info_hash = with_timeout(" info" , " size" ) { @client .info }
368+ return 0 unless info_hash
369+
371370 # Look for db0 or current database keys count
372371 if keyspace = info_hash[" db0" ]?
373372 # Parse db0 value like "keys=123,expires=45"
@@ -392,6 +391,8 @@ module Azu
392391 with_timeout(" incrby" , key) { @client .incrby(key, amount) }
393392 end
394393
394+ return nil unless result
395+
395396 # Set TTL if provided and key was just created
396397 if ttl && result == amount
397398 with_timeout(" expire" , key) { @client .expire(key, ttl.total_seconds.to_i) }
@@ -411,6 +412,8 @@ module Azu
411412 with_timeout(" decrby" , key) { @client .decrby(key, amount) }
412413 end
413414
415+ return nil unless result
416+
414417 # Set TTL if provided
415418 if ttl
416419 with_timeout(" expire" , key) { @client .expire(key, ttl.total_seconds.to_i) }
@@ -427,6 +430,8 @@ module Azu
427430 return Hash (String , String ?).new if keys.empty?
428431
429432 values = with_timeout(" mget" , keys.join(" ," )) { @client .mget(keys) }
433+ return Hash (String , String ?).new unless values
434+
430435 result = Hash (String , String ?).new
431436 keys.each_with_index do |key , index |
432437 result[key] = values[index]?.as(String | Nil )
@@ -502,8 +507,8 @@ module Azu
502507
503508 # Timeout wrapper for Redis operations
504509 private def with_timeout (operation : String , key : String , & block )
505- channel = Channel (typeof (block.call)).new
506- timeout_channel = Channel (Nil ).new
510+ channel = :: Channel (typeof (block.call)).new
511+ timeout_channel = :: Channel (Nil ).new
507512
508513 # Spawn the operation
509514 spawn do
@@ -528,7 +533,7 @@ module Azu
528533 result
529534 when timeout_channel.receive
530535 Log .for(" Azu::Cache::RedisStore" ).warn { " Redis #{ operation } operation timed out after #{ @operation_timeout .total_seconds} s for key: #{ key } " }
531- raise TimeoutError .new(" Redis #{ operation } operation timed out" )
536+ raise Exception .new(" Redis #{ operation } operation timed out" )
532537 end
533538 end
534539 end
0 commit comments