Module: Legion::Cache::Local

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

Class Method Summary collapse

Class Method Details

.availableObject



166
167
168
# File 'lib/legion/cache/local.rb', line 166

def available
  @driver.available
end

.clientObject



141
142
143
# File 'lib/legion/cache/local.rb', line 141

def client
  @driver&.client
end

.closeObject



145
146
147
148
149
150
151
152
# File 'lib/legion/cache/local.rb', line 145

def close
  @driver&.close
  @driver = nil
  @driver_name = nil
  @connected.make_false
  log.info 'Legion::Cache::Local pool closed'
  @connected
end

.connected?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/legion/cache/local.rb', line 53

def connected?
  @connected&.true? || false
end

.delete(key) ⇒ Object



93
94
95
# File 'lib/legion/cache/local.rb', line 93

def delete(key, **)
  delete_sync(key)
end

.delete_sync(key) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/legion/cache/local.rb', line 97

def delete_sync(key)
  result = @driver.delete_sync(key)
  log.debug { "[cache:local] DELETE #{key} success=#{result}" }
  result
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :cache_local_delete_sync, key: key)
  raise
end

.driver_nameObject



57
58
59
# File 'lib/legion/cache/local.rb', line 57

def driver_name
  @driver_name || Legion::Cache::Settings.normalize_driver(local_settings[:driver] || Legion::Cache::Settings.driver)
end

.enabled?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/legion/cache/local.rb', line 44

def enabled?
  return true unless defined?(Legion::Settings)

  Legion::Settings.dig(:cache_local, :enabled) != false
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_enabled)
  true
end

.fetch(key, ttl: nil) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/legion/cache/local.rb', line 84

def fetch(key, ttl: nil, &)
  result = @driver.fetch(key, ttl: ttl, &)
  log.debug { "[cache:local] FETCH #{key} hit=#{!result.nil?}" }
  result
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_fetch, key: key, ttl: ttl)
  nil
end

.flushObject



106
107
108
109
110
111
112
113
# File 'lib/legion/cache/local.rb', line 106

def flush
  result = @driver.flush
  log.debug { '[cache:local] FLUSH completed' }
  result
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_flush)
  nil
end

.get(key) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/legion/cache/local.rb', line 61

def get(key)
  result = @driver.get(key)
  log.debug { "[cache:local] GET #{key} hit=#{!result.nil?}" }
  result
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_get, key: key)
  nil
end

.mget(*keys) ⇒ Object



115
116
117
118
119
120
# File 'lib/legion/cache/local.rb', line 115

def mget(*keys)
  keys = keys.flatten
  return {} if keys.empty?

  keys.to_h { |key| [key, get(key)] }
end

.mset(hash, ttl: nil) ⇒ Object



122
123
124
125
126
127
# File 'lib/legion/cache/local.rb', line 122

def mset(hash, ttl: nil, **)
  return true if hash.empty?

  hash.each { |key, value| set(key, value, ttl: ttl) }
  true
end

.pool_sizeObject



170
171
172
# File 'lib/legion/cache/local.rb', line 170

def pool_size
  @driver.pool_size
end

.reset!Object



178
179
180
181
182
183
184
# File 'lib/legion/cache/local.rb', line 178

def reset!
  @driver = nil
  @driver_name = nil
  @connected.make_false
  log.debug 'Legion::Cache::Local state reset'
  @connected
end

.restart(**opts) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/legion/cache/local.rb', line 154

def restart(**opts)
  settings = local_settings
  @driver&.restart(**settings.merge(opts, logger: log))
  @connected.make_true
  log.info 'Legion::Cache::Local pool restarted'
  @connected
end

.set(key, value, ttl: nil) ⇒ Object



70
71
72
# File 'lib/legion/cache/local.rb', line 70

def set(key, value, ttl: nil, **)
  set_sync(key, value, ttl: ttl, **)
end

.set_sync(key, value, ttl: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/legion/cache/local.rb', line 74

def set_sync(key, value, ttl: nil, **)
  effective_ttl = ttl || local_default_ttl
  result = @driver.set_sync(key, value, ttl: effective_ttl)
  log.debug { "[cache:local] SET #{key} ttl=#{effective_ttl} success=#{result}" }
  result
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :cache_local_set_sync, key: key, ttl: effective_ttl)
  raise
end

.setupObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/cache/local.rb', line 15

def setup(**)
  return unless enabled?
  return if connected?

  settings = local_settings
  return unless settings[:enabled]

  driver_name = settings[:driver] || Legion::Cache::Settings.driver
  @driver_name = Legion::Cache::Settings.normalize_driver(driver_name)
  @driver = build_driver(driver_name)
  @driver.client(**settings, logger: log, **)
  @connected.make_true
  servers = Array(settings[:servers]).join(', ')
  log.info "Legion::Cache::Local connected (#{driver_name}) to #{servers}"
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_setup, driver: driver_name)
  @connected.make_false
end

.shutdownObject



34
35
36
37
38
39
40
41
42
# File 'lib/legion/cache/local.rb', line 34

def shutdown
  return unless @connected

  log.info 'Shutting down Legion::Cache::Local'
  @driver&.close
  @driver = nil
  @driver_name = nil
  @connected.make_false
end

.sizeObject



162
163
164
# File 'lib/legion/cache/local.rb', line 162

def size
  @driver.size
end

.statsObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/legion/cache/local.rb', line 129

def stats
  {
    driver:    driver_name,
    servers:   local_servers,
    enabled:   enabled?,
    connected: connected?
  }.freeze
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :cache_local_stats)
  { error: e.message }.freeze
end

.timeoutObject



174
175
176
# File 'lib/legion/cache/local.rb', line 174

def timeout
  @driver.timeout
end