legion-cache 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/legion/cache/memcached.rb +10 -0
- data/lib/legion/cache/memory.rb +19 -0
- data/lib/legion/cache/redis.rb +11 -0
- data/lib/legion/cache/version.rb +1 -1
- data/lib/legion/cache.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 151b591709ad7af3c6012b1e701bef89a7516ee606bf8bd91385ed3da986f590
|
|
4
|
+
data.tar.gz: '0593b44cbefdfa99cf2f30fc4e1601ac9d758ed404896ca90eb639ddcc2a7fbd'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 95d1f2d1b482514062f7711727571b7a168f830454d0faf516d527f86eb3d047b892ac77b8c4709c163161414e1f099f5d751bd57c3b9b29f8e20de3c4282740
|
|
7
|
+
data.tar.gz: 8e7b5eb4667e70e23a91c75a65589210611d66865647ce9d0bb32edaff8fc323073c742a340594ea7dc395b9bd993d5c9493f38611eb211dcc39d8ee90f705b7
|
|
@@ -76,6 +76,16 @@ module Legion
|
|
|
76
76
|
nil
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
def set_nx(key, value, ttl: nil)
|
|
80
|
+
effective_ttl = ttl || default_ttl
|
|
81
|
+
result = client.with { |conn| conn.add(key, value, effective_ttl) == true }
|
|
82
|
+
log.debug { "[cache] SET_NX #{key} ttl=#{effective_ttl.inspect} result=#{result}" }
|
|
83
|
+
result
|
|
84
|
+
rescue StandardError => e
|
|
85
|
+
handle_exception(e, level: :error, handled: false, operation: :memcached_set_nx, key: key, ttl: effective_ttl)
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
|
|
79
89
|
def set(key, value, ttl: nil, **)
|
|
80
90
|
set_sync(key, value, ttl: ttl, **)
|
|
81
91
|
end
|
data/lib/legion/cache/memory.rb
CHANGED
|
@@ -55,6 +55,25 @@ module Legion
|
|
|
55
55
|
set_sync(key, value, ttl: ttl, phi: phi)
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
+
def set_nx(key, value, ttl: nil)
|
|
59
|
+
@mutex.synchronize do
|
|
60
|
+
expire_if_needed(key)
|
|
61
|
+
return false if @store.key?(key)
|
|
62
|
+
|
|
63
|
+
@store[key] = value
|
|
64
|
+
if ttl&.positive?
|
|
65
|
+
@expiry[key] = Time.now + ttl
|
|
66
|
+
else
|
|
67
|
+
@expiry.delete(key)
|
|
68
|
+
end
|
|
69
|
+
log.debug { "[cache:memory] SET_NX #{key} ttl=#{ttl.inspect} result=true" }
|
|
70
|
+
true
|
|
71
|
+
end
|
|
72
|
+
rescue StandardError => e
|
|
73
|
+
handle_exception(e, level: :warn, handled: true, operation: :memory_set_nx)
|
|
74
|
+
false
|
|
75
|
+
end
|
|
76
|
+
|
|
58
77
|
def set_sync(key, value, ttl: nil, phi: false)
|
|
59
78
|
ttl = enforce_phi_ttl(ttl, phi: phi) if phi
|
|
60
79
|
@mutex.synchronize do
|
data/lib/legion/cache/redis.rb
CHANGED
|
@@ -100,6 +100,17 @@ module Legion
|
|
|
100
100
|
result
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
+
def set_nx(key, value, ttl: nil)
|
|
104
|
+
effective_ttl = ttl || default_ttl
|
|
105
|
+
serialized = serialize_value(value)
|
|
106
|
+
result = client.with { |conn| conn.set(key, serialized, nx: true, ex: effective_ttl) == 'OK' }
|
|
107
|
+
log.debug { "[cache] SET_NX #{key} ttl=#{effective_ttl.inspect} result=#{result}" }
|
|
108
|
+
result
|
|
109
|
+
rescue StandardError => e
|
|
110
|
+
handle_exception(e, level: :error, handled: false, operation: :redis_set_nx, key: key, ttl: effective_ttl)
|
|
111
|
+
raise
|
|
112
|
+
end
|
|
113
|
+
|
|
103
114
|
def set(key, value, ttl: nil, **)
|
|
104
115
|
set_sync(key, value, ttl: ttl, **)
|
|
105
116
|
end
|
data/lib/legion/cache/version.rb
CHANGED
data/lib/legion/cache.rb
CHANGED
|
@@ -190,6 +190,19 @@ module Legion
|
|
|
190
190
|
end
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
+
def set_nx(key, value, ttl: nil)
|
|
194
|
+
effective_ttl = resolve_ttl(ttl)
|
|
195
|
+
return Legion::Cache::Memory.set_nx(key, value, ttl: effective_ttl) if using_memory?
|
|
196
|
+
return Legion::Cache::Local.set_nx(key, value, ttl: effective_ttl) if using_local?
|
|
197
|
+
return Legion::Cache::Local.set_nx(key, value, ttl: effective_ttl) if failback_to_local?
|
|
198
|
+
|
|
199
|
+
configure_shared_adapter!
|
|
200
|
+
super
|
|
201
|
+
rescue StandardError => e
|
|
202
|
+
handle_exception(e, level: :warn, handled: true, operation: :cache_set_nx, key: key)
|
|
203
|
+
false
|
|
204
|
+
end
|
|
205
|
+
|
|
193
206
|
def set_sync(key, value, ttl: nil, **)
|
|
194
207
|
return Legion::Cache::Memory.set_sync(key, value, ttl: ttl) if using_memory?
|
|
195
208
|
return Legion::Cache::Local.set_sync(key, value, ttl: ttl) if using_local?
|