gocache

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

gocache

Badge Go Go Report Card GoDoc Latest codecov License

支持的功能

1.并发安全;
2.LRU淘汰策略;
3.数据过期,过期策略为懒更新;
4.value支持interface;
5.支持返回过期数据和对应error;
6.支持singelfight方式更新下游;
7.支持一键式读取并更新缓存;
8.支持缓存状态信息打印。

使用示例
func main() {
    cache := NewGoCacheBuilder(WithExpire(time.Second), WithKeyCnt(3), WithName("test_cache"))
    v, err := cache.Get(context.TODO(), "1", func() (interface{}, error) {
        return "1", nil
    })
}

Documentation

Overview

Package gocache 用于并发场景,支持自动化LRU,支持singlefight更新缓存

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound key不存在错误
	ErrKeyNotFound = fmt.Errorf("key not found")
	// ErrKeyIsExpired key过期错误
	ErrKeyIsExpired = fmt.Errorf("key is expired")
)

Functions

This section is empty.

Types

type Cache

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

Cache 缓存核心结构

func (*Cache) Get

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

Get 获取数据,返回数据和对应的error。如果数据正常,则error返回nil

func (*Cache) Len added in v1.1.4

func (c *Cache) Len() int

Len 获取缓存key的数量

func (*Cache) Set

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

Set 设置数据,设置数据。用error是否为nil来标识是否出错

type CacheOpt

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

CacheOpt Cache参数选项结构

type CacheOptHelper

type CacheOptHelper func(opt *CacheOpt)

CacheOptHelper Cache参数选项结构helper

type GoCacheBuilder added in v1.1.4

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

GoCacheBuilder 缓存功能基础结构定义

func NewGoCacheBuilder added in v1.1.4

func NewGoCacheBuilder(opts ...GoCacheBuilderOptHelper) *GoCacheBuilder

NewGoCacheBuilder GoCacheBuilder的构造函数

func (*GoCacheBuilder) Get added in v1.1.4

func (c *GoCacheBuilder) Get(ctx context.Context, key string, fn UpdateHelper) (interface{}, error)

Get 获取val,若val存在且未过期,则更新至缓存,否则通过singleflight的方式更新至缓存 若更新失败,则使用旧数据兜底并返回对应的error给调用方

type GoCacheBuilderOpt added in v1.1.4

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

GoCacheBuilderOpt cache相关配置

type GoCacheBuilderOptHelper added in v1.1.4

type GoCacheBuilderOptHelper func(opts *GoCacheBuilderOpt)

GoCacheBuilderOptHelper GoCacheBuilderOpt结构helper

func WithExpire

func WithExpire(expire time.Duration) GoCacheBuilderOptHelper

WithExpire 设置key过期时间

func WithKeyCnt

func WithKeyCnt(keyCnt int) GoCacheBuilderOptHelper

WithKeyCnt 设置key数量上限

func WithName added in v1.1.4

func WithName(name string) GoCacheBuilderOptHelper

WithName 设置更新函数

type UpdateCallback added in v0.0.2

type UpdateCallback func() (interface{}, error)

UpdateCallback 更新数据回调

type UpdateHelper added in v1.1.4

type UpdateHelper func() (val interface{}, err error)

UpdateHelper 更新功能helper

Jump to

Keyboard shortcuts

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