cache

package
v0.1.81 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

README

是缓存K/V数据,类似于 memcached 适用于在单台机器上运行的应用程序。 主要优点是,具有过期时间的线程安全的“map[string]interface{}”,不需要通过网络序列化或传输其内容。

任何对象都可以存储,给定的持续时间或永久,并且缓存可以由多个协程安全使用。

并不能用作持久性数据存储,但整个缓存可以保存到文件中并从文件中加载(使用“c.Items()”检索要序列化的项目映射,使用“NewFrom()”从反序列化的缓存创建缓存)以快速从停机中恢复。(有关注意事项,请参阅“NewFrom()”的文档。

使用

import (
	"fmt"
	"gitee.com/xingnan/toolbox/cache"
	"time"
)

func main() {
	// 创建过期时间为 5 分钟的缓存,每 10 分钟清除一次过期项目
	c := cache.New(5*time.Minute, 10*time.Minute)

	// 将键“foo”的值设置为“bar”,默认过期时间
	c.Set("foo", "bar", cache.DefaultExpiration)
	
	// 将键“baz”的值设置为 42,没有过期时间(在重新设置或使用 c.Delete(“baz”)) 删除之前不会删除该项目
	c.Set("baz", 42, cache.NoExpiration)

	// 从缓存中获取与键“foo”关联的字符串
	foo, found := c.Get("foo")
	if found {
		fmt.Println(foo)
	}
	
	// 由于 Go 是静态类型的,并且缓存值可以是任何内容,因此当值传递给不采用任意类型的函数(即 interface{})时,需要类型断言。对于只会使用一次的值执行此操作的最简单方法 - 例如对于传递给另一个函数:
	foo, found := c.Get("foo")
	if found {
		MyFunction(foo.(string))
	}
	
	// 如果该值在同一函数中多次使用,您可以改为执行以下任一操作:
	if x, found := c.Get("foo"); found {
		foo := x.(string)
		// ...
	}
	// 或
	var foo string
	if x, found := c.Get("foo"); found {
		foo = x.(string)
	}
	// ...
	// 然后 foo 可以作为字符串自由传递

	// 想要性能?设置指针!
	c.Set("foo", &MyStruct, cache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		foo := x.(*MyStruct)
			// ...
	}
}

Documentation

Index

Constants

View Source
const (
	// NoExpiration 用于需要过期时间的函数,永不过期
	NoExpiration time.Duration = -1

	// DefaultExpiration 用于需要过期时间的函数。相当于传递与创建缓存时提供给 New() 或 NewFrom() 的相同过期时间(例如 5 分钟)。
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

New 返回指定默认过期时间和清理间隔的新缓存。 如果过期时间小于 1(或 NoExpiration),则缓存中的项目永不过期(默认情况下),必须手动删除。 如果清理间隔小于 1,则在调用 c.DeleteExpired() 之前,不会从缓存中删除过期的项目。

func NewFrom

func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache

NewFrom 返回指定默认过期时间和清理间隔的新缓存。 如果过期时间小于 1(或 NoExpiration),则缓存中的项目永不过期(默认情况下),必须手动删除。 如果清理间隔小于 1,则在调用 c.DeleteExpired() 之前,不会从缓存中删除过期的项目。

还接受将用作缓存的基础映射的项目映射。 这对于从反序列化缓存(c.Items() 序列化使用 gob.encode() ),或者传入(例如make(map[string]Item, 500)) 以提高缓存预期达到某个最小大小时的性能。

只有缓存的方法会同步对此映射的访问,因此不建议在创建缓存后保留对映射的任何引用。如果需要,可以在以后使用 c.Items 访问(受相同警告的约束)。

关于序列化的注意事项:使用 gob 时,请确保使用 gob.Register() 在对使用 c.Items() 检索到的映射进行编码之前存储在缓存中的单个类型,并在解码包含项目映射的 blob 之前注册这些相同的类型。

func (Cache) Add

func (c Cache) Add(k string, x interface{}, d time.Duration) error

Add 只有key不存在或已过期,才能将项添加到缓存中。否则返回错误。

func (Cache) Decrement

func (c Cache) Decrement(k string, n int64) error

Decrement 将类型为 int、int8、int16、int32、int64、uintptr、uint、uint8、uint32 、 uint64、float32 、 float64 的项目减 n。

func (Cache) DecrementFloat

func (c Cache) DecrementFloat(k string, n float64) error

DecrementFloat float类型的值递减 n

func (Cache) DecrementFloat32

func (c Cache) DecrementFloat32(k string, n float32) (float32, error)

DecrementFloat32 float32类型的值递减 n

func (Cache) DecrementFloat64

func (c Cache) DecrementFloat64(k string, n float64) (float64, error)

DecrementFloat64 float64类型的值递减 n

func (Cache) DecrementInt

func (c Cache) DecrementInt(k string, n int) (int, error)

DecrementInt int类型的值递减 n

func (Cache) DecrementInt8

func (c Cache) DecrementInt8(k string, n int8) (int8, error)

DecrementInt8 Int8类型的值递减 n

func (Cache) DecrementInt16

func (c Cache) DecrementInt16(k string, n int16) (int16, error)

DecrementInt16 int16类型的值递减 n

func (Cache) DecrementInt32

func (c Cache) DecrementInt32(k string, n int32) (int32, error)

DecrementInt32 int32类型的值递减 n

func (Cache) DecrementInt64

func (c Cache) DecrementInt64(k string, n int64) (int64, error)

DecrementInt64 int64类型的值递减 n

func (Cache) DecrementUint

func (c Cache) DecrementUint(k string, n uint) (uint, error)

DecrementUint uint类型的值递减 n

func (Cache) DecrementUint8

func (c Cache) DecrementUint8(k string, n uint8) (uint8, error)

DecrementUint8 uint8类型的值递减 n

func (Cache) DecrementUint16

func (c Cache) DecrementUint16(k string, n uint16) (uint16, error)

DecrementUint16 uint16类型的值递减 n

func (Cache) DecrementUint32

func (c Cache) DecrementUint32(k string, n uint32) (uint32, error)

DecrementUint32 uint32类型的值递减 n

func (Cache) DecrementUint64

func (c Cache) DecrementUint64(k string, n uint64) (uint64, error)

DecrementUint64 uint64类型的值递减 n

func (Cache) DecrementUintptr

func (c Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)

DecrementUintptr uintptr类型的值递减 n

func (Cache) Delete

func (c Cache) Delete(k string)

Delete 从缓存中删除项目

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

DeleteExpired 从缓存中删除所有已过期的项目

func (Cache) Flush

func (c Cache) Flush()

Flush 删除缓存中的所有项目

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get 从缓存中取的项目。返回项目或 nil,以及是否已找到项目的布尔值。

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)

GetWithExpiration 从缓存中返回一个项目及其过期时间。返回项目或 nil,如果设置了过期时间(如果项目永不过期,时间为0。),以及是否找到key的布尔值。

func (Cache) Increment

func (c Cache) Increment(k string, n int64) error

Increment 将类型为 int、int8、int16、int32、int64、uintptr、uint、uint8、uint32 、 uint64、float32 、 float64 的项目递增 n。 如果key的值不是整数、找不到key的值或无法按 n 递增,则返回错误。 要检索递增的值,请使用专用方法,例如 IncrementInt64。

func (Cache) IncrementFloat

func (c Cache) IncrementFloat(k string, n float64) error

IncrementFloat 将 float32 或 float64 类型的项目递增 n。 如果key的值不是浮点型、找不到浮点型或无法将其递增 n 时,则返回错误。 传递负数以减小该值。要检索递增的值,请使用专用方法,例如 IncrementFloat64。

func (Cache) IncrementFloat32

func (c Cache) IncrementFloat32(k string, n float32) (float32, error)

IncrementFloat32 float32类型的值递增 n

func (Cache) IncrementFloat64

func (c Cache) IncrementFloat64(k string, n float64) (float64, error)

IncrementFloat64 float64类型的值递增 n

func (Cache) IncrementInt

func (c Cache) IncrementInt(k string, n int) (int, error)

IncrementInt int类型的值递增 n

func (Cache) IncrementInt8

func (c Cache) IncrementInt8(k string, n int8) (int8, error)

IncrementInt8 int8类型的值递增 n

func (Cache) IncrementInt16

func (c Cache) IncrementInt16(k string, n int16) (int16, error)

IncrementInt16 int16类型的值递增 n

func (Cache) IncrementInt32

func (c Cache) IncrementInt32(k string, n int32) (int32, error)

IncrementInt32 int32类型的值递增 n

func (Cache) IncrementInt64

func (c Cache) IncrementInt64(k string, n int64) (int64, error)

IncrementInt64 int64类型的值递增 n

func (Cache) IncrementUint

func (c Cache) IncrementUint(k string, n uint) (uint, error)

IncrementUint uint类型的值递增 n

func (Cache) IncrementUint8

func (c Cache) IncrementUint8(k string, n uint8) (uint8, error)

IncrementUint8 uint8类型的值递增 n

func (Cache) IncrementUint16

func (c Cache) IncrementUint16(k string, n uint16) (uint16, error)

IncrementUint16 uint16类型的值递增 n

func (Cache) IncrementUint32

func (c Cache) IncrementUint32(k string, n uint32) (uint32, error)

IncrementUint32 uint32类型的值递增 n

func (Cache) IncrementUint64

func (c Cache) IncrementUint64(k string, n uint64) (uint64, error)

IncrementUint64 uint64类型的值递增 n

func (Cache) IncrementUintptr

func (c Cache) IncrementUintptr(k string, n uintptr) (uintptr, error)

IncrementUintptr uintptr类型的值递增 n

func (Cache) ItemCount

func (c Cache) ItemCount() int

ItemCount 返回缓存中的项目数量,可能包含已过期未清理的项目。

func (Cache) Items

func (c Cache) Items() map[string]Item

Items 将缓存中未过期的项目复制的新的map中

func (Cache) Load

func (c Cache) Load(r io.Reader) error

Load 从io.Reader中读取Gob序列化缓存项目。不包括当前缓存中的项目。 注:此方法已弃用以支持c.Items() 和 NewFrom()(参考 NewFrom() 描述)

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

LoadFile 从指定的文件中读取已保存的项目,不包含已缓存的项目。 注:此方法已弃用以支持c.Items() 和 NewFrom()(参考 NewFrom() 描述)

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(string, interface{}))

