generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
92 lines (73 loc) · 2.05 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 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 use a near cache with a NamedMap or NamedCache with high units of 1000.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
)
func main() {
var (
ctx = context.Background()
size int
)
const maxEntries = 2000
// 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()
// near cache options to use for NamedCache
nearCacheOptions := coherence.NearCacheOptions{HighUnits: 1000}
// create a new NamedCache with key of int and value of string which has a
// near cache that will keep up to 1000 units
namedCache, err := coherence.GetNamedCache[int, string](session, "my-near-cache-high-units", coherence.WithNearCache(&nearCacheOptions))
if err != nil {
panic(err)
}
defer namedCache.Release()
err = namedCache.Clear(ctx)
if err != nil {
panic(err)
}
fmt.Println("Adding", maxEntries, "entries")
buffer := make(map[int]string, 0)
for i := 1; i <= maxEntries; i++ {
buffer[i] = fmt.Sprintf("value-%v", i)
}
err = namedCache.PutAll(ctx, buffer)
if err != nil {
panic(err)
}
size, err = namedCache.Size(ctx)
if err != nil {
panic(err)
}
fmt.Printf("Cache size is %v, doing 1000 Get() operations to populate near cache\n\n", size)
for i := 1; i <= 1000; i++ {
_, err = namedCache.Get(ctx, i)
if err != nil {
panic(err)
}
}
// issue another Get() for an entry in the near cache to show a hit
_, err = namedCache.Get(ctx, 1)
if err != nil {
panic(err)
}
fmt.Println(namedCache.GetNearCacheStats())
fmt.Println("\nIssuing another get which will make the near cache over the high-units and it will prune to 80% of size")
fmt.Println()
_, err = namedCache.Get(ctx, 2000)
if err != nil {
panic(err)
}
fmt.Println(namedCache.GetNearCacheStats())
}