adapter

package
v0.0.0-...-15a7b9f Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_EXT = ".tmp"
)

Variables

This section is empty.

Functions

func New

func New(path string, ext ...string) gcache.Adapter

new adapter creates and returns a new cache object

Types

type FileCache

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

func (*FileCache) Clear

func (c *FileCache) Clear(ctx context.Context) error

Clear clears all data of the cache. Note that this function is sensitive and should be carefully used. 小心使用,将删除所有

func (*FileCache) Close

func (c *FileCache) Close(ctx context.Context) error

Close closes the cache if necessary.

func (*FileCache) Contains

func (c *FileCache) Contains(ctx context.Context, key interface{}) (bool, error)

Contains returns true if <key> exists in the cache, or else returns false.

func (*FileCache) Data

func (c *FileCache) Data(ctx context.Context) (map[interface{}]interface{}, error)

Data returns a copy of all key-value pairs in the cache as map type. Note that this function may leads lots of memory usage, you can implement this function if necessary.

func (*FileCache) Get

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

Get retrieves and returns the associated value of given <key>. It returns nil if it does not exist or its value is nil.

func (*FileCache) GetExpire

func (c *FileCache) GetExpire(ctx context.Context, key interface{}) (time.Duration, error)

GetExpire retrieves and returns the expiration of <key> in the cache.

It returns 0 if the <key> does not expire. It returns -1 if the <key> does not exist in the cache.

func (*FileCache) GetOrSet

func (c *FileCache) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (interface{}, error)

GetOrSet retrieves and returns the value of <key>, or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.

func (*FileCache) GetOrSetFunc

func (c *FileCache) GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFunc retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil, but it does nothing if <value> is a function and the function result is nil.

func (*FileCache) GetOrSetFuncLock

func (c *FileCache) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)

GetOrSetFuncLock retrieves and returns the value of <key>, or sets <key> with result of function <f> and returns its result if <key> does not exist in the cache. The key-value pair expires after <duration>.

It does not expire if <duration> == 0. It does nothing if function <f> returns nil.

Note that the function <f> should be executed within writing mutex lock for concurrent safety purpose.

func (*FileCache) Keys

func (c *FileCache) Keys(ctx context.Context) ([]interface{}, error)

Keys returns all keys in the cache as slice. 容易引起读写瓶颈,慎用

func (*FileCache) Remove

func (c *FileCache) Remove(ctx context.Context, keys ...interface{}) (value interface{}, err error)

Remove deletes one or more keys from cache, and returns its value. If multiple keys are given, it returns the value of the last deleted item.

func (*FileCache) Set

func (c *FileCache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

Set sets cache with <key>-<value> pair, which is expired after <duration>.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0.

func (*FileCache) SetIfNotExist

func (c *FileCache) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (bool, error)

SetIfNotExist sets cache with <key>-<value> pair which is expired after <duration> if <key> does not exist in the cache. It returns true the <key> dose not exist in the cache and it sets <value> successfully to the cache, or else it returns false.

The parameter <value> can be type of <func() interface{}>, but it dose nothing if its result is nil.

It does not expire if <duration> == 0. It deletes the <key> if <duration> < 0 or given <value> is nil.

func (*FileCache) Sets

func (c *FileCache) Sets(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error

Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.

It does not expire if <duration> == 0. It deletes the keys of <data> if <duration> < 0 or given <value> is nil.

func (*FileCache) Size

func (c *FileCache) Size(ctx context.Context) (size int, err error)

Size returns the number of items in the cache.

func (*FileCache) Update

func (c *FileCache) Update(ctx context.Context, key interface{}, value interface{}) (oldValue interface{}, exist bool, err error)

Update updates the value of <key> without changing its expiration and returns the old value. The returned value <exist> is false if the <key> does not exist in the cache.

It deletes the <key> if given <value> is nil. It does nothing if <key> does not exist in the cache.

func (*FileCache) UpdateExpire

func (c *FileCache) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)

UpdateExpire updates the expiration of <key> and returns the old expiration duration value.

It returns -1 and does nothing if the <key> does not exist in the cache. It deletes the <key> if <duration> < 0.

func (*FileCache) Values

func (c *FileCache) Values(ctx context.Context) ([]interface{}, error)

Values returns all values in the cache as slice.

Jump to

Keyboard shortcuts

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