fscache

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 6 Imported by: 1

README

fscache

统一封装的缓存接口,目前已包含文件缓存(FileCache)、内存LRU缓存。

GoDoc

1.缓存接口定义

// Cache 缓存API
type Cache interface {
    Get(ctx context.Context, key any) GetResult
    Set(ctx context.Context, key any, value any, ttl time.Duration) SetResult
    Has(ctx context.Context, key any) HasResult
    Delete(ctx context.Context, key any) DeleteResult
    
    // 以下为批处理接口
    MGet(ctx context.Context, keys []any) MGetResult
    MSet(ctx context.Context, kvs KVData, ttl time.Duration) MSetResult
    MDelete(ctx context.Context, keys []any) MDeleteResult
    MHas(ctx context.Context, keys []any) MHasResult
}

注:为了将批请求结果和单个处理结果尽量保持一致,操作结果均返回一个值。可以使用对应的Err()方法来判断是否有异常

2.使用示例

import (
    "github.com/fsgo/fscache/filecache"
)

opt:=&filecache.Option{
    Dir: "./testdata/cache_dir/",
}

fc,err:=filecache.New(opt)
if err != nil {
    log.Fatalf("init cache failed: %v", err)
}

// 1.读取缓存
retGet := fc.Get(context.Background(), "abc")
if err := retGet.Err(); err != nil {
    t.Fatalf("retGet.Value with error:%v", err)
}

