Skip to content

Commit 8d4d09f

Browse files
committed
add some simple core cache API examples
1 parent 227a4ae commit 8d4d09f

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

cache/core/core_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package core_test
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"time"
7+
8+
"github.com/fastly/compute-sdk-go/cache/core"
9+
)
10+
11+
func ExampleLookup() {
12+
// f is a core.Found value, representing a found cache item.
13+
// core.ErrNotFound is returned if the item is not cached.
14+
f, err := core.Lookup([]byte("my_key"), core.LookupOptions{})
15+
if err != nil {
16+
panic(err)
17+
}
18+
defer f.Body.Close()
19+
20+
// The contents of the cached item are in the Found's Body field.
21+
cachedStr, err := io.ReadAll(f.Body)
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
fmt.Printf("The cached value was: %s", cachedStr)
27+
}
28+
29+
func ExampleInsert() {
30+
const (
31+
key = "my_key"
32+
contents = "my cached object"
33+
)
34+
35+
// w is a core.WriteCloseAbandoner, a superset of io.WriteCloser.
36+
// Data written to this handle is streamed into the Fastly cache.
37+
w, err := core.Insert([]byte(key), core.WriteOptions{
38+
TTL: time.Hour,
39+
SurrogateKeys: []string{key},
40+
Length: uint64(len(contents)),
41+
})
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
if _, err := io.WriteString(w, contents); err != nil {
47+
panic(err)
48+
}
49+
50+
// The writer must be closed to complete the cache operation.
51+
// Always check for errors from Close.
52+
if err := w.Close(); err != nil {
53+
panic(err)
54+
}
55+
}
56+
57+
func ExampleTransaction() {
58+
// Users of the transactional API should at a minimum anticipate
59+
// lookups that are obligated to insert an object into the cache,
60+
// and lookups which are not. If the stale-while-revalidate
61+
// parameter is set for a cached object, the user should also
62+
// distinguish between the insertion and revalidation cases.
63+
64+
useFoundItem := func(f *core.Found) {
65+
// Do something with the found item
66+
}
67+
68+
buildContents := func() []byte {
69+
// Build the contents of the cached item
70+
return []byte("hello world!")
71+
}
72+
73+
shouldReplace := func(f *core.Found, contents []byte) bool {
74+
// Determine whether the cached item should be replaced with
75+
// the new contents
76+
return true
77+
}
78+
79+
tx, err := core.NewTransaction([]byte("my_key"), core.LookupOptions{})
80+
if err != nil {
81+
panic(err)
82+
}
83+
defer tx.Close()
84+
85+
// f is a core.Found value, representing a found cache item.
86+
// core.ErrNotFound is returned if the item is not cached.
87+
f, err := tx.Found()
88+
switch err {
89+
case nil:
90+
// A cached item was found, though it might be stale.
91+
useFoundItem(f)
92+
93+
// Perform revalidation, if necessary.
94+
if tx.MustInsertOrUpdate() {
95+
contents := buildContents()
96+
if shouldReplace(f, contents) {
97+
// Use Insert to replace the previous object
98+
w, err := tx.Insert(core.WriteOptions{
99+
TTL: time.Hour,
100+
SurrogateKeys: []string{"my_key"},
101+
Length: uint64(len(contents)),
102+
})
103+
if err != nil {
104+
panic(err)
105+
}
106+
107+
if _, err := w.Write(contents); err != nil {
108+
panic(err)
109+
}
110+
111+
if err := w.Close(); err != nil {
112+
panic(err)
113+
}
114+
} else {
115+
// Otherwise update the stale object's metadata
116+
if err := tx.Update(core.WriteOptions{
117+
TTL: time.Hour,
118+
SurrogateKeys: []string{"my_key"},
119+
}); err != nil {
120+
panic(err)
121+
}
122+
}
123+
}
124+
125+
case core.ErrNotFound:
126+
// The item was not found.
127+
if tx.MustInsert() {
128+
// We've been chosen to insert the object.
129+
contents := buildContents()
130+
w, f, err := tx.InsertAndStreamBack(core.WriteOptions{
131+
TTL: time.Hour,
132+
SurrogateKeys: []string{"my_key"},
133+
Length: uint64(len(contents)),
134+
})
135+
if err != nil {
136+
panic(err)
137+
}
138+
139+
if _, err := w.Write(contents); err != nil {
140+
panic(err)
141+
}
142+
143+
if err := w.Close(); err != nil {
144+
panic(err)
145+
}
146+
147+
useFoundItem(f)
148+
} else {
149+
panic(err)
150+
}
151+
152+
default:
153+
// An unexpected error
154+
panic(err)
155+
}
156+
}

0 commit comments

Comments
 (0)