Package com.tnsai.idempotency
Class RedisIdempotencyStore
java.lang.Object
com.tnsai.idempotency.RedisIdempotencyStore
- All Implemented Interfaces:
IdempotencyStore
Distributed
IdempotencyStore backed by Redis (issue #91).
Use this when the framework runs as more than one process and dedup
needs to survive a crash or a horizontal restart.
Commands use the caller-owned, pooled RedisClient, so the
store can be shared across threads. The caller closes the client at
application shutdown; store operations never close it. TTL is enforced
server-side via SET ... EX ..., so stale entries need no sweep.
Entries are JSON-encoded by IdempotencyEntryCodec. The
round-trip is generic-JSON (not type-preserving): a typed POJO
cached in process A and read in process B comes back as a
Map/List mosaic. Consumers needing the original
type should re-marshal at the call site.
Optional dependency
Jedis is declared as an<optional> dependency on
tnsai-core: consumers add it themselves to opt in. Loading
this class without Jedis on the classpath is a hard
NoClassDefFoundError -- by design, since the store cannot
function without it.
Key prefix
The configured prefix is prepended to every key the store touches, giving operators a way to share a Redis instance across multiple unrelated TnsAI deployments. Default"tnsai:idem:" is safe
for sole-tenant Redis but should be customised for shared clusters.- Since:
- 0.10.0 (issue #91 closeout)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault key prefix; safe for sole-tenant Redis. -
Constructor Summary
ConstructorsConstructorDescriptionRedisIdempotencyStore(redis.clients.jedis.RedisClient client) UsesDEFAULT_KEY_PREFIXwith a caller-owned client; the store never closes it.RedisIdempotencyStore(redis.clients.jedis.RedisClient client, String keyPrefix) Uses a caller-owned client; the store never closes it. -
Method Summary
Modifier and TypeMethodDescriptionLook up a cached entry.voidinvalidate(String key) Remove the entry forkeyif any.voidput(String key, IdempotencyEntry entry, Duration ttl) Store an entry underkeywith the given TTL.intsize()Approximate entry count.
-
Field Details
-
DEFAULT_KEY_PREFIX
Default key prefix; safe for sole-tenant Redis.- See Also:
-
-
Constructor Details
-
RedisIdempotencyStore
public RedisIdempotencyStore(redis.clients.jedis.RedisClient client) UsesDEFAULT_KEY_PREFIXwith a caller-owned client; the store never closes it.- Since:
- 0.17.5
-
RedisIdempotencyStore
Uses a caller-owned client; the store never closes it.- Since:
- 0.17.5
-
-
Method Details
-
get
Description copied from interface:IdempotencyStoreLook up a cached entry. Returns empty when no entry exists OR when the entry's TTL has elapsed.- Specified by:
getin interfaceIdempotencyStore
-
put
Description copied from interface:IdempotencyStoreStore an entry underkeywith the given TTL. SubsequentIdempotencyStore.get(java.lang.String)calls within the TTL window return this entry. Overwrites any prior entry for the same key.- Specified by:
putin interfaceIdempotencyStore
-
invalidate
Description copied from interface:IdempotencyStoreRemove the entry forkeyif any. No-op if the key is not present.- Specified by:
invalidatein interfaceIdempotencyStore
-
size
public int size()Description copied from interface:IdempotencyStoreApproximate entry count. Intended for monitoring / smoke-testing ("cache has grown to 10M entries, check your TTL"). Implementations may return an eventually-consistent snapshot.- Specified by:
sizein interfaceIdempotencyStore
-