-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
74 lines (56 loc) · 1.69 KB
/
examples_test.go
File metadata and controls
74 lines (56 loc) · 1.69 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
package cache_test
import (
"context"
"fmt"
"hash/fnv"
"io"
"time"
cache "github.com/thewizardplusplus/go-cache"
"github.com/thewizardplusplus/go-cache/gc"
hashmap "github.com/thewizardplusplus/go-hashmap"
)
type StringKey string
func (key StringKey) Hash() int {
hash := fnv.New32()
io.WriteString(hash, string(key)) // nolint: errcheck
return int(hash.Sum32())
}
func (key StringKey) Equals(other hashmap.Key) bool {
return key == other.(StringKey)
}
const (
gcPeriod = time.Millisecond
exampleDelay = gcPeriod * 100
)
func ExampleNewCache() {
storage := hashmap.NewConcurrentHashMap()
gcInstance := gc.NewPartialGC(storage)
go gc.Run(context.Background(), gcInstance, gcPeriod)
timeZones := cache.NewCache(cache.WithStorage(storage))
timeZones.Set(StringKey("EST"), -5*60*60, exampleDelay/2)
timeZones.Set(StringKey("CST"), -6*60*60, exampleDelay/2)
timeZones.Set(StringKey("MST"), -7*60*60, exampleDelay/2)
estOffset, err := timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, err)
time.Sleep(exampleDelay)
estOffset, err = timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, err)
// Output:
// -18000 <nil>
// <nil> key missed
}
func ExampleNewCacheWithGC() {
timeZones :=
cache.NewCacheWithGC(context.Background(), cache.WithGCAndGCPeriod(gcPeriod))
timeZones.Set(StringKey("EST"), -5*60*60, exampleDelay/2)
timeZones.Set(StringKey("CST"), -6*60*60, exampleDelay/2)
timeZones.Set(StringKey("MST"), -7*60*60, exampleDelay/2)
estOffset, err := timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, err)
time.Sleep(exampleDelay)
estOffset, err = timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, err)
// Output:
// -18000 <nil>
// <nil> key missed
}