cache

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

GoCache

Go Report Card Go.Dev reference Sourcegraph Release

Installation

Make sure you have a working Go environment (Go 1.18 or higher is required). See the install instructions.

To install GoCache, simply run:

go get github.com/pkg6/go-cache

Example

package main

import (
	"github.com/pkg6/go-cache"
)

func main() {
	c := cache.New()
	c.Extend(cache.FileCacheName, cache.NewFileCache())
	c.Set("cache", "test", 0)
	c.Get("cache")
	c.GetMulti([]string{"cache"})
	c.Delete("cache")
	c.Has("cache")
	c.Increment("cache_inc", 1)
	c.Decrement("cache_dec", 1)
	c.Clear()
	c.Pull("cache")
	c.Remember("cache", func() any {
		return "test1"
	}, 0)
}

Documentation

Index

Constants

View Source
const (
	MinUint32 uint32 = 0
	MinUint64 uint64 = 0

	FileCacheName     = "file"
	MemoryCacheName   = "memory"
	RedisCacheName    = "redis"
	MemcacheCacheName = "memcache"
	CacheName         = "go-cache"
)

Variables

View Source
var (
	ErrKeyExpired  = errors.New("the key is expired")
	ErrKeyNotExist = errors.New("the key isn't exist")
)
View Source
var (
	ErrIncrementOverflow = errors.New("this incr invocation will overflow")
	ErrDecrementOverflow = errors.New("this decr invocation will overflow")
	ErrNotIntegerType    = errors.New("item val is not (u)int (u)int32 (u)int64 float64 float32")
)
View Source
var (
	FileCachePath = os.TempDir()
)
View Source
var IndefiniteTime = (86400 * 365 * 20) * time.Second

Functions

func Decrement

func Decrement(originVal any, step int) (any, error)

Decrement Self decrement

func Increment

func Increment(originVal any, step int) (any, error)

Increment Autoincrement

Types

type Cache

type Cache interface {
	Name() string
	Set(key string, value any, ttl time.Duration) error
	Has(key string) (bool, error)
	GetMulti(keys []string) ([]any, error)
	Get(key string) (any, error)
	Delete(key string) error
	Increment(key string, step int) error
	Decrement(key string, step int) error
	Clear() error
}

func NewFileCache

func NewFileCache(opts ...FileCacheOptions) Cache

func NewMemoryCache

func NewMemoryCache(interval time.Duration) Cache

NewMemoryCache returns a new MemoryCache.

type CacheItem

type CacheItem struct {
	// data
	Data any `json:"data"`
	//expired ttl
	TTL time.Duration `json:"ttl"`
	// now data
	JoinTime time.Time `json:"join_time"`
	// expired data
	ExpirationTime time.Time `json:"expiration_time"`
	//Is it indefinite
	NeverExpires bool `json:"never_expires"`
}

func (*CacheItem) GetCacheItem

func (c *CacheItem) GetCacheItem(data any) (item ICacheItem, err error)

func (*CacheItem) GetData

func (c *CacheItem) GetData() any

func (*CacheItem) GetExpirationTime

func (c *CacheItem) GetExpirationTime() time.Time

func (*CacheItem) GetJoinTime

func (c *CacheItem) GetJoinTime() time.Time

func (*CacheItem) GetTTL

func (c *CacheItem) GetTTL() time.Duration

func (*CacheItem) IsNeverExpires

func (c *CacheItem) IsNeverExpires() bool

func (*CacheItem) SetCacheItem

func (c *CacheItem) SetCacheItem(data any, ttl time.Duration) (string, error)

type CacheManager

type CacheManager interface {
	Extend(cache Cache, names ...string) CacheManager
	Disk(name string) CacheManager
	Pull(key string) (any, error)
	Remember(key string, value any, ttl time.Duration) (any, error)
	Cache
}

func New

func New() CacheManager

func NewCache

func NewCache(caches ...Cache) CacheManager

type FileCache

type FileCache struct {
	Path      string
	CacheItem ICacheItem
}

