Documentation
¶
Index ¶
- func RegisteBackend(configBackend string, backendFunc func() CacheBackend) error
- func WithMakeCacheKey(f makeCacheKeyFunc) cacheOption
- func WithTTL(ttl time.Duration) cacheOption
- func WithVersion(version int64) cacheOption
- type CacheBackend
- type CacheExt
- func (c *CacheExt) Application() *gobay.Application
- func (c *CacheExt) Cached(funcName string, f cachedFunc, options ...cacheOption) *CachedConfig
- func (c *CacheExt) CheckHealth(ctx context.Context) error
- func (c *CacheExt) Close() error
- func (c *CacheExt) Delete(ctx context.Context, key string) bool
- func (c *CacheExt) DeleteMany(ctx context.Context, keys ...string) bool
- func (c *CacheExt) Exists(ctx context.Context, key string) bool
- func (c *CacheExt) Expire(ctx context.Context, key string, ttl time.Duration) bool
- func (c *CacheExt) Get(ctx context.Context, key string, m interface{}) (bool, error)
- func (c *CacheExt) GetMany(ctx context.Context, out map[string]interface{}) error
- func (c *CacheExt) Init(app *gobay.Application) error
- func (c *CacheExt) Object() interface{}
- func (c *CacheExt) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *CacheExt) SetMany(ctx context.Context, keyValues map[string]interface{}, ttl time.Duration) error
- func (c *CacheExt) TTL(ctx context.Context, key string) time.Duration
- type CachedConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisteBackend ¶
func RegisteBackend(configBackend string, backendFunc func() CacheBackend) error
RegisteBackend if you want a new backend, use this func to registe your backend then load it by config
func WithMakeCacheKey ¶
func WithMakeCacheKey(f makeCacheKeyFunc) cacheOption
WithMakeCacheKey you can write your own makeCacheKey, use this func to change the default makeCacheKey. first param means funcName, the second param means version, next params mean real function input param.
func WithVersion ¶
func WithVersion(version int64) cacheOption
WithVersion set version to the cacheFuncConfig object, if you want a function's all cache update immediately, change the version.
Types ¶
type CacheBackend ¶
type CacheBackend interface {
Init(*viper.Viper) error
Get(context.Context, string) ([]byte, error) // if record not exist, return (nil, nil)
Set(context.Context, string, []byte, time.Duration) error
SetMany(context.Context, map[string][]byte, time.Duration) error
GetMany(context.Context, []string) [][]byte // if record not exist, use nil instead
Delete(context.Context, string) bool
DeleteMany(context.Context, []string) bool
Expire(context.Context, string, time.Duration) bool
TTL(context.Context, string) time.Duration
Exists(context.Context, string) bool
Close() error
CheckHealth(context.Context) error
}
CacheBackend
type CacheExt ¶
type CacheExt struct {
NS string
// contains filtered or unexported fields
}
CacheExt
func (*CacheExt) Cached ¶
func (c *CacheExt) Cached(funcName string, f cachedFunc, options ...cacheOption) *CachedConfig
Cached return a ptr with two function: MakeCacheKey and GetResult
Example ¶
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/apocelipes/gobay"
"github.com/apocelipes/gobay/extensions/cachext"
_ "github.com/apocelipes/gobay/extensions/cachext/backend/memory"
)
func main() {
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
if _, err := gobay.CreateApp("../../testdata/", "testing", exts); err != nil {
fmt.Println(err)
return
}
var call_times = 0
var err error
f := func(_ context.Context, keys []string, arg []int64) (interface{}, error) {
call_times += 1
res := make([]string, 2)
res[0] = keys[0]
res[1] = keys[0]
return res, nil
}
cachedFunc := cache.Cached("f", f, cachext.WithTTL(10*time.Second), cachext.WithVersion(1), cachext.WithMakeCacheKey(
func(funcName string, version int64, strArgs []string, intArgs []int64) string {
return strings.Join(strArgs, "_")
},
))
zero_res := make([]string, 2)
for i := 0; i <= 1; i++ {
if err := cachedFunc.GetResult(context.Background(), &zero_res, []string{"hello", "world"}, []int64{}); err != nil {
fmt.Println("Cache set Failed")
}
}
err = cachedFunc.GetResult(context.Background(), &zero_res, []string{"hello", "world"}, []int64{})
fmt.Println(zero_res, call_times, err)
}
Output: [hello hello] 1 <nil>
func (*CacheExt) CheckHealth ¶
CheckHealth - Check if extension is healthy
Example ¶
package main
import (
"context"
"fmt"
"github.com/apocelipes/gobay"
"github.com/apocelipes/gobay/extensions/cachext"
_ "github.com/apocelipes/gobay/extensions/cachext/backend/memory"
)
func main() {
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
if _, err := gobay.CreateApp("../../testdata/", "testing", exts); err != nil {
fmt.Println(err)
return
}
err := cache.CheckHealth(context.Background())
fmt.Println(err)
}
Output: <nil>
func (*CacheExt) DeleteMany ¶
DeleteMany
func (*CacheExt) Init ¶
func (c *CacheExt) Init(app *gobay.Application) error
Init init a cache extension
func (*CacheExt) Set ¶
Set
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/apocelipes/gobay"
"github.com/apocelipes/gobay/extensions/cachext"
_ "github.com/apocelipes/gobay/extensions/cachext/backend/memory"
)
func main() {
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
if _, err := gobay.CreateApp("../../testdata/", "testing", exts); err != nil {
fmt.Println(err)
return
}
var key = "cache_key"
err := cache.Set(context.Background(), key, "hello", 10*time.Second)
fmt.Println(err)
var res string
exists, err := cache.Get(context.Background(), key, &res)
fmt.Println(exists, res, err)
}
Output: <nil> true hello <nil>
func (*CacheExt) SetMany ¶
func (c *CacheExt) SetMany(ctx context.Context, keyValues map[string]interface{}, ttl time.Duration) error
SetMany
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/apocelipes/gobay"
"github.com/apocelipes/gobay/extensions/cachext"
_ "github.com/apocelipes/gobay/extensions/cachext/backend/memory"
)
func main() {
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
if _, err := gobay.CreateApp("../../testdata/", "testing", exts); err != nil {
fmt.Println(err)
return
}
// SetMany GetMany
many_map := make(map[string]interface{})
many_map["1"] = "hello"
many_map["2"] = []bool{true, true}
err := cache.SetMany(context.Background(), many_map, 10*time.Second)
fmt.Println(err)
many_res := make(map[string]interface{})
// 填上零值
var str1 string
val2 := []bool{}
many_res["1"] = &str1
many_res["2"] = &val2
err = cache.GetMany(context.Background(), many_res)
fmt.Println(err, *(many_res["1"].(*string)), *(many_res["2"].(*[]bool)))
}
Output: <nil> <nil> hello [true true]
type CachedConfig ¶
type CachedConfig struct {
// contains filtered or unexported fields
}
CachedConfig save the param and config for a cached func
func (*CachedConfig) GetResult ¶
func (c *CachedConfig) GetResult(ctx context.Context, out interface{}, strArgs []string, intArgs []int64) error
GetResult
func (*CachedConfig) MakeCacheKey ¶
func (c *CachedConfig) MakeCacheKey(strArgs []string, intArgs []int64) string
MakeCacheKey return the cache key of a function cache