cache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	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 func(interface{}) (*Item, bool)) *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) LoadWithUpdate

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

LoadWithUpdate loads the cached item. if the item is not cached, it returns the newest and cache the new item. if the item is expired, it returns the cached item and try to cache the newest. if the item is not expired, it returns it and do nothing.

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 *Item)

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

type Item

type Item struct {
	Object interface{}
	// contains filtered or unexported fields
}

Item contains the item be cached

func (*Item) IsExpired

func (i *Item) IsExpired() bool

IsExpired returns whether the item is expired

Jump to

Keyboard shortcuts

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