local_cache

package
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 9 Imported by: 114

Documentation

Index

Examples

Constants

View Source
const (
	DefaultExpire time.Duration = 0
	NoExpire      time.Duration = -1
)

Variables

View Source
var (
	CacheExist   = errors.New("local_cache: cache exist")
	CacheNoExist = errors.New("local_cache: cache no exist")
	CacheExpire  = errors.New("local_cache: cache expire")
	CacheTypeErr = errors.New("local_cache: cache incr type err")
	CacheGobErr  = errors.New("local_cache: cache save gob err")
)

Functions

func CacheErrExist

func CacheErrExist(e error) bool

func CacheErrExpire

func CacheErrExpire(e error) bool

func CacheErrNoExist

func CacheErrNoExist(e error) bool

func CacheErrTypeErr

func CacheErrTypeErr(e error) bool

func NewSentinel

func NewSentinel(ctx context.Context, interval time.Duration, fn func()) *sentinel

func SetCapture

func SetCapture(capture func(k string, v interface{})) options.Option

SetCapture 设置触发删除后的捕获函数, 数据删除后回调用设置的捕获函数

func SetDefaultExpire

func SetDefaultExpire(expire time.Duration) options.Option

SetDefaultExpire 设置默认的超时时间

func SetFn

func SetFn(fn func()) options.Option

SetFn 设置周期的执行函数,默认(不设置)是扫描全局清除过期的k

func SetInternal

func SetInternal(interval time.Duration) options.Option

SetInternal 设置间隔时间

func SetMember

func SetMember(m map[string]Iterator) options.Option

SetMember 设置初始化存储的成员对象

Types

type Cache

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

func NewCache

func NewCache(options ...options.Option) Cache
Example
// 默认配置
// ch = NewCache()

// 可供选择的配置选项

// 设置间隔时间
// SetInternal(interval time.Duration)

// 设置默认的超时时间
// SetDefaultExpire(expire time.Duration)

// 设置周期的执行函数,默认(不设置)是扫描全局清除过期的k
// SetFn(fn func())

// 设置触发删除后的捕获函数, 数据删除后回调用设置的捕获函数
// SetCapture(capture func(k string, v interface{}))

// 设置初始化存储的成员对象
// SetMember(m map[string]Iterator)

ch = NewCache(SetInternal(1000),
	SetDefaultExpire(10000),
	SetCapture(func(k string, v interface{}) {
		log.Println(k, v)
	}))
Output:

func (Cache) Add

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

Add 添加cache 如果存在则抛出异常

func (Cache) ChangeCapture

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

ChangeCapture 替换cache中capture的处理函数

func (Cache) Count

func (c Cache) Count() int

Count 计算现在 member 中 kv的数量 (所有)

func (Cache) Decrement

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

Decrement 为k对应的value减少n n必须为数字类型

func (Cache) DecrementFloat

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

DecrementFloat 为k对应的value减少n n必须为浮点数类型

func (Cache) DecrementFloat32

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

DecrementFloat32 为k对应的value减少n n必须为float32类型

func (Cache) DecrementFloat64

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

DecrementFloat64 为k对应的value减少n n必须为float64类型

func (Cache) DecrementInt

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

DecrementInt 为k对应的value减少n n必须为int类型

func (Cache) DecrementInt16

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

DecrementInt16 为k对应的value减少n n必须为int16类型

func (Cache) DecrementInt32

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

DecrementInt32 为k对应的value减少n n必须为int32类型

func (Cache) DecrementInt64

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

DecrementInt64 为k对应的value减少n n必须为int64类型

func (Cache) DecrementInt8

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

DecrementInt8 为k对应的value减少n n必须为int8类型

func (Cache) DecrementUint

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

DecrementUint 为k对应的value减少n n必须为uint类型

func (Cache) DecrementUint16

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

DecrementUint16 为k对应的value减少n n必须为uint16类型

func (Cache) DecrementUint32

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