OnEvicted 设置一个(可选)函数,当从缓存中逐出项时,该函数与键和值一起调用。(包括手动删除时,但不包括覆盖时。设置为 nil 表示禁用。

func (Cache) Replace

func (c Cache) Replace(k string, x interface{}, d time.Duration) error

Replace key已存在且未过期时,才为其设置新值。否则返回错误。

func (Cache) Save

func (c Cache) Save(w io.Writer) (err error)

Save io.Writer 使用Gob将缓存中的项目保存 注:此方法已弃用以支持c.Items() 和 NewFrom()(参考 NewFrom() 描述)

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

SaveFile 将缓存的项目保存到指定的文件,如果文件不存在,则创建该文件,如果存在,则覆盖该文件。 注:此方法已弃用以支持c.Items() 和 NewFrom()(参考 NewFrom() 描述)

func (Cache) Set

func (c Cache) Set(k string, x interface{}, d time.Duration)

Set 将项目添加到缓存,替换已有项目。如果设置时间为 0(默认过期),则使用缓存的默认过期时间。如果为 -1,则项目永不过期。

func (Cache) SetDefault

func (c Cache) SetDefault(k string, x interface{})

SetDefault 使用默认过期时间将项目添加到缓存,替换已项目。

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

func (Item) Expired

func (item Item) Expired() bool

Expired 如果已过期,则返回 true

Jump to

Keyboard shortcuts

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