2023-04-26 12:57:36 +09:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall/js"
|
|
|
|
|
|
|
|
"github.com/syumai/workers/internal/jsutil"
|
|
|
|
)
|
|
|
|
|
2024-01-04 22:45:03 +09:00
|
|
|
var cache = js.Global().Get("caches")
|
2023-04-26 12:57:36 +09:00
|
|
|
|
|
|
|
// Cache
|
|
|
|
type Cache struct {
|
|
|
|
// instance - The object that Cache API belongs to.
|
|
|
|
instance js.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyOptions applies client options.
|
|
|
|
func (c *Cache) applyOptions(opts []CacheOption) {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheOption
|
|
|
|
type CacheOption func(*Cache)
|
|
|
|
|
|
|
|
// WithNamespace
|
|
|
|
func WithNamespace(namespace string) CacheOption {
|
|
|
|
return func(c *Cache) {
|
|
|
|
v, err := jsutil.AwaitPromise(cache.Call("open", namespace))
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to open cache")
|
|
|
|
}
|
|
|
|
c.instance = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opts ...CacheOption) *Cache {
|
|
|
|
c := &Cache{
|
|
|
|
instance: cache.Get("default"),
|
|
|
|
}
|
|
|
|
c.applyOptions(opts)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|