-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
67 lines (56 loc) · 1.51 KB
/
get.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/hyp3rd/hypercache"
)
func main() {
// Create a new HyperCache with a capacity of 10
cache, err := hypercache.NewInMemoryWithDefaults(10)
if err != nil {
fmt.Println(err)
return
}
// Stop the cache when the program exits
defer cache.Stop()
log.Println("adding items to the cache")
// Add 10 items to the cache
for i := 0; i < 10; i++ {
key := fmt.Sprintf("key%d", i)
val := fmt.Sprintf("val%d", i)
err = cache.Set(context.TODO(), key, val, time.Minute)
if err != nil {
fmt.Printf("unexpected error: %v\n", err)
return
}
}
log.Println("fetching items from the cache using the `GetMultiple` method, key11 does not exist")
// Retrieve the specific of items from the cache
items, errs := cache.GetMultiple(context.TODO(), "key1", "key7", "key9", "key11")
// Print the errors if any
for k, e := range errs {
log.Printf("error fetching item %s: %s\n", k, e)
}
// Print the items
for k, v := range items {
fmt.Println(k, v)
}
log.Println("fetching items from the cache using the `GetOrSet` method")
// Retrieve a specific of item from the cache
// If the item is not found, set it and return the value
val, err := cache.GetOrSet(context.TODO(), "key11", "val11", time.Minute)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(val)
log.Println("fetching items from the cache using the simple `Get` method")
item, ok := cache.Get("key7")
if !ok {
fmt.Println("item not found")
return
}
fmt.Println(item)
}