-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyvalue.go
More file actions
129 lines (108 loc) · 5.45 KB
/
keyvalue.go
File metadata and controls
129 lines (108 loc) · 5.45 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package gpa
import (
"context"
"time"
)
// =====================================
// Key-Value Repository Interfaces
// =====================================
// BasicKeyValueRepository provides basic key-value storage operations.
// Suitable for simple caching and key-value storage scenarios.
type BasicKeyValueRepository[T any] interface {
Repository[T]
// Set stores a value with the given key.
// Overwrites any existing value at that key.
// Example: err := Set(ctx, "user:123", user)
Set(ctx context.Context, key string, value *T) error
// Get retrieves a value by key.
// Returns the value with compile-time type safety.
// Returns ErrorTypeNotFound if the key doesn't exist.
// Example: user, err := Get(ctx, "user:123")
Get(ctx context.Context, key string) (*T, error)
// DeleteKey removes a key-value pair.
// Returns ErrorTypeNotFound if the key doesn't exist.
// Example: err := DeleteKey(ctx, "user:123")
DeleteKey(ctx context.Context, key string) error
// KeyExists checks if a key exists.
// Returns true if the key exists, false otherwise.
// Example: exists, err := KeyExists(ctx, "user:123")
KeyExists(ctx context.Context, key string) (bool, error)
}
// BatchKeyValueRepository extends BasicKeyValueRepository with batch operations.
// Provides efficient bulk operations for better performance.
type BatchKeyValueRepository[T any] interface {
BasicKeyValueRepository[T]
// MSet sets multiple key-value pairs in a single operation.
// More efficient than multiple individual Set calls.
// Example: err := MSet(ctx, map[string]*T{"user:1": user1, "user:2": user2})
MSet(ctx context.Context, pairs map[string]*T) error
// MGet retrieves multiple values by their keys.
// Returns a map of key-value pairs with compile-time type safety.
// Missing keys are omitted from the result map.
// Example: users, err := MGet(ctx, []string{"user:1", "user:2", "user:3"})
MGet(ctx context.Context, keys []string) (map[string]*T, error)
// MDelete removes multiple keys in a single operation.
// More efficient than multiple individual DeleteKey calls.
// Returns the number of keys that were actually deleted.
// Example: deleted, err := MDelete(ctx, []string{"user:1", "user:2"})
MDelete(ctx context.Context, keys []string) (int64, error)
}
// TTLKeyValueRepository extends BasicKeyValueRepository with TTL (Time To Live) support.
// Values automatically expire after a specified duration.
type TTLKeyValueRepository[T any] interface {
BasicKeyValueRepository[T]
// SetWithTTL stores a value with an expiration time.
// The value will be automatically deleted after the TTL expires.
// Example: err := SetWithTTL(ctx, "session:abc123", session, 30*time.Minute)
SetWithTTL(ctx context.Context, key string, value *T, ttl time.Duration) error
// GetTTL returns the remaining time-to-live for a key.
// Returns 0 if the key doesn't exist or has no TTL.
// Example: ttl, err := GetTTL(ctx, "session:abc123")
GetTTL(ctx context.Context, key string) (time.Duration, error)
// SetTTL sets or updates the TTL for an existing key.
// Returns ErrorTypeNotFound if the key doesn't exist.
// Example: err := SetTTL(ctx, "session:abc123", 15*time.Minute)
SetTTL(ctx context.Context, key string, ttl time.Duration) error
// RemoveTTL removes the TTL from a key, making it persistent.
// The key will no longer expire automatically.
// Example: err := RemoveTTL(ctx, "permanent:key")
RemoveTTL(ctx context.Context, key string) error
}
// IncrementKeyValueRepository provides atomic increment/decrement operations.
// Useful for counters, statistics, and rate limiting.
type IncrementKeyValueRepository interface {
// Increment atomically increments a numeric value.
// Creates the key with value 0 if it doesn't exist, then adds the delta.
// Returns the new value after incrementing.
// Example: newValue, err := Increment(ctx, "counter:visits", 1)
Increment(ctx context.Context, key string, delta int64) (int64, error)
// Decrement atomically decrements a numeric value.
// Creates the key with value 0 if it doesn't exist, then subtracts the delta.
// Returns the new value after decrementing.
// Example: newValue, err := Decrement(ctx, "counter:items", 1)
Decrement(ctx context.Context, key string, delta int64) (int64, error)
}
// PatternKeyValueRepository provides pattern-based key operations.
// Useful for finding keys that match certain patterns.
type PatternKeyValueRepository interface {
// Keys returns all keys matching the given pattern.
// Uses glob-style patterns (*, ?, [abc], etc.).
// WARNING: Can be slow with large datasets - use with caution.
// Example: keys, err := Keys(ctx, "user:*")
Keys(ctx context.Context, pattern string) ([]string, error)
// Scan iterates over keys matching a pattern using cursor-based pagination.
// More efficient than Keys() for large datasets as it doesn't load all keys at once.
// Returns matching keys and a cursor for the next iteration.
// Example: keys, cursor, err := Scan(ctx, 0, "user:*", 10)
Scan(ctx context.Context, cursor uint64, pattern string, count int64) ([]string, uint64, error)
}
// AdvancedKeyValueRepository combines all key-value capabilities.
// Provides the full spectrum of key-value operations including batching, TTL, atomics, and patterns.
// Only the most advanced KV stores (Redis, Hazelcast) implement this complete interface.
type AdvancedKeyValueRepository[T any] interface {
BasicKeyValueRepository[T]
BatchKeyValueRepository[T]
TTLKeyValueRepository[T]
IncrementKeyValueRepository
PatternKeyValueRepository
}