Documentation
¶
Index ¶
- func WithValidator[K comparable, V any](validator trcache.Validator[V]) trcache.RootOption
- type Cache
- func (c *Cache[K, V]) Delete(ctx context.Context, key K, options ...trcache.DeleteOption) error
- func (c *Cache[K, V]) Get(ctx context.Context, key K, options ...trcache.GetOption) (V, error)
- func (c *Cache[K, V]) Handle() map[K]V
- func (c *Cache[K, V]) Name() string
- func (c *Cache[K, V]) Set(ctx context.Context, key K, value V, options ...trcache.SetOption) error
- type RefreshCache
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithValidator ¶
func WithValidator[K comparable, V any](validator trcache.Validator[V]) trcache.RootOption
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Example ¶
package main
import (
"context"
"fmt"
trmap "github.com/rrgmc/trcache/cache/map"
)
func main() {
ctx := context.Background()
cache, err := trmap.NewDefault[string, string]()
err = cache.Set(ctx, "a", "12")
if err != nil {
panic(err)
}
v, err := cache.Get(ctx, "a")
if err != nil {
panic(err)
}
fmt.Println(v)
err = cache.Delete(ctx, "a")
if err != nil {
panic(err)
}
_, err = cache.Get(ctx, "a")
fmt.Println(err)
}
Output: 12 not found
func New ¶
func New[K comparable, V any](cache map[K]V, options ...trcache.RootOption) (*Cache[K, V], error)
func NewDefault ¶
func NewDefault[K comparable, V any](options ...trcache.RootOption) (*Cache[K, V], error)
type RefreshCache ¶
type RefreshCache[K comparable, V any] struct { *Cache[K, V] // contains filtered or unexported fields }
Example ¶
package main
import (
"context"
"fmt"
"github.com/rrgmc/trcache"
trmap "github.com/rrgmc/trcache/cache/map"
)
func main() {
ctx := context.Background()
cache, err := trmap.NewRefreshDefault[string, string](
trcache.WithDefaultRefreshFunc[string, string](func(ctx context.Context, key string, options trcache.RefreshFuncOptions) (string, error) {
return fmt.Sprintf("abc%d", options.Data), nil
}),
)
if err != nil {
panic(err)
}
err = cache.Set(ctx, "a", "12")
if err != nil {
panic(err)
}
v, err := cache.Get(ctx, "a")
if err != nil {
panic(err)
}
fmt.Println(v)
err = cache.Delete(ctx, "a")
if err != nil {
panic(err)
}
_, err = cache.Get(ctx, "a")
fmt.Println(err)
v, err = cache.GetOrRefresh(ctx, "b", trcache.WithRefreshData[string, string](123))
if err != nil {
panic(err)
}
fmt.Println(v)
}
Output: 12 not found abc123
func NewRefresh ¶
func NewRefresh[K comparable, V any](cache map[K]V, options ...trcache.RootOption) (*RefreshCache[K, V], error)
func NewRefreshDefault ¶
func NewRefreshDefault[K comparable, V any](options ...trcache.RootOption) (*RefreshCache[K, V], error)
func (*RefreshCache[K, V]) GetOrRefresh ¶
func (c *RefreshCache[K, V]) GetOrRefresh(ctx context.Context, key K, options ...trcache.RefreshOption) (V, error)
Click to show internal directories.
Click to hide internal directories.
