@@ -20,57 +20,60 @@ go version devel go1.18-c2397905e0 Sat Nov 13 03:33:55 2021 +0000 darwin/arm64
20
20
21
21
## Install
22
22
23
- go get github.com/Code-Hex/go-generics-cache
23
+ $ go get github.com/Code-Hex/go-generics-cache
24
24
25
25
## Usage
26
26
27
27
See also [ examples] ( https://github.com/Code-Hex/go-generics-cache/blob/main/example_test.go )
28
28
29
+ playground: https://gotipplay.golang.org/p/FXRk6ngYV-s
30
+
29
31
``` go
30
32
package main
31
33
32
34
import (
33
35
" fmt"
36
+ " time"
34
37
35
38
cache " github.com/Code-Hex/go-generics-cache"
36
39
)
37
40
38
41
func main () {
39
- // Create a cache. key as string, value as int.
40
- c1 := cache.New [string , int ]()
42
+ // Create a cache. key as string, value as int.
43
+ c1 := cache.New [string , int ]()
41
44
42
- // Sets the value of int. you can set with expiration option.
43
- c1.Set (" foo" , 1 , cache.WithExpiration (time.Hour ))
45
+ // Sets the value of int. you can set with expiration option.
46
+ c1.Set (" foo" , 1 , cache.WithExpiration (time.Hour ))
44
47
45
- // the value never expires.
46
- c1.Set (" bar" , 2 )
48
+ // the value never expires.
49
+ c1.Set (" bar" , 2 )
47
50
48
- foo , ok := c .Get (" foo" )
51
+ foo , ok := c1 .Get (" foo" )
49
52
if ok {
50
53
fmt.Println (foo) // 1
51
54
}
52
55
53
- fmt.Println (c .Keys ()) // outputs "foo" "bar" may random
56
+ fmt.Println (c1 .Keys ()) // outputs "foo" "bar" may random
54
57
55
- // Create a cache. key as int, value as string.
56
- c2 := cache.New [int , string ]()
57
- c2.Set (1 , " baz" )
58
- baz , ok := c .Get (1 )
58
+ // Create a cache. key as int, value as string.
59
+ c2 := cache.New [int , string ]()
60
+ c2.Set (1 , " baz" )
61
+ baz , ok := c2 .Get (1 )
59
62
if ok {
60
63
fmt.Println (baz) // "baz"
61
64
}
62
65
63
66
// Create a cache for Number constraint.. key as string, value as int.
64
67
nc := cache.NewNumber [string , int ]()
65
- nc.Set (" age" , 26 )
68
+ nc.Set (" age" , 26 )
66
69
67
- // This will be compile error, because string is not satisfied cache.Number constraint.
68
- // nc := cache.NewNumber[string, string]()
70
+ // This will be compile error, because string is not satisfied cache.Number constraint.
71
+ // nc := cache.NewNumber[string, string]()
69
72
70
- incremented , _ := nc.Increment (" age" , 1 )
71
- fmt.Println (incremented) // 27
73
+ incremented , _ := nc.Increment (" age" , 1 )
74
+ fmt.Println (incremented) // 27
72
75
73
- decremented , _ := nc.Decrement (" age" , 1 )
74
- fmt.Println (decremented) // 26
76
+ decremented , _ := nc.Decrement (" age" , 1 )
77
+ fmt.Println (decremented) // 26
75
78
}
76
79
```
0 commit comments