-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
130 lines (103 loc) · 3.26 KB
/
redis.go
File metadata and controls
130 lines (103 loc) · 3.26 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
130
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/constants"
"github.com/hyp3rd/hypercache/pkg/backend"
"github.com/hyp3rd/hypercache/pkg/backend/redis"
cache "github.com/hyp3rd/hypercache/pkg/cache/v2"
)
const (
cacheCapacity = 20
items = 50
delay = 5 * time.Second
)
//nolint:funlen
func main() {
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultTimeout*2)
defer cancel()
// Create a new Redis backend
redisStore, err := redis.New(
redis.WithAddr("localhost:6379"),
redis.WithPassword("k7oMs2G5bc4mRN45jPZjLBZxuMFrCLahvPn648Zwq1lT41gSYZqapBRnSF2L995FaYcZBz8c7xkKXku94HeReDgdwBu1N4CzgfQ94Z504hjfzrST1u0idVkbXe8ust"),
redis.WithDB(0),
)
if err != nil {
panic(err)
}
conf := &hypercache.Config[backend.Redis]{
BackendType: constants.RedisBackend,
RedisOptions: []backend.Option[backend.Redis]{
backend.WithRedisClient(redisStore.Client),
backend.WithCapacity[backend.Redis](cacheCapacity),
},
HyperCacheOptions: []hypercache.Option[backend.Redis]{
hypercache.WithEvictionInterval[backend.Redis](constants.DefaultEvictionInterval),
},
}
hyperCache, err := hypercache.New(ctx, hypercache.GetDefaultManager(), conf)
if err != nil {
panic(err)
}
fmt.Fprintln(os.Stdout, "setting 50 items to the cache")
for i := range items {
err = hyperCache.Set(ctx, fmt.Sprintf("key-%d", i), fmt.Sprintf("value-%d", i), time.Hour)
if err != nil {
panic(err)
}
}
fmt.Fprintln(os.Stdout, "count", hyperCache.Count(ctx))
fmt.Fprintln(os.Stdout, "capacity", hyperCache.Capacity())
fmt.Fprintln(os.Stdout, "fetching all items (sorted by key, ascending, filtered by value != 'value-16')")
// Apply filters
// Define a filter function
itemsFilterFunc := func(item *cache.Item) bool {
// return time.Since(item.LastAccess) > 1*time.Microsecond
return item.Value != "value-16"
}
sortByFilter := backend.WithSortBy(constants.SortByKey.String())
// sortOrderFilter := backend.WithSortOrderAsc(true)
// Create a filterFuncFilter with the defined filter function
filter := backend.WithFilterFunc(itemsFilterFunc)
// Retrieve the list of items from the cache
allItems, err := hyperCache.List(ctx, sortByFilter, filter)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Fprintln(os.Stdout, "printing all items")
// Print the list of items
for _, item := range allItems {
fmt.Fprintln(os.Stdout, item.Key, item.Value)
}
fmt.Fprintln(os.Stdout, "count", hyperCache.Count(ctx))
fmt.Fprintln(os.Stdout, "capacity", hyperCache.Capacity())
fmt.Fprintln(os.Stdout, "sleep for 5 seconds to trigger eviction")
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
fmt.Fprintln(os.Stdout, "context canceled before eviction")
return
}
fmt.Fprintln(os.Stdout, "fetching all items again")
allItems, err = hyperCache.List(ctx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Fprintln(os.Stdout, "printing all items")
// Print the list of items
for _, item := range allItems {
fmt.Fprintln(os.Stdout, item.Key, item.Value)
}
fmt.Fprintln(os.Stdout, "count", hyperCache.Count(ctx))
value, ok := hyperCache.Get(ctx, "key-49")
if ok {
fmt.Fprintln(os.Stdout, value)
}
}