cachext

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const Nil = cacheNil("cache result is nil")

Variables

This section is empty.

Functions

func RegisteBackend

func RegisteBackend(configBackend string, backend CacheBackend) error

RegisteBackend if you want a new backend, use this func to registe your backend then load it by config

func SetCacheNil

func SetCacheNil(cacheNil bool) cacheOption

SetCacheNil set whether cacheNil to cacheFuncConfig, if cacheNil seted and function returns nil, GetResult will return Nil cacheNil stored in redis with []byte{192} 0xC0

func SetMakeCacheKey

func SetMakeCacheKey(f func(funcName string, version int64, strArgs []string, intArgs []int64) string) cacheOption

SetMakeCacheKey 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 SetTTL

func SetTTL(ttl time.Duration) cacheOption

SetTTL set ttl to the cachedConfig object, ttl must be a positive number

func SetVersion

func SetVersion(version int64) cacheOption

SetVersion 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(key string) ([]byte, error) // if record not exist, return (nil, nil)
	Set(key string, value []byte, ttl time.Duration) error
	SetMany(keyValues map[string][]byte, ttl time.Duration) error
	GetMany(keys []string) [][]byte // if record not exist, use nil instead
	Delete(key string) bool
	DeleteMany(keys []string) bool
	Expire(key string, ttl time.Duration) bool
	TTL(key string) time.Duration
	Exists(key string) bool
	Close() error
}

type CacheExt

type CacheExt struct {
	NS string
	// contains filtered or unexported fields
}

CacheExt

func (*CacheExt) Application

func (c *CacheExt) Application() *gobay.Application

Application

func (*CacheExt) Cached

func (c *CacheExt) Cached(f func([]string, []int64) (interface{}, error), options ...cacheOption) (*cachedConfig, error)

Cached return a ptr with two function: MakeCacheKey and GetResult

Example
package main

import (
	"fmt"
	"github.com/shanbay/gobay"
	"github.com/shanbay/gobay/cachext"
	"strings"
	"time"

	_ "github.com/shanbay/gobay/cachext/backend/memory"
)

func main() {
	cache := &cachext.CacheExt{}
	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(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, cachext.SetTTL(10*time.Second), cachext.SetVersion(1), cachext.SetMakeCacheKey(
		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(&zero_res, []string{"hello", "world"}, []int64{}); err != nil {
			fmt.Println("Cache set Failed")
		}
	}
	err = cachedFunc.GetResult(&zero_res, []string{"hello", "world"}, []int64{})
	fmt.Println(zero_res, call_times, err)
}
Output:
[hello hello] 1 <nil>

func (*CacheExt) Close

func (c *CacheExt) Close() error

Close

func (*CacheExt) Delete

func (c *CacheExt) Delete(key string) bool

Delete

func (*CacheExt) DeleteMany

func (c *CacheExt) DeleteMany(keys ...string) bool

DeleteMany

func (*CacheExt) Exists

func (c *CacheExt) Exists(key string) bool

Exists

func (*CacheExt) Expire

func (c *CacheExt) Expire(key string, ttl time.Duration) bool

Expire

func (*CacheExt) Get

func (c *CacheExt) Get(key string, m interface{}) (bool, error)

Get

func (*CacheExt) GetMany

func (c *CacheExt) GetMany(out map[string]interface{}) error

GetMany out map[string]*someStruct

func (*CacheExt) Init

func (c *CacheExt) Init(app *gobay.Application) error

Init init a cache extension

func (*CacheExt) Object

func (c *CacheExt) Object() interface{}

Object

func (*CacheExt) Set

func (c *CacheExt) Set(key string, value interface{}, ttl time.Duration) error

Set

Example
package main

import (
	"fmt"
	"github.com/shanbay/gobay"
	"github.com/shanbay/gobay/cachext"
	"time"

	_ "github.com/shanbay/gobay/cachext/backend/memory"
)

func main() {
	cache := &cachext.CacheExt{}
	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(key, "hello", 10*time.Second)
	fmt.Println(err)
	var res string
	exists, err := cache.Get(key, &res)
	fmt.Println(exists, res, err)
}
Output:
<nil>
true hello <nil>

func (*CacheExt) SetMany

func (c *CacheExt) SetMany(keyValues map[string]interface{}, ttl time.Duration) error

SetMany

Example
package main

import (
	"fmt"
	"github.com/shanbay/gobay"
	"github.com/shanbay/gobay/cachext"
	"time"

	_ "github.com/shanbay/gobay/cachext/backend/memory"
)

func main() {
	cache := &cachext.CacheExt{}
	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(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(many_res)
	fmt.Println(err, *(many_res["1"].(*string)), *(many_res["2"].(*[]bool)))
}
Output:
<nil>
<nil> hello [true true]

func (*CacheExt) TTL

func (c *CacheExt) TTL(key string) time.Duration

TTL

Directories

Path Synopsis
backend

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL