-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_memory.go
More file actions
119 lines (94 loc) · 2.65 KB
/
Copy pathdriver_memory.go
File metadata and controls
119 lines (94 loc) · 2.65 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
package cache
import (
"context"
"sync"
"time"
)
var _ Driver[string] = (*MemoryDriver[string])(nil)
// MemoryConfig holds configuration for memory driver
type MemoryConfig struct {
DefaultTTL time.Duration
}
// MemoryOption is a function that configures memory driver
type MemoryOption func(*MemoryConfig)
// WithMemoryDefaultTTL sets the default TTL for memory driver
func WithMemoryDefaultTTL(duration time.Duration) MemoryOption {
return func(c *MemoryConfig) {
c.DefaultTTL = duration
}
}
// MemoryDriver implements the Driver interface using in-memory storage
type MemoryDriver[Value any] struct {
storage map[Key]Hit[Value]
mu sync.RWMutex
config MemoryConfig
}
// NewMemoryDriver creates a new memory driver instance
func NewMemoryDriver[Value any](options ...MemoryOption) *MemoryDriver[Value] {
d := &MemoryDriver[Value]{
storage: make(map[Key]Hit[Value]),
config: MemoryConfig{
DefaultTTL: time.Minute,
},
}
for _, option := range options {
option(&d.config)
}
return d
}
// NewMemoryCache creates a new cache instance with a memory driver
// This is a convenience function that combines NewMemoryDriver and NewCache
func NewMemoryCache[Value any](options ...MemoryOption) *Cache[Value] {
driver := NewMemoryDriver[Value](options...)
return NewCache[Value](driver)
}
// Get retrieves a value from memory storage
func (d *MemoryDriver[Value]) Get(_ context.Context, key Key) (Value, bool, error) { //nolint:ireturn
d.mu.RLock()
hit, found := d.storage[key]
d.mu.RUnlock()
var zeroValue Value
if !found {
return zeroValue, false, nil
}
// Check expiration
if !hit.Expiry.IsZero() && time.Now().After(hit.Expiry) {
d.mu.Lock()
delete(d.storage, key)
d.mu.Unlock()
return zeroValue, false, nil
}
return hit.Value, true, nil
}
// Set stores a value in memory storage with the specified TTL
func (d *MemoryDriver[Value]) Set(_ context.Context, key Key, value Value, ttl TTL) error {
var expiry time.Time
switch {
case ttl.useDefault:
expiry = time.Now().Add(d.config.DefaultTTL)
case ttl.duration == 0:
// Zero duration and not using default means no expiration (Forever)
expiry = time.Time{}
default:
expiry = time.Now().Add(ttl.duration)
}
d.mu.Lock()
d.storage[key] = Hit[Value]{
Value: value,
Expiry: expiry,
}
d.mu.Unlock()
return nil
}
// Has checks if a key exists in memory storage
func (d *MemoryDriver[Value]) Has(ctx context.Context, key Key) (bool, error) {
_, found, _ := d.Get(ctx, key)
return found, nil
}
// Delete removes a key from memory storage
func (d *MemoryDriver[Value]) Delete(_ context.Context, key Key) error {
d.mu.Lock()
delete(d.storage, key)
d.mu.Unlock()
return nil
}