DecrementUint32 为k对应的value减少n n必须为uint32类型

func (Cache) DecrementUint64

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

DecrementUint64 为k对应的value减少n n必须为uint64类型

func (Cache) DecrementUint8

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

DecrementUint8 为k对应的value减少n n必须为uint8类型

func (Cache) DecrementUintPtr

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

DecrementUintPtr 为k对应的value减少n n必须为uintptr类型

func (Cache) Delete

func (c Cache) Delete(k string)

Delete 删除k的cache 如果 capture != nil 会调用 capture 函数 将 kv传入

func (Cache) DeleteExpire

func (c Cache) DeleteExpire()

DeleteExpire 删除已经过期的kv

func (Cache) Flush

func (c Cache) Flush()

Flush 释放member成员

func (Cache) Get

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

Get 根据key获取 cache

func (Cache) GetWithExpire

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

GetWithExpire 根据key获取 cache 并带出超时时间

func (Cache) Increment

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

Increment 为k对应的value增加n n必须为数字类型

func (Cache) IncrementFloat

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

IncrementFloat 为k对应的value增加n n必须为浮点数类型

func (Cache) IncrementFloat32

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

IncrementFloat32 为k对应的value增加n n必须为float32类型

func (Cache) IncrementFloat64

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

IncrementFloat64 为k对应的value增加n n必须为float64类型

func (Cache) IncrementInt

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

IncrementInt 为k对应的value增加n n必须为int类型

func (Cache) IncrementInt16

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

IncrementInt16 为k对应的value增加n n必须为int16类型

func (Cache) IncrementInt32

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

IncrementInt32 为k对应的value增加n n必须为int32类型

func (Cache) IncrementInt64

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

IncrementInt64 为k对应的value增加n n必须为int64类型

func (Cache) IncrementInt8

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

IncrementInt8 为k对应的value增加n n必须为int8类型

func (Cache) IncrementUint

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

IncrementUint 为k对应的value增加n n必须为uint类型

func (Cache) IncrementUint16

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

IncrementUint16 为k对应的value增加n n必须为uint16类型

func (Cache) IncrementUint32

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

IncrementUint32 为k对应的value增加n n必须为uint32类型

func (Cache) IncrementUint64

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

IncrementUint64 为k对应的value增加n n必须为uint64类型

func (Cache) IncrementUint8

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

IncrementUint8 为k对应的value增加n n必须为uint8类型

func (Cache) IncrementUintPtr

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

IncrementUintPtr 为k对应的value增加n n必须为uintptr类型

func (Cache) Iterator

func (c Cache) Iterator() map[string]Iterator

Iterator 返回 cache 中所有有效的对象

func (Cache) Load

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

Load 从r 中加载 c.member

func (Cache) LoadFile

func (c Cache) LoadFile(path string) error

LoadFile 从 path 中加载 c.member

func (Cache) Replace

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

Replace 替换cache 如果有就设置没有就抛出异常

func (Cache) Save

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

Save 将 c.member 写入到 w 中

func (Cache) SaveFile

func (c Cache) SaveFile(path string) error

SaveFile 将 c.member 保存到 path 中

func (Cache) Set

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

Set 添加cache 无论是否存在都会覆盖

func (Cache) SetDefault

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

SetDefault 添加cache 无论是否存在都会覆盖 超时设置为创建cache的默认时间

func (Cache) SetNoExpire

func (c Cache) SetNoExpire(k string, v interface{})

SetNoExpire 添加cache 无论是否存在都会覆盖 超时设置为0

func (Cache) Shutdown

func (c Cache) Shutdown() error

type Config

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

type Iterator

type Iterator struct {
	// Val 实际存储的对象
	Val interface{}

	// Expire 过期时间
	// 0 不设置过期时间
	Expire int64
}

Iterator cache存储的实际成员

func (Iterator) Expired

func (i Iterator) Expired(v ...int64) bool

Expired 判断是否过期,过期返回 true

Jump to

Keyboard shortcuts

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