generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
91 lines (72 loc) · 1.97 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
/*
* Copyright (c) 2023, 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 listen for all lifecycle events on a NamedMap or NamedCache.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"time"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
func (p Person) String() string {
return fmt.Sprintf("Person{id=%d, name=%s, age=%d}", p.ID, p.Name, p.Age)
}
func main() {
var (
size int
ctx = context.Background()
)
// create a new Session
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
defer session.Close()
// create a new NamedMap of Person with key int
namedMap, err := coherence.GetNamedMap[int, Person](session, "people")
if err != nil {
panic(err)
}
// clear the Map
if err = namedMap.Clear(ctx); err != nil {
panic(err)
}
// Create a listener and add to the cache
listener := coherence.NewMapLifecycleListener[int, Person]().OnAny(func(e coherence.MapLifecycleEvent[int, Person]) {
fmt.Printf("**EVENT=%s: source=%v\n", e.Type(), e.Source())
})
namedMap.AddLifecycleListener(listener)
defer namedMap.RemoveLifecycleListener(listener)
newPerson := Person{ID: 1, Name: "Tim", Age: 21}
fmt.Println("Add new Person", newPerson)
if _, err = namedMap.Put(ctx, newPerson.ID, newPerson); err != nil {
panic(err)
}
if size, err = namedMap.Size(ctx); err != nil {
panic(err)
}
fmt.Println("Cache size is", size, ", truncate cache")
if err = namedMap.Truncate(ctx); err != nil {
panic(err)
}
sleep("Sleeping to ensure we see event")
fmt.Println("Cache size is", size, "destroy cache")
if err = namedMap.Destroy(ctx); err != nil {
panic(err)
}
sleep("Sleeping to ensure we see events")
}
func sleep(msg string) {
fmt.Println(msg)
time.Sleep(time.Duration(5) * time.Second)
}