cache

package
v2.0.0-...-e2e97c8 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 7 Imported by: 0

README

介绍

1. map类型缓存

  • 提供间隔时间对数据进行过期清理
  • 可手动开启/停止清理能力
  • 可手动清除全部缓存
  • 缓存持久化
  • ...

接口

// IsExpired judge whether the data is expired
IsExpired(key string) (bool, error)
// DeleteExpired delete all expired data
DeleteExpired()

// StartGc start gc
// After the expiration time is set, GC will be started automatically without manual GC
StartGc() error
// StopGc stop gc
StopGc() error

// Get data
// When the data does not exist or expires, it will return nonexistence(false)
Get(key string) (E, bool)
// GetAndDelete get data and delete by key
GetAndDelete(key string) (E, bool)
// GetAndExpired  get data and expire by key
// It will be deleted at the next clearing. If the clearing capability is not enabled, it will never be deleted
GetAndExpired(key string) (E, bool)

// Delete delete data by key
Delete(key string) (E, bool)


// Set  data by key,it will overwrite the data if the key exists
Set(key string, value E)
// Add data,Cannot add existing data
// To override the addition, use the set method
Add(key string, value E) error
// Clear remove all data
Clear()
// Keys get all keys
Keys() []string

初始化可选项

// 设置过期时间
SetExpirationTime(expiration time.Duration)

// 设置gc时间间隔
SetGcInterval(gcInterval time.Duration)

// 开启持久化(需要指定持久化文件名前缀)
SetEnablePersistence(name string)

// 设置持久化策略(目前只支持一种FFB:全量保存)
SetPersistencePolicy(policy Persistence)

// 设置持久化文件保存路径
SetPersistencePath(path string)

使用

示例

func Test(t *testing.T) {{
    c,err := cache.NewMapCache[int]()
    if err != nil {
        fmt.Println("err:", err)
        return
    }
    c.Set("1", 1)
    fmt.Println(c.Get("1"))
}

输出

1 true

Documentation

Index

Constants

View Source
const (
	// DefaultExpiration Default expiration time flag, never expires
	DefaultExpiration time.Duration = -1

	// DefaultInterval Default expiration interval is one minute
	DefaultInterval = time.Minute

	// DefaultPersistencePath default persistence path
	DefaultPersistencePath = "/val/cache/persistence"
)
View Source
const FileSUFFIX = "_ffb.cdb"

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateOptionFunc

type CreateOptionFunc func(o *options)

CreateOptionFunc Initialize optional parameters

func SetEnablePersistence

func SetEnablePersistence(name string) CreateOptionFunc

SetEnablePersistence SetDefault whether to enable persistencePolicy

func SetExpirationTime

func SetExpirationTime(expiration time.Duration) CreateOptionFunc

SetExpirationTime set expiration time expiration time

func SetGcInterval

func SetGcInterval(gcInterval time.Duration) CreateOptionFunc

SetGcInterval set gc interval When the cleaning cycle is 0, it is automatically adjusted to 1 minute

func SetPersistencePath

func SetPersistencePath(path string) CreateOptionFunc

SetPersistencePath set persistence path,default persistence path is DefaultPersistencePath

func SetPersistencePolicy

func SetPersistencePolicy(policy Persistence) CreateOptionFunc

SetPersistencePolicy set persistencePolicy policy,default persistencePolicy is FFB

type Interface

type Interface[E any] interface {
	// IsExpired judge whether the data is expired
	IsExpired(key string) (bool, error)
	// DeleteExpired delete all expired data
	DeleteExpired()

	// StartGc start gc
	// After the expiration time is set, GC will be started automatically without manual GC
	StartGc() error
	// StopGc stop gc
	StopGc() error

	// Get  data
	// When the data does not exist or expires, it will return nonexistence(false)
	Get(key string) (E, bool)
	// GetAndDelete get data and delete by key
	GetAndDelete(key string) (E, bool)
	// GetAndExpired  get data and expire by key
	// It will be deleted at the next clearing. If the clearing capability is not enabled, it will never be deleted
	GetAndExpired(key string) (E, bool)
	// GetWithExpiration get expiration time
	GetWithExpiration(key string) (E, time.Time, bool)

	// Delete delete data by key
	Delete(key string) (E, bool)
}

type Item

type Item[E any] struct {
	Object     E     // data
	Expiration int64 // expiration time
}

type Map

type Map[E any] struct {
	// contains filtered or unexported fields
}

func (*Map[E]) Add

func (c *Map[E]) Add(key string, value E) error

Add data,Cannot add existing data To override the addition, use the set method

func (*Map[E]) Clear

func (c *Map[E]) Clear()

Clear remove all data

func (*Map[E]) Delete

func (c *Map[E]) Delete(key string) (E, bool)

Delete delete data by key

func (*Map[E]) DeleteExpired

func (c *Map[E]) DeleteExpired()

DeleteExpired delete all expired data

func (*Map[E]) Get

func (c *Map[E]) Get(key string) (E, bool)

Get data When the data does not exist or expires, it will return nonexistence(false)

func (*Map[E]) GetAndDelete

func (c *Map[E]) GetAndDelete(key string) (E, bool)

GetAndDelete get data and delete by key

func (*Map[E]) GetAndExpired

func (c *Map[E]) GetAndExpired(key string) (E, bool)

GetAndExpired get data and expire by key It will be deleted at the next clearing. If the clearing capability is not enabled, it will never be deleted

func (*Map[E]) GetWithExpiration

func (c *Map[E]) GetWithExpiration(key string) (E, time.Time, bool)

func (*Map[E]) IsExpired

func (c *Map[E]) IsExpired(key string) (bool, error)

IsExpired judge whether the data is expired

func (*Map[E]) Keys

func (c *Map[E]) Keys() []string

Keys get all keys

func (*Map[E]) Set

func (c *Map[E]) Set(key string, value E)

Set data by key,it will overwrite the data if the key exists

func (*Map[E]) SetDefault

func (c *Map[E]) SetDefault(key string, value E, expiration time.Duration)

SetDefault data by key,it will overwrite the data if the key exists

func (*Map[E]) StartGc

func (c *Map[E]) StartGc() error

StartGc start gc After the expiration time is set, GC will be started automatically without manual GC

func (*Map[E]) StopGc

func (c *Map[E]) StopGc() error

StopGc stop gc

type MapInterface

type MapInterface[E any] interface {
	Interface[E]

	// Set  data by key,it will overwrite the data if the key exists
	Set(key string, value E)
	// SetDefault  data by key,it will overwrite the data if the key exists
	SetDefault(key string, value E, expiration time.Duration)
	// Add data,Cannot add existing data
	// To override the addition, use the set method
	Add(key string, value E) error
	// Clear remove all data
	Clear()
	// Keys get all keys
	Keys() []string
}

func NewMapCache

func NewMapCache[E any](opts ...CreateOptionFunc) (MapInterface[E], error)

NewMapCache create a cache with Map

type Persistence

type Persistence int

Persistence policy

const (
	// FFB Full File Backup
	FFB Persistence = iota
)

Jump to

Keyboard shortcuts

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