Module: Legion::Cache::Memory
Instance Method Summary collapse
- #available ⇒ Object
- #client ⇒ Object
- #close ⇒ Object
- #connected? ⇒ Boolean
-
#delete(key, async: true) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #delete_sync(key) ⇒ Object
- #fetch(key, ttl: nil, &block) ⇒ Object
- #flush ⇒ Object
- #get(key) ⇒ Object
- #mget(*keys) ⇒ Object
- #mset(hash, ttl: nil, async: true) ⇒ Object
- #mset_sync(hash, ttl: nil, phi: false) ⇒ Object
- #reset! ⇒ Object
- #restart ⇒ Object
-
#set(key, value, ttl: nil, async: true, phi: false) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #set_nx(key, value, ttl: nil) ⇒ Object
- #set_sync(key, value, ttl: nil, phi: false) ⇒ Object
- #setup ⇒ Object
- #shutdown ⇒ Object
- #size ⇒ Object
Instance Method Details
#available ⇒ Object
193 |
# File 'lib/legion/cache/memory.rb', line 193 def available = 1 |
#client ⇒ Object
27 |
# File 'lib/legion/cache/memory.rb', line 27 def client(**) = self |
#close ⇒ Object
169 |
# File 'lib/legion/cache/memory.rb', line 169 def close = nil |
#connected? ⇒ Boolean
29 30 31 |
# File 'lib/legion/cache/memory.rb', line 29 def connected? @connected.true? end |
#delete(key, async: true) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
104 105 106 |
# File 'lib/legion/cache/memory.rb', line 104 def delete(key, async: true) # rubocop:disable Lint/UnusedMethodArgument delete_sync(key) end |
#delete_sync(key) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/legion/cache/memory.rb', line 108 def delete_sync(key) @mutex.synchronize do removed = @store.delete(key) @expiry.delete(key) log.debug { "[cache:memory] DELETE #{key} success=#{!removed.nil?}" } !removed.nil? end end |
#fetch(key, ttl: nil, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/legion/cache/memory.rb', line 91 def fetch(key, ttl: nil, &block) val = get(key) return val unless val.nil? log.debug { "[cache:memory] FETCH #{key} miss=true" } val = block&.call set(key, val, ttl: ttl) val rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_fetch) nil end |
#flush ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/legion/cache/memory.rb', line 157 def flush @mutex.synchronize do @store.clear @expiry.clear end log.info 'Legion::Cache::Memory flushed' true rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_flush) false end |
#get(key) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/legion/cache/memory.rb', line 42 def get(key) @mutex.synchronize do expire_if_needed(key) result = @store[key] log.debug { "[cache:memory] GET #{key} hit=#{!result.nil?}" } result end rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_get) nil end |
#mget(*keys) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/legion/cache/memory.rb', line 117 def mget(*keys) keys = keys.flatten return {} if keys.empty? @mutex.synchronize do keys.each { |k| expire_if_needed(k) } keys.to_h { |k| [k, @store[k]] } end rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_mget) {} end |
#mset(hash, ttl: nil, async: true) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/legion/cache/memory.rb', line 130 def mset(hash, ttl: nil, async: true) return true if hash.empty? hash.each { |k, v| set(k, v, ttl: ttl, async: async) } true rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_mset) true end |
#mset_sync(hash, ttl: nil, phi: false) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/legion/cache/memory.rb', line 140 def mset_sync(hash, ttl: nil, phi: false) return true if hash.empty? @mutex.synchronize do hash.each do |key, value| effective_ttl = phi ? enforce_phi_ttl(ttl, phi: true) : ttl @store[key] = value if effective_ttl&.positive? @expiry[key] = Time.now + effective_ttl else @expiry.delete(key) end end true end end |
#reset! ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/legion/cache/memory.rb', line 182 def reset! @mutex.synchronize do @connected.make_false @store.clear @expiry.clear end log.info 'Legion::Cache::Memory state reset' false end |
#restart ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/legion/cache/memory.rb', line 33 def restart(**) shutdown setup rescue StandardError => e @connected.make_false handle_exception(e, level: :warn, handled: true, operation: :memory_restart) false end |
#set(key, value, ttl: nil, async: true, phi: false) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
54 55 56 |
# File 'lib/legion/cache/memory.rb', line 54 def set(key, value, ttl: nil, async: true, phi: false) # rubocop:disable Lint/UnusedMethodArgument set_sync(key, value, ttl: ttl, phi: phi) end |
#set_nx(key, value, ttl: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/legion/cache/memory.rb', line 58 def set_nx(key, value, ttl: nil) @mutex.synchronize do expire_if_needed(key) return false if @store.key?(key) @store[key] = value if ttl&.positive? @expiry[key] = Time.now + ttl else @expiry.delete(key) end log.debug { "[cache:memory] SET_NX #{key} ttl=#{ttl.inspect} result=true" } true end rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :memory_set_nx) false end |
#set_sync(key, value, ttl: nil, phi: false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/legion/cache/memory.rb', line 77 def set_sync(key, value, ttl: nil, phi: false) ttl = enforce_phi_ttl(ttl, phi: phi) if phi @mutex.synchronize do @store[key] = value if ttl&.positive? @expiry[key] = Time.now + ttl else @expiry.delete(key) end log.debug { "[cache:memory] SET #{key} ttl=#{ttl.inspect}" } true end end |
#setup ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/legion/cache/memory.rb', line 17 def setup(**) @connected.make_true log.info 'Legion::Cache::Memory connected' true rescue StandardError => e @connected.make_false handle_exception(e, level: :warn, handled: true, operation: :memory_setup) false end |
#shutdown ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/legion/cache/memory.rb', line 171 def shutdown flush @connected.make_false log.info 'Legion::Cache::Memory shut down' false rescue StandardError => e @connected.make_false handle_exception(e, level: :warn, handled: true, operation: :memory_shutdown) false end |
#size ⇒ Object
192 |
# File 'lib/legion/cache/memory.rb', line 192 def size = 1 |