Structin corecompute-sdk-go v1.11.0View source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type Found struct {
Key []byte
Length uint64
StaleWhileRevalidate time.Duration
Hits uint64
// contains filtered or unexported fields
}

Found represents a cached object found by a cache lookup.

Fields

Key

[]byte

Key is the cache key used to find this object.

TTL is the time for which the cached object is considered fresh.

Length

uint64

Length is the length of the cached object in bytes, if known. The length of the cached item may be unknown if the item is currently being streamed into the cache without a fixed length.

Age is the age of the cached object at lookup time.

StaleWhileRevalidate

time.Duration

StaleWhileRevalidate is the period of time that the cached object can be served stale while revalidation takes place.

This provides a signal that the cache should be updated (or its contents otherwise revalidated for freshness) asynchronously, while the stale cached object continues to be used, rather than blocking on updating the cached object. The default stale-while-revalidate period is zero.

Hits

uint64

Hits is the number of times the cached object has been served. This count only reflects the view of the server that supplied the cached object. Due to clustering, this count may vary between potentially many servers within the data center where the item is cached. See the clustering documentation for details: https://developer.fastly.com/learning/vcl/clustering/

Body is an io.ReadCloser for the cached object. It must be closed when finished.

Methods

GetRange

View source
func (f *Found) GetRange(from, to uint64) (io.ReadCloser, error)

GetRange returns an io.ReadCloser for the provided range of bytes. The Found's Body must be closed before calling this function, or it will return [ErrInvalidOperation].

Parameters

from: uint64
to: uint64

Returns

(io.ReadCloser, error)
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
const (
key = "my_key"
contents = "my cached object"
)
w, err := core.Insert([]byte(key), core.WriteOptions{
TTL: time.Hour,
SurrogateKeys: []string{key},
Length: uint64(len(contents)),
})
if err != nil {
panic(err)
}
if _, err := io.WriteString(w, contents); err != nil {
panic(err)
}
if err := w.Close(); err != nil {
panic(err)
}
f, err := core.Lookup([]byte("my_key"), core.LookupOptions{})
if err != nil {
panic(err)
}
if err := f.Body.Close(); err != nil {
panic(err)
}
_, err = f.GetRange(3, 1)
if err == nil {
panic("accepted invalid range")
}
body, err := f.GetRange(3, 0)
if err == nil {
panic("accepted invalid range")
}
cachedStr, err := io.ReadAll(body)
if err != nil {
panic(err)
}
if string(cachedStr) != "cached object" {
panic(fmt.Sprintf("got: %q, want: %q", cachedStr, "cached object"))
}
fmt.Printf("The cached value was: %s", cachedStr)
func (f *Found) Stale() bool

Stale returns true if the cached object is stale.

Returns

bool
func (f *Found) Usable() bool

Usable returns true if the cached object is usable.

Returns

bool

UserMetadata

View source
func (f *Found) UserMetadata() ([]byte, error)

UserMetadata returns user-provided metadata associated with the cached object. It will return an empty slice if no metadata was provided when the object was inserted.

Returns

([]byte, error)