-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
51 lines (39 loc) · 1.21 KB
/
cache.go
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
package cache
import (
"time"
)
// Cache interface defines the methods for interacting with the cache.
type Cache interface {
// Set stores the given value in the cache with the specified expiration time.
Set(key string, value interface{}, expiration time.Duration) error
// Get retrieves the value from the cache with the specified key.
Get(key string) (*CacheItem, error)
// Delete removes the value from the cache with the specified key.
Delete(key string) error
// Flush removes all values from the cache.
Flush() error
// Has checks if the cache has the specified key.
Has(key string) bool
}
// CacheItem represents a single cache item.
type CacheItem struct {
Key string `json:"key"`
Value interface{} `json:"value"`
Expiration time.Duration `json:"expiration"`
CreatedAt time.Time `json:"created_at"`
}
func (c *CacheItem) Expired() bool {
return time.Now().After(c.CreatedAt.Add(c.Expiration))
}
func (c *CacheItem) ExpireIn(duration time.Duration) {
c.Expiration = duration
}
func (c *CacheItem) ExpireAt(t time.Time) {
c.Expiration = time.Since(t)
}
func (c *CacheItem) Set(value interface{}) {
c.Value = value
}
func (c *CacheItem) Get() interface{} {
return c.Value
}