func (*FileCache) Clear

func (f *FileCache) Clear() error

func (*FileCache) Decrement

func (f *FileCache) Decrement(key string, step int) error

func (*FileCache) Delete

func (f *FileCache) Delete(key string) error

func (*FileCache) DeleteMultiple

func (f *FileCache) DeleteMultiple(keys []string) error

func (*FileCache) Get

func (f *FileCache) Get(key string) (any, error)

func (*FileCache) GetMulti

func (f *FileCache) GetMulti(keys []string) ([]any, error)

func (*FileCache) Has

func (f *FileCache) Has(key string) (bool, error)

func (*FileCache) Increment

func (f *FileCache) Increment(key string, step int) error

func (*FileCache) Name

func (f *FileCache) Name() string

func (*FileCache) Set

func (f *FileCache) Set(key string, val any, ttl time.Duration) error

type FileCacheOptions

type FileCacheOptions func(c *FileCache)

func FileCacheWithCacheItem

func FileCacheWithCacheItem(cacheItem ICacheItem) FileCacheOptions

func FileCacheWithCachePath

func FileCacheWithCachePath(cachePath string) FileCacheOptions

type GoCache

type GoCache struct {
	Maps  map[string]Cache
	Names []string
	// contains filtered or unexported fields
}

func (*GoCache) Clear

func (f *GoCache) Clear() error

func (*GoCache) Decrement

func (f *GoCache) Decrement(key string, step int) error

func (*GoCache) Delete

func (f *GoCache) Delete(key string) error

func (*GoCache) Disk

func (f *GoCache) Disk(name string) CacheManager

func (*GoCache) Extend

func (f *GoCache) Extend(cache Cache, names ...string) CacheManager

Extend 扩展

func (*GoCache) FindAdapter

func (f *GoCache) FindAdapter() Cache

FindAdapter Find Adapter

func (*GoCache) Get

func (f *GoCache) Get(key string) (any, error)

func (*GoCache) GetMulti

func (f *GoCache) GetMulti(keys []string) ([]any, error)

func (*GoCache) Has

func (f *GoCache) Has(key string) (bool, error)

func (*GoCache) Increment

func (f *GoCache) Increment(key string, step int) error

func (*GoCache) Name

func (f *GoCache) Name() string

func (*GoCache) Pull

func (f *GoCache) Pull(key string) (any, error)

Pull 读取缓存并删除

func (*GoCache) Remember

func (f *GoCache) Remember(key string, value any, ttl time.Duration) (any, error)

func (*GoCache) Set

func (f *GoCache) Set(key string, value any, ttl time.Duration) error

type ICacheItem

type ICacheItem interface {
	SetCacheItem(data any, ttl time.Duration) (string, error)
	GetCacheItem(data any) (ICacheItem, error)
	GetExpirationTime() time.Time
	IsNeverExpires() bool
	GetTTL() time.Duration
	GetData() any
	GetJoinTime() time.Time
}

type MemoryCache

type MemoryCache struct {
	sync.RWMutex

	Interval time.Duration
	// contains filtered or unexported fields
}

func (*MemoryCache) Clear

func (m *MemoryCache) Clear() error

func (*MemoryCache) ClearExpiredKeys

func (m *MemoryCache) ClearExpiredKeys()

func (*MemoryCache) Decrement

func (m *MemoryCache) Decrement(key string, step int) error

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(key string) error

func (*MemoryCache) Get

func (m *MemoryCache) Get(key string) (any, error)

func (*MemoryCache) GetMulti

func (m *MemoryCache) GetMulti(keys []string) ([]any, error)

func (*MemoryCache) Has

func (m *MemoryCache) Has(key string) (bool, error)

func (*MemoryCache) Increment

func (m *MemoryCache) Increment(key string, step int) error

func (*MemoryCache) Name

func (m *MemoryCache) Name() string

func (*MemoryCache) Set

func (m *MemoryCache) Set(key string, value any, ttl time.Duration) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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