gcache

package module
v0.3.1 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 AppendToSlice added in v0.3.1

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

Append scalar to an existing slice cache

func Decrease added in v0.3.1

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

func Delete added in v0.3.1

func Delete(c *Cache, key string)

func DeleteFromMap added in v0.3.1

func DeleteFromMap[T ScalarType](c *Cache, key string, name string) error

Delete value from an existing map

func DeleteKeys added in v0.3.1

func DeleteKeys(c *Cache, keys []string)

func Exists

func Exists(c *Cache, key string) bool

func Get

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

WARNING: If value is in SliceType or MapType, the operation on the returned value is not thread-safe.

func GetMapCopy added in v0.3.1

func GetMapCopy[T ScalarType](c *Cache, key string) (retV map[string]T, retErr error)

Note: Thread-safe but expensive

func GetSliceCopy added in v0.3.1

func GetSliceCopy[T ScalarType](c *Cache, key string) (retV []T, retErr error)

Note: thread-safe but expensive

func GetTTL

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

func GetWithTTL added in v0.3.1

func GetWithTTL[T ValType](c *Cache, key string) (T, time.Duration, error)

func Increase added in v0.3.1

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

func InsertToMap added in v0.3.1

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

Insert scalar to an existing map cache

func Keys added in v0.3.1

func Keys(c *Cache) []string

func Len added in v0.3.1

func Len(c *Cache) int

could all items which may include the expired items

func LenValid added in v0.3.1

func LenValid(c *Cache) int

count only unexpired items, more expensive than TotalItems

func Set

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

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

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 MapType added in v0.3.1

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

Map types are not recommended to use.

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
}

Slice types are not recommended to use.

type ValType

type ValType interface {
	ScalarType | SliceType | MapType
}

Jump to

Keyboard shortcuts

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