cache

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 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

Overview

cache 是一个可以使用go-cache或redis作为缓存存储的缓存库。 默认使用基于内存的go-cache(patrickmn/go-cache)作为缓存存储, 缓存过期时间为5分钟,可以通过InitCache方法修改默认缓存存储。 提供了Set,SetNX,Get,GetWithTLL,Delete这些常用操作缓存的方法。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(key string) error

删除key对应的缓存。

Example
err := Delete("my-key")
if err != nil {
	log.Fatal(err)
}
Output:

func Get

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

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

Example
err := Set("my-key", "my-value", 0)
if err != nil {
	log.Fatal(err)
}
val, err := Get("my-key")
if err != nil {
	if err == store.ErrValueNotFound {
		fmt.Println(err.Error())
	} else {
		log.Fatal(err)
	}
}
fmt.Println(val)
Output:

my-value

func GetStore

func GetStore() store.StoreInterface

获取缓存的存储。

func GetType

func GetType() string

获取缓存的存储类型。

func GetWithTTL

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

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

Example
err := Set("my-key", "my-value", 0)
if err != nil {
	log.Fatal(err)
}
value, ttl, err := GetWithTTL("my-key")
if err != nil {
	if err == store.ErrValueNotFound {
		fmt.Println(err.Error())
	} else {
		log.Fatal(err)
	}
}
fmt.Println(value, ttl)
Output:

func InitCache

func InitCache(store store.StoreInterface)

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

Example
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
redisStore := store.NewRedis(redisClient, nil)
InitCache(redisStore)
err := Set("my-key", "my-value", 0)
if err != nil {
	log.Fatal(err)
}
Output:

func Set

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

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

Example
err := Set("my-key", "my-value", 0)
if err != nil {
	log.Fatal(err)
}
Output:

func SetNX added in v1.9.1

func SetNX(key string, val interface{}, expiration time.Duration) bool

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

Example
exists := SetNX("my-key", "my-value", 0)
fmt.Println(exists)
Output:

true

Types

type Cache

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

Cache

func New

func New(store store.StoreInterface) *Cache

New 创建一个Cache对象

func (*Cache) Clear

func (c *Cache) Clear() error

Clear 清除缓存的所有数据

func (*Cache) Delete

func (c *Cache) Delete(key string) error

Delete 根据key将缓存中的对象删除

func (*Cache) Get

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

Get 如果缓存中存在key对应的缓存对象,则返回该对象,否则返回ErrValueNotFound

func (*Cache) GetStore

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

GetStore 返回缓存存储

func (*Cache) GetType

func (c *Cache) GetType() string

GetType 返回缓存的存储类型

func (*Cache) GetWithTTL

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

GetWithTTL 如果缓存中存在key对应的缓存对象,则返回该对象和相应的TTL, 否则返回ErrValueNotFound

func (*Cache) Set

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

Set 根据key,和过期时间,过期时间为0表示使用初始化缓存存储的过期时间,将value添加缓存中

func (*Cache) SetNX added in v1.9.1

func (c *Cache) SetNX(key string, value interface{}, expiration time.Duration) bool

SetNX 根据key,和过期时间,过期时间为0表示使用初始化缓存存储的过期时间,将value添加缓存中 如果缓存中原来不存在key对应的对象则返回true,否则返回flase

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