filecache

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FileCachePath           = "cache"     // cache directory
	FileCacheFileSuffix     = ".bin"      // cache file suffix
	FileCacheDirectoryLevel = 2           // cache file deep level if auto generated cache files.
	FileCacheEmbedExpiry    time.Duration // cache expire time, default is no expire forever.
)

FileCache Config

Functions

func FileGetContents

func FileGetContents(filename string) ([]byte, error)

FileGetContents Reads bytes from a file. if non-existent, create this file.

func FilePutContents

func FilePutContents(filename string, content []byte) error

FilePutContents puts bytes into a file. if non-existent, create this file.

func GobDecode

func GobDecode(data []byte, to *FileCacheItem) error

GobDecode Gob decodes a file cache item.

func GobEncode

func GobEncode(data interface{}) ([]byte, error)

GobEncode Gob encodes a file cache item.

func NewFileCache

func NewFileCache() cache.Cache

NewFileCache creates a new file cache with no config. The level and expiry need to be set in the method StartAndGC as config string.

Types

type FileCache

type FileCache struct {
	CachePath      string
	FileSuffix     string
	DirectoryLevel int
	EmbedExpiry    int
}

FileCache is cache adapter for file storage.

func (*FileCache) ClearAll

func (fc *FileCache) ClearAll(context.Context) error

ClearAll cleans cached files (not implemented)

func (*FileCache) Decr

func (fc *FileCache) Decr(ctx context.Context, key string) error

Decr decreases cached int value.

func (*FileCache) Delete

func (fc *FileCache) Delete(ctx context.Context, key string) error

Delete file cache value.

func (*FileCache) Fetch

func (fc *FileCache) Fetch(ctx context.Context, keys []string) ([]any, error)

Fetch gets values from file cache. if nonexistent or expired return an empty string.

func (*FileCache) Get

func (fc *FileCache) Get(ctx context.Context, key string) (interface{}, error)

Get value from file cache. if nonexistent or expired return an empty string.

func (*FileCache) Incr

func (fc *FileCache) Incr(ctx context.Context, key string) error

Incr increases cached int value. fc value is saved forever unless deleted.

func (*FileCache) Init

func (fc *FileCache) Init() error

Init makes new a dir for file cache if it does not already exist

func (*FileCache) IsExist

func (fc *FileCache) IsExist(ctx context.Context, key string) (bool, error)

IsExist checks if value exists.

func (*FileCache) Put

func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeout ...time.Duration) error

Put value into file cache. timeout: how long this file should be kept in ms if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.

func (*FileCache) StartAndGC

func (fc *FileCache) StartAndGC(config string) error

StartAndGC starts gc for file cache. config must be in the format {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}

type FileCacheItem

type FileCacheItem struct {
	Data       interface{}
	Lastaccess time.Time
	Expired    time.Time
}

FileCacheItem is basic unit of file cache adapter which contains data and expire time.

Jump to

Keyboard shortcuts

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