-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
68 lines (52 loc) · 2.04 KB
/
store.go
File metadata and controls
68 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cachegrid
import "time"
// StorageMode selects the storage backend.
type StorageMode int
const (
// Memory is the default in-memory storage mode with LRU eviction.
Memory StorageMode = iota
// Disk uses PebbleDB for persistent disk-based storage.
Disk
)
// Store is the storage backend interface for CacheGrid.
// Implementations must be safe for concurrent use.
type Store interface {
// Get retrieves the raw serialized value for a key.
// Returns nil, false on miss or expiry.
Get(key string) ([]byte, bool)
// Set stores a serialized value with the given TTL.
// A TTL of 0 means no expiration.
Set(key string, value []byte, ttl time.Duration)
// Delete removes a key. Returns true if the key existed.
Delete(key string) bool
// Exists checks if a key exists and is not expired.
Exists(key string) bool
// TTL returns the remaining time-to-live for a key.
// Returns -1 if no expiry, 0 if missing or expired.
TTL(key string) time.Duration
// Incr atomically increments a counter stored at key by delta.
// If the key does not exist, it is initialized to 0 before incrementing.
// The defaultTTL is applied only when creating a new counter.
Incr(key string, delta int64, defaultTTL time.Duration) (int64, error)
// DeleteExpired removes expired entries. Returns count of removed entries.
DeleteExpired() int
// Len returns the total number of stored items.
Len() int
// Close releases all resources held by the store.
Close() error
// SetOnEvict sets the callback fired when an entry is evicted (e.g., LRU).
SetOnEvict(fn func(key string, value []byte))
// SetOnExpire sets the callback fired when an entry expires.
SetOnExpire(fn func(key string, value []byte))
}
// NewMemory creates a Cache with in-memory storage using sensible defaults.
func NewMemory() (*Cache, error) {
return New(DefaultConfig())
}
// NewDisk creates a Cache with disk-based (Pebble) storage at the given path.
func NewDisk(path string) (*Cache, error) {
config := DefaultConfig()
config.StorageMode = Disk
config.DiskPath = path
return New(config)
}