gcache

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: MIT Imports: 7 Imported by: 0

README

gcache

Simple pure go in-memory key-value cache with persistent storage.

Documentation

Index

Constants

View Source
const (
	NEVER_EXPIRE             time.Duration = -1
	DEFAULT_CLEANUP_INTERVAL time.Duration = time.Minute * 5
	DEFAULT_PERSIST_INTERVAL time.Duration = time.Minute * 2
)

Variables

View Source
var (
	ErrNotExists   = errors.New("key not exists")
	ErrInvalidType = errors.New("invalid type")
)

Functions

func Append

func Append[T ScalarType](c *Cache, key string, val T) ([]T, error)

func Dec

func Dec[T NumType](c *Cache, key string, val T) (T, error)

func Del

func Del(c *Cache, key string)

func Exists

func Exists(c *Cache, key string) bool

func Get

func Get[T ValType](c *Cache, key string) (T, error)

func GetTTL

func GetTTL(c *Cache, key string) (time.Duration, error)

func Inc

func Inc[T NumType](c *Cache, key string, val T) (T, error)

func Set

func Set[T ValType](c *Cache, key string, val T, ttl time.Duration)

func TotalItems

func TotalItems(c *Cache) int

could all items which may include the expired items

func TotalValidItems

func TotalValidItems(c *Cache) int

count only unexpired items, more expensive than TotalItems

Types

type Cache

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

func New

func New(ctx context.Context, cleanupInterval, persistInterval time.Duration, persister Persister) (*Cache, error)

func (*Cache) Close

func (c *Cache) Close() error

func (*Cache) Del

func (c *Cache) Del(key string)

func (*Cache) Exists

func (c *Cache) Exists(key string) bool

func (*Cache) Get

func (c *Cache) Get(key string) (any, error)

func (*Cache) GetTTL

func (c *Cache) GetTTL(key string) (time.Duration, error)

func (*Cache) Set

func (c *Cache) Set(key string, val any, ttl time.Duration)

func (*Cache) TotalItems

func (c *Cache) TotalItems() int

func (*Cache) TotalValidItems

func (c *Cache) TotalValidItems() int

type FilePersister added in v0.2.0

type FilePersister struct {
	FilePath string
}

func (*FilePersister) Load added in v0.2.0

func (p *FilePersister) Load() (map[string]Item, error)

func (*FilePersister) Save added in v0.2.0

func (p *FilePersister) Save(items map[string]Item) error

type Item

type Item struct {
	Object   interface{}
	ExpireMs int64 // expiration time in ms, never expire if equals to `kNoExpiration`
}

type NumType

type NumType interface {
	float32 | float64 |
		int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64
}

type Persister

type Persister interface {
	Load() (map[string]Item, error)
	Save(items map[string]Item) error
}

type ScalarType

type ScalarType interface {
	string | bool | NumType
}

type SliceType

type SliceType interface {
	[]string | []bool |
		[]float32 | []float64 |
		[]int | []int8 | []int16 | []int32 | []int64 |
		[]uint | []uint8 | []uint16 | []uint32 | []uint64
}

type ValType

type ValType interface {
	ScalarType | SliceType
}

Jump to

Keyboard shortcuts

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