cache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

用法

go-cache

package main

import (
	"fmt"
	gocache "github.com/patrickmn/go-cache"
	"github.com/zhiting-tech/smartassistant/pkg/cache"
	"github.com/zhiting-tech/smartassistant/pkg/cache/store"
	"time"
)

func main() {
	gocacheClient := gocache.New(5*time.Minute, 10*time.Minute)
	gocacheStore := store.NewGoCache(gocacheClient, nil)

	cacheManager := cache.New(gocacheStore)
	err := cacheManager.Set("my-key", "my-value", 0)
	if err != nil {
		panic(err)
	}

	value, err := cacheManager.Get("my-key")
	if err == store.ErrValueNotFound {
		fmt.Println("value not found")
	} else {
		fmt.Println(value)
	}

}

redis

package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"github.com/zhiting-tech/smartassistant/pkg/cache"
	"github.com/zhiting-tech/smartassistant/pkg/cache/store"
	"time"
)

func main() {
	redisStore := store.NewRedis(redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
	}), nil)

	cacheManager := cache.New(redisStore)
	err := cacheManager.Set("my-key", "my-value", 15*time.Second)
	if err != nil {
		panic(err)
	}

	value, err := cacheManager.Get("my-key")
	if err == store.ErrValueNotFound {
		fmt.Println("value not found")
	} else {
		fmt.Println(value)
	}

}

通过包名方式

package main

import (
	"fmt"
	"github.com/zhiting-tech/smartassistant/pkg/cache"
	"github.com/zhiting-tech/smartassistant/pkg/cache/store"
)

func main() {
	//如果想使用自定义存储,可以使用一下方式初始化,默认使用的是go-cache作为存储。
	//redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
	//redisStore := store.NewRedis(redisClient, nil)
	//cache.InitCache(redisStore)
	
	err := cache.Set("my-key", "my-value", 0)
	if err != nil {
		panic(err)
	}

	value, err := cache.Get("my-key")
	if err == store.ErrValueNotFound {
		fmt.Println("value not found")
	} else {
		fmt.Println("value:", value)
	}

	value, ttl, err := cache.GetWithTTL("my-key")
	if err == store.ErrValueNotFound {
		fmt.Println("value not found")
	} else {
		fmt.Println("value:", value, " ttl:", ttl)
	}

	err = cache.Delete("my-key")
	if err != nil {
		panic(err)
	}

	value, err = cache.Get("my-key")
	if err == store.ErrValueNotFound {
		fmt.Println("value not found")
	} else {
		fmt.Println("value:", value)
	}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(key interface{}) error

删除key对应的缓存。

func Get

func Get(key interface{}) (interface{}, error)

根据key获取value,如果获取不到,返回ErrValueNotFound错误。

func GetStore

func GetStore() store.StoreInterface

获取缓存的存储。

func GetType

func GetType() string

获取缓存的存储类型。

func GetWithTTL

func GetWithTTL(key interface{}) (interface{}, time.Duration, error)

根据key获取value和value在缓存的有限期。

func InitCache

func InitCache(store store.StoreInterface)

使用自定义的存储初始化全局cache。

func Set

func Set(key interface{}, val interface{}, expiration time.Duration) error

添加key对应的value到缓存中,有效期expiration为0, 则使用创建缓存存储时的options的expiration属性,默认缓存存储go-cache的过期时间为5分钟。

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

func New

func New(store store.StoreInterface) *Cache

func (*Cache) Clear

func (c *Cache) Clear() error

func (*Cache) Delete

func (c *Cache) Delete(key interface{}) error

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, error)

func (*Cache) GetStore

func (c *Cache) GetStore() store.StoreInterface

func (*Cache) GetType

func (c *Cache) GetType() string

func (*Cache) GetWithTTL

func (c *Cache) GetWithTTL(key interface{}) (interface{}, time.Duration, error)

func (*Cache) Set

func (c *Cache) Set(key interface{}, val interface{}, expiration time.Duration) error

type CacheInterface

type CacheInterface interface {
	store.StoreInterface

	GetStore() store.StoreInterface
}

Directories

Path Synopsis
test
mocks/store
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
mocks/store/clients
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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