cache

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Map sync.Map
	C   chan interface{}
	// contains filtered or unexported fields
}

Cache defines the methods LoadWithUpdate and Store, the user needs to define the function `update`.

func New

func New(name string, expired time.Duration, update Update) *Cache

New returns the *Cache. the function update, if found new *item, returns true, and stores it; else returns false, and delete the key from cache.

func (*Cache) Load

func (c *Cache) Load(key interface{}) (interface{}, bool)

Load loads the value from cache.

func (*Cache) LoadWithUpdate

func (c *Cache) LoadWithUpdate(key interface{}) (interface{}, bool)

LoadWithUpdate loads the cached item. If the item is not cached, it returns the newest and caches the new item. If the time is cached, it returns the item. if the cached item is expired, it returns the current item and tries to cache the newest.

func (*Cache) LoadWithUpdateSync

func (c *Cache) LoadWithUpdateSync(key interface{}) (interface{}, bool)

LoadWithUpdateSync loads the cached item. If the item is not cached, it returns the newest and caches the new item. If the item is cached, it returns the item. If the cached item is expired, it tries to retrieve the newest then caches and returns it.

func (*Cache) Name

func (c *Cache) Name() string

Name returns the Cache object's name

func (*Cache) Store

func (c *Cache) Store(key interface{}, value interface{})

Store caches the key and value, and updates its expired time.

type Update

type Update func(interface{}) (interface{}, bool)

Update retrieves the caching item

Jump to

Keyboard shortcuts

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