cache

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultExpiration = 0
	NoExpiration      = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func NewCache

func NewCache(defaultExpiration time.Duration) *Cache

func (*Cache) Add

func (c *Cache) Add(k string, v interface{}, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (*Cache) Get

func (c *Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (*Cache) GetAll

func (c *Cache) GetAll() map[string]Item

func (*Cache) GetWithExpiration

func (c *Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (*Cache) Replace

func (c *Cache) Replace(k string, v interface{}, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (*Cache) Set

func (c *Cache) Set(k string, v interface{}, d time.Duration)

func (*Cache) SetDefault

func (c *Cache) SetDefault(k string, v interface{})

Add an item to the cache, replacing any existing item, using the default expiration.

type Item

type Item struct {
	Value      interface{}
	Expiration int64
}

Jump to

Keyboard shortcuts

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