// 2.获取缓存值
var num int
if has, err := retGet.Value(&num); err != nil {
    t.Fatalf("retGet.Value with error:%v", err)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotExists = errors.New("cache not exists")

ErrNotExists 缓存数据不存在

Functions

This section is empty.

Types

type Bag

type Bag[T any] struct {
	Data T
}

type Cache

type Cache interface {
	SCache
	MCache
}

Cache 缓存API

func NewTemplate

func NewTemplate(sc SCache, concurrent bool) Cache

NewTemplate 利用一个简单的缓存类,创建一个包含批量接口的缓存类

type Codec

type Codec interface {
	// Marshal 数据序列化方法
	Marshal(obj any) ([]byte, error)

	// Unmarshal 数据反序列化方法
	Unmarshal(bf []byte, obj any) error
}

Codec 数据编解码器

func NewCodec

func NewCodec(encode MarshalFunc, decode UnmarshalFunc) Codec

NewCodec 创建一个新的编解码器

type DeleteResult

type DeleteResult struct {
	Err     error
	Deleted int
}

DeleteResult Delete 方法的结果接口定义

type GetResult

type GetResult struct {
	Err           error
	UnmarshalFunc UnmarshalFunc
	Payload       []byte
}

GetResult Get 方法的结果

func (GetResult) String

func (g GetResult) String() string

func (GetResult) Value

func (g GetResult) Value(obj any) (has bool, err error)

Value 获取值

type HasResult

type HasResult struct {
	Err error
	Has bool
}

HasResult Has 方法的结果

type KVData

type KVData map[any]any

KVData k-v pairs

type MCache

type MCache interface {
	MGetter
	MSetter
	MDeleter
	MHaser
}

MCache 缓存-批处理接口

func NewMCacheBySCache

func NewMCacheBySCache(sCache SCache, concurrent bool) MCache

NewMCacheBySCache 创建一个MCacheBySCache实例

type MDeleteResult

type MDeleteResult map[any]DeleteResult

MDeleteResult 批量删除 MDelete 接口的结果

func (MDeleteResult) Deleted

func (md MDeleteResult) Deleted() int

Deleted 删除的条数

func (MDeleteResult) Err

func (md MDeleteResult) Err() error

Err 是否有异常

func (MDeleteResult) Get

func (md MDeleteResult) Get(key any) DeleteResult

Get 获取对应key的信息

type MDeleter

type MDeleter interface {
	MDelete(ctx context.Context, keys []any) MDeleteResult
}

MDeleter 批量删除缓存

type MGetResult

type MGetResult map[any]GetResult

MGetResult 批量查询 MGet 接口的结果 若key不存在,是不存在

func (MGetResult) Err

func (mr MGetResult) Err() error

Err 是否有异常

func (MGetResult) Get

func (mr MGetResult) Get(key any) GetResult

Get 读取 key 的结果

type MGetter

type MGetter interface {
	MGet(ctx context.Context, keys []any) MGetResult
}

MGetter 批量查询缓存

type MHasResult

type MHasResult map[any]HasResult

MHasResult 批量判断是否存在 MHas 接口的结果

func (MHasResult) Err

func (mh MHasResult) Err() error

Err 是否有异常

func (MHasResult) Get

func (mh MHasResult) Get(key any) HasResult

Get 读取 key 的结果

type MHaser

type MHaser interface {
	MHas(ctx context.Context, keys []any) MHasResult
}

MHaser 批量判断是否存在

type MSetResult

type MSetResult map[any]SetResult

MSetResult 批量设置 MSet 接口的结果

func (MSetResult) Err

func (mr MSetResult) Err() error

Err 是否有异常

func (MSetResult) Get

func (mr MSetResult) Get(key any) SetResult

Get 读取 key 的结果

type MSetter

type MSetter interface {
	MSet(ctx context.Context, kvs KVData, ttl time.Duration) MSetResult
}

MSetter 批量设置缓存

type MarshalFunc

type MarshalFunc func(obj any) ([]byte, error)

MarshalFunc 数据序列化方法

type Option

type Option struct {
	// Codec 编解码器,可选,默认为 msgpack
	Codec Codec
}

Option 配置

func (*Option) GetCodec

func (o *Option) GetCodec() Codec

GetCodec 获取编解码器,若没有设置,会返回默认值(msgpack)

type ProS

type ProS[K any, V any] struct {
	// SCache 必填
	SCache SCache
}

func (*ProS[K, V]) Delete

func (pc *ProS[K, V]) Delete(ctx context.Context, key K) (deleted int, err error)

Delete 删除指定的 key

func (*ProS[K, V]) Get

func (pc *ProS[K, V]) Get(ctx context.Context, key K) (value V, err error)

Get 查询单个

Example
package main

import (
	"context"
	"fmt"

	"github.com/fsgo/fscache"
	"github.com/fsgo/fscache/nopcache"
)

func main() {
	ps := &fscache.ProS[string, string]{
		SCache: nopcache.Nop,
	}
	got, err := ps.Get(context.Background(), "hello")
	fmt.Println("got=", got, ", err=", err)
}
Output:

got=  , err= cache not exists

func (*ProS[K, V]) Has

func (pc *ProS[K, V]) Has(ctx context.Context, key K) (has bool, err error)

Has 判断是否存在

func (*ProS[K, V]) Set

func (pc *ProS[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration) error

Set 设置并附带有效期

type ReSetter

type ReSetter interface {
	Reset(ctx context.Context) error
}

ReSetter 重置缓存 如本地文件缓存,可以实现该接口

type SCache

type SCache interface {
	// Get 查询单个
	Get(ctx context.Context, key any) GetResult

	// Set 设置并附带有效期
	Set(ctx context.Context, key any, value any, ttl time.Duration) SetResult

	// Has 判断是否存在
	Has(ctx context.Context, key any) HasResult

	// Delete 删除指定的 key
	Delete(ctx context.Context, key any) DeleteResult
}

SCache 普通的单个缓存

type SetResult

type SetResult struct {
	Err error
}

SetResult Set 方法的结果

type Template

type Template struct {
	SCache SCache
	MCache MCache
}

Template 缓存模板类

func (*Template) Delete

func (ct *Template) Delete(ctx context.Context, key any) DeleteResult

Delete 删除

func (*Template) Get

func (ct *Template) Get(ctx context.Context, key any) GetResult

Get 读取

func (*Template) Has

func (ct *Template) Has(ctx context.Context, key any) HasResult

Has 是否存在

func (*Template) MDelete

func (ct *Template) MDelete(ctx context.Context, keys []any) MDeleteResult

MDelete 批量删除

func (*Template) MGet

func (ct *Template) MGet(ctx context.Context, keys []any) MGetResult

MGet 批量获取

func (*Template) MHas

func (ct *Template) MHas(ctx context.Context, keys []any) MHasResult

MHas 批量判断是否存在

func (*Template) MSet

func (ct *Template) MSet(ctx context.Context, kvs KVData, ttl time.Duration) MSetResult

MSet 批量写入

func (*Template) Reset

func (ct *Template) Reset(ctx context.Context) error

Reset 重置缓存

func (*Template) Set

func (ct *Template) Set(ctx context.Context, key any, value any, ttl time.Duration) SetResult

Set 写入

type UnmarshalFunc

type UnmarshalFunc func(bf []byte, obj any) error

UnmarshalFunc 数据反序列化方法

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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