You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
go-cache is an in-memory key:value store/cache similar to memcached that is
9
-
suitable for applications running on a single machine. Its major advantage is
10
-
that, being essentially a thread-safe `map[string]interface{}` with expiration
11
-
times, it doesn't need to serialize or transmit its contents over the network.
7
+
go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major advantage is that, being essentially a thread-safe `map[string]interface{}` with expiration times, it doesn't need to serialize or transmit its contents over the network.
12
8
13
9
**Key Features:**
10
+
14
11
***Sharding**: Reduces lock contention for high-concurrency workloads.
15
-
***Redis Integration**: Optional L2 caching and persistence layer (fallback on miss, async write).
12
+
***Redis Integration**: Optional L2 caching and persistence layer using `go-redis` (supports v8 and v9).
16
13
***Capacity Management**: Internal LRU-like eviction when memory limits are reached.
17
14
***Generics**: Type-safe API (Go 1.18+).
18
-
19
-
Any object can be stored, for a given duration or forever, and the cache can be
20
-
safely used by multiple goroutines.
21
-
22
-
Although go-cache isn't meant to be used as a persistent datastore, the entire
23
-
cache can be saved to and loaded from a file (using `c.Items()` to retrieve the
24
-
items map to serialize, and `NewFrom()` to create a cache from a deserialized
25
-
one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats.)
15
+
***Numeric Operations**: Atomic increment/decrement support for numeric types, persisted to Redis.
16
+
***Graceful Shutdown**: Ensures pending Redis operations are completed before exit.
17
+
***Sync**: Force refresh items from Redis.
26
18
27
19
### Installation
28
20
29
21
`go get github.com/mysamimi/go-cache/v3`
30
22
31
23
### Usage
32
24
25
+
#### Basic Cache
26
+
33
27
```go
34
28
import (
35
29
"fmt"
@@ -45,11 +39,6 @@ func main() {
45
39
// Set the value of the key "foo" to "bar", with the default expiration time
46
40
c.Set("foo", "bar", cache.DefaultExpiration)
47
41
48
-
// Set the value of the key "baz" to "yes", with no expiration time
49
-
// (the item won't be removed until it is re-set, or removed using
50
-
// c.Delete("baz")
51
-
c.Set("baz", "yes", cache.NoExpiration)
52
-
53
42
// Get the string associated with the key "foo" from the cache
54
43
foo, found:= c.Get("foo")
55
44
if found == cache.Found {
@@ -58,46 +47,87 @@ func main() {
58
47
}
59
48
```
60
49
61
-
###Redis Integration (L2 Cache)
50
+
#### Sharded Cache (Recommended for High Concurrency)
62
51
63
-
You can configure a Redis client to act as a Level 2 cache and persistent store. Writes are asynchronous to Redis, while reads fallback to Redis if the key is missing from memory.
52
+
`ShardedCache` automatically partitions keys into multiple buckets to reduce lock contention.
You can configure a Redis client to act as a Level 2 cache and persistent store. Writes (Set/Delete/ModifyNumeric) are asynchronous to Redis, while reads look up Redis if the key is missing from memory.
77
65
78
-
You can set a maximum number of items for the local in-memory cache.
66
+
Supports both `go-redis/v8` and `go-redis/v9` via adapters.
// Ensures all pending Redis writes are flushed before exit
93
+
defer c.Close()
94
+
}
82
95
```
83
96
84
-
**Bulk Eviction Strategy**: When the cache reaches the configured capacity, it automatically triggers a bulk eviction, removing random items until the cache size drops to **75%** of the capacity. This prevents "thrashing" (constant delete/add cycles) under heavy load.
97
+
#### Sync from Redis
98
+
99
+
If you know an item has changed in Redis (e.g. by another service) and want to refresh the local cache immediately:
85
100
101
+
```go
102
+
val, err:= c.Sync("key")
103
+
```
86
104
87
-
###ShardedCache
105
+
#### Capacity & Aviction
88
106
89
-
For high concurrency scenarios, you can use `ShardedCache` to reduce lock contention. `ShardedCache` automatically partitions keys into multiple buckets.
107
+
Set a maximum number of items for the local cache. When the limit is reached, items are evicted (approx 25% of cache is cleared) to prevent thrashing.
0 commit comments