-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (40 loc) · 1.04 KB
/
main.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
package main
import (
"fmt"
"github.com/sprootik/inMemeory_cache"
"sync"
"time"
)
func main() {
// initiate a new cache with an item lifetime of 1 seconds
c := cache.NewCache(1000, 1*time.Second)
// add 1001 elements in cache.
// The cache will only have the last 1000 elements since the capacity is 1000
var wg sync.WaitGroup
for i := 0; i < 1001; i++ {
wg.Add(1)
go func() {
key := fmt.Sprintf("key-%d", i)
value := fmt.Sprintf("value-%d", i)
c.Add(key, value)
wg.Done()
}()
}
wg.Wait()
// show cache size
fmt.Printf("c size: %d\n", c.CacheSize())
// get value from cache by key. After second.
// after a second, the elements are considered not relevant, since the lifetime is 1 second.
// The element will be removed from the cache upon request
for i := 0; i < 2; i++ {
key := "key-666"
value, ok := c.Get(key)
if !ok {
fmt.Println("key not found")
} else {
fmt.Printf("key: %s found in cache, value: %s\n", key, value)
}
fmt.Printf("c size: %d\n", c.CacheSize())
time.Sleep(1 * time.Second)
}
}