Skip to content

Commit 688964b

Browse files
committed
feat: add SetNX operation with Redis synchronization support
1 parent 906cffe commit 688964b

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go-cache is an in-memory key:value store/cache similar to memcached that is suit
1212
* **Redis Integration**: Optional L2 caching and persistence layer using `go-redis` (supports v8 and v9).
1313
* **Capacity Management**: Internal LRU-like eviction when memory limits are reached.
1414
* **Generics**: Type-safe API (Go 1.18+).
15+
* **SetNX**: Atomic "Set if Not Exists" operation, seamlessly synchronized with Redis.
1516
* **Numeric Operations**: Atomic increment/decrement support for numeric types, persisted to Redis.
1617
* **Set Cache**: Track unique members per key, each with its own TTL — ideal for counting active sessions/devices per user.
1718
* **Graceful Shutdown**: Ensures pending Redis operations are completed before exit.
@@ -43,6 +44,11 @@ func main() {
4344

4445
c.Set("foo", "bar", cache.DefaultExpiration)
4546

47+
// SetNX: Only sets the key if it does not already exist.
48+
// Seamlessly integrates with Redis SetNX if configured.
49+
set := c.SetNX("foo", "baz", cache.DefaultExpiration)
50+
fmt.Println(set) // false, since "foo" already exists
51+
4652
foo, found := c.Get("foo")
4753
if found == cache.Found {
4854
fmt.Println(foo) // bar

0 commit comments

Comments
 (0)