generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
76 lines (64 loc) · 2.16 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
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
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
/*
Package main shows how to put entries that expire in a NamedCache using the coherence.WithExpiry option.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"time"
)
func main() {
var (
value *string
ctx = context.Background()
size int
)
// create a new Session to the default gRPC port of 1408 using plain text
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
defer session.Close()
// create a new NamedCache with key of int and value of string.
// NOTE: A NamedCache is required to call the PutWithExpiry function.
namedCache, err := coherence.GetNamedCache[int, string](session, "my-cache", coherence.WithExpiry(time.Duration(5)*time.Second))
if err != nil {
panic(err)
}
fmt.Println("Put key 1, value \"one\" with using Put, default expiry will apply")
// put a new key / value without specifying expiry
if _, err = namedCache.Put(ctx, 1, "one"); err != nil {
panic(err)
}
time.Sleep(time.Duration(6) * time.Second)
if size, err = namedCache.Size(ctx); err != nil {
panic(err)
}
fmt.Printf("Cache size = %d\n", size)
if value, err = namedCache.Get(ctx, 1); err != nil {
panic(err)
}
fmt.Printf("Value for key 1 is %v, should be nil pointer as entry no longer exists\n", value)
// If we do call PutWithExpiry, this expiry value will override the default
fmt.Println("Issue PutWithExpiry key 1, value \"one\" with expiry 10 seconds")
// put a new key / value with expiry of 5 seconds
if _, err = namedCache.PutWithExpiry(ctx, 1, "one", time.Duration(10)*time.Second); err != nil {
panic(err)
}
time.Sleep(time.Duration(6) * time.Second)
if size, err = namedCache.Size(ctx); err != nil {
panic(err)
}
fmt.Printf("After 6 seconds cache size is size = %d, wait another 6 seconds\n", size)
time.Sleep(time.Duration(6) * time.Second)
if size, err = namedCache.Size(ctx); err != nil {
panic(err)
}
fmt.Printf("After another 6 seconds cache size is size = %d\n", size)
}