Skip to content

Commit 227a4ae

Browse files
committed
implement the purge API
Currently this only supports surrogate key purging.
1 parent 013a88c commit 227a4ae

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

_examples/corecache/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/fastly/compute-sdk-go/cache/core"
1414
"github.com/fastly/compute-sdk-go/fsthttp"
15+
"github.com/fastly/compute-sdk-go/purge"
1516
)
1617

1718
func main() {
@@ -115,8 +116,11 @@ func main() {
115116

116117
// Purge the key from the cache.
117118
case "DELETE":
118-
// TODO: purge the surrogate key.
119-
w.WriteHeader(fsthttp.StatusNotImplemented)
119+
if err := purge.PurgeSurrogateKey(hex.EncodeToString(key), purge.PurgeOptions{}); err != nil {
120+
fsthttp.Error(w, err.Error(), fsthttp.StatusInternalServerError)
121+
return
122+
}
123+
w.WriteHeader(fsthttp.StatusAccepted)
120124

121125
default:
122126
w.WriteHeader(fsthttp.StatusMethodNotAllowed)

internal/abi/fastly/hostcalls_guest.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,3 +2908,38 @@ func (c *CacheEntry) Hits() (uint64, error) {
29082908

29092909
return uint64(d), nil
29102910
}
2911+
2912+
type PurgeOptions struct {
2913+
mask purgeOptionsMask
2914+
opts purgeOptions
2915+
}
2916+
2917+
func (o *PurgeOptions) SoftPurge(v bool) {
2918+
if v {
2919+
o.mask |= purgeOptionsMaskSoftPurge
2920+
} else {
2921+
o.mask &^= purgeOptionsMaskSoftPurge
2922+
}
2923+
}
2924+
2925+
// witx:
2926+
//
2927+
// (@interface func (export "purge_surrogate_key")
2928+
// (param $surrogate_key string)
2929+
// (param $options_mask $purge_options_mask)
2930+
// (param $options (@witx pointer $purge_options))
2931+
// (result $err (expected (error $fastly_status)))
2932+
// )
2933+
//
2934+
//go:wasm-module fastly_purge
2935+
//export purge_surrogate_key
2936+
//go:noescape
2937+
func fastlyPurgeSurrogateKey(surrogateKey prim.Wstring, mask purgeOptionsMask, opts *purgeOptions) FastlyStatus
2938+
2939+
func PurgeSurrogateKey(surrogateKey string, opts PurgeOptions) error {
2940+
return fastlyPurgeSurrogateKey(
2941+
prim.NewReadBufferFromString(surrogateKey).Wstring(),
2942+
opts.mask,
2943+
&opts.opts,
2944+
).toError()
2945+
}

internal/abi/fastly/hostcalls_noguest.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,13 @@ func (c *CacheEntry) Age() (time.Duration, error) {
399399
func (c *CacheEntry) Hits() (uint64, error) {
400400
return 0, fmt.Errorf("not implemented")
401401
}
402+
403+
type PurgeOptions struct{}
404+
405+
func (o *PurgeOptions) SoftPurge(v bool) error {
406+
return fmt.Errorf("not implemented")
407+
}
408+
409+
func PurgeSurrogateKey(surrogateKey string, opts PurgeOptions) error {
410+
return fmt.Errorf("not implemented")
411+
}

internal/abi/fastly/types.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,34 @@ const (
639639
CacheLookupStateStale CacheLookupState = 0b0000_0100 // $stale
640640
CacheLookupStateMustInsertOrUpdate CacheLookupState = 0b0000_1000 // $must_insert_or_update
641641
)
642+
643+
// witx:
644+
//
645+
// (typename $purge_options_mask
646+
// (flags (@witx repr u32)
647+
// $soft_purge
648+
// $ret_buf ;; all ret_buf fields must be populated
649+
// )
650+
// )
651+
type purgeOptionsMask prim.U32
652+
653+
const (
654+
purgeOptionsMaskSoftPurge purgeOptionsMask = 1 << 0 // $soft_purge
655+
purgeOptionsMaskRetBuf purgeOptionsMask = 1 << 1 // $ret_buf
656+
)
657+
658+
// witx:
659+
//
660+
// (typename $purge_options
661+
// (record
662+
// ;; JSON purge response as in https://developer.fastly.com/reference/api/purging/#purge-tag
663+
// (field $ret_buf_ptr (@witx pointer u8))
664+
// (field $ret_buf_len (@witx usize))
665+
// (field $ret_buf_nwritten_out (@witx pointer (@witx usize)))
666+
// )
667+
// )
668+
type purgeOptions struct {
669+
retBufPtr *prim.U8
670+
retBufLen prim.Usize
671+
retBufNwrittenOut *prim.Usize
672+
}

purge/purge.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Package purge provides cache purging operations for Fastly
2+
// Compute@Edge.
3+
//
4+
// See the [Fastly purge documentation] for details.
5+
//
6+
// [Fastly purge documentation]: https://developer.fastly.com/learning/concepts/purging/
7+
package purge
8+
9+
import "github.com/fastly/compute-sdk-go/internal/abi/fastly"
10+
11+
// PurgeOptions control the behavior of purge operations.
12+
type PurgeOptions struct {
13+
// Whether to soft purge the item. A soft purge marks a cached
14+
// object as stale, rather than invalidating it.
15+
Soft bool
16+
}
17+
18+
// PurgeSurrogateKey purges all cached objects with the provided
19+
// surrogate key.
20+
func PurgeSurrogateKey(surrogateKey string, opts PurgeOptions) error {
21+
var abiOpts fastly.PurgeOptions
22+
abiOpts.SoftPurge(opts.Soft)
23+
24+
return fastly.PurgeSurrogateKey(surrogateKey, abiOpts)
25+
}

0 commit comments

Comments
 (0)