Module: Legion::Cache::RedisHash

Extended by:
Logging::Helper
Defined in:
lib/legion/cache/redis_hash.rb

Class Method Summary collapse

Class Method Details

.expire(key, seconds) ⇒ Object

Set a TTL (in seconds) on a key.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/legion/cache/redis_hash.rb', line 115

def expire(key, seconds)
  return false unless redis_available?

  result = Legion::Cache.pool.with do |conn|
    conn.expire(key, seconds.to_i) == 1
  end
  log.debug "[cache:redis_hash] EXPIRE #{key} seconds=#{seconds} success=#{result}"
  result
rescue StandardError => e
  log_redis_error('expire', e)
  false
end

.hdel(key, *fields) ⇒ Object

Delete one or more hash fields.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cache/redis_hash.rb', line 55

def hdel(key, *fields)
  return 0 unless redis_available?

  result = Legion::Cache.pool.with do |conn|
    conn.hdel(key, *fields)
  end
  log.debug "[cache:redis_hash] HDEL #{key} fields=#{fields.size} removed=#{result}"
  result
rescue StandardError => e
  log_redis_error('hdel', e)
  0
end

.hgetall(key) ⇒ Object

Returns a Ruby Hash (string keys) of all field-value pairs for the key.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/cache/redis_hash.rb', line 41

def hgetall(key)
  return nil unless redis_available?

  result = Legion::Cache.pool.with do |conn|
    conn.hgetall(key)
  end
  log.debug "[cache:redis_hash] HGETALL #{key} fields=#{result.size}"
  result
rescue StandardError => e
  log_redis_error('hgetall', e)
  nil
end

.hset(key, hash) ⇒ Object

Set hash fields from a Ruby Hash. Uses Redis HSET key field value [field value …]



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/cache/redis_hash.rb', line 26

def hset(key, hash)
  return false unless redis_available?

  Legion::Cache.pool.with do |conn|
    flat = hash.flat_map { |k, v| [k.to_s, v.to_s] }
    conn.hset(key, *flat)
  end
  log.debug "[cache:redis_hash] HSET #{key} fields=#{hash.size}"
  true
rescue StandardError => e
  log_redis_error('hset', e)
  false
end

.log_redis_error(method, error) ⇒ Object



128
129
130
# File 'lib/legion/cache/redis_hash.rb', line 128

def log_redis_error(method, error)
  handle_exception(error, level: :warn, handled: true, operation: method)
end

.redis_available?Boolean

Returns true when the Redis driver is loaded and the connection pool is live.

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/cache/redis_hash.rb', line 13

def redis_available?
  pool = Legion::Cache.pool
  return false if pool.nil?
  return false unless Legion::Cache.respond_to?(:driver_name) && Legion::Cache.driver_name == 'redis'

  Legion::Cache.connected?
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :redis_hash_available)
  false
end

.zadd(key, score, member) ⇒ Object

Add a member to a sorted set with the given score.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/cache/redis_hash.rb', line 69

def zadd(key, score, member)
  return false unless redis_available?

  Legion::Cache.pool.with do |conn|
    conn.zadd(key, score.to_f, member.to_s)
  end
  log.debug "[cache:redis_hash] ZADD #{key} member=#{member}"
  true
rescue StandardError => e
  log_redis_error('zadd', e)
  false
end

.zrangebyscore(key, min, max, limit: nil) ⇒ Object

Range query on a sorted set by score. Returns an array of members. limit: accepts [offset, count] array matching Redis LIMIT semantics.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/legion/cache/redis_hash.rb', line 84

def zrangebyscore(key, min, max, limit: nil)
  return [] unless redis_available?

  opts = {}
  opts[:limit] = limit if limit

  result = Legion::Cache.pool.with do |conn|
    conn.zrangebyscore(key, min, max, **opts)
  end
  log.debug "[cache:redis_hash] ZRANGEBYSCORE #{key} results=#{result.size}"
  result
rescue StandardError => e
  log_redis_error('zrangebyscore', e)
  []
end

.zrem(key, member) ⇒ Object

Remove a member from a sorted set.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/legion/cache/redis_hash.rb', line 101

def zrem(key, member)
  return false unless redis_available?

  Legion::Cache.pool.with do |conn|
    conn.zrem(key, member.to_s)
  end
  log.debug "[cache:redis_hash] ZREM #{key} member=#{member}"
  true
rescue StandardError => e
  log_redis_error('zrem', e)
  false
end