gocache

package
v0.0.0-...-911af99 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NoExpiration is for item without expiration time.
	NoExpiration time.Duration = -1
	// DefaultExpiration is for default expiration time.
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is the cache entity.

func NewCache

func NewCache(defaultExpiration, gcInterval time.Duration) *Cache

NewCache creates a new cache and starts the gcLoop.

func (*Cache) Add

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

Add adds a new item to cache if it doesn't exist.

func (*Cache) Clear

func (c *Cache) Clear()

Clear clears all items.

func (*Cache) Count

func (c *Cache) Count() int

Count returns the number of items.

func (*Cache) Delete

func (c *Cache) Delete(k string)

Delete deletes the key k and its item.

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

DeleteExpired deletes the expired items.

func (*Cache) Get

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

Get returns the item and true if the key exists.

func (*Cache) Load

func (c *Cache) Load(r io.Reader) error

Load reads the cache from io.Reader.

func (*Cache) LoadFromFile

func (c *Cache) LoadFromFile(file string) error

LoadFromFile loads the cache from a local file.

func (*Cache) Replace

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

Replace replaces the existed item with key k if it exists.

func (*Cache) Save

func (c *Cache) Save(w io.Writer) (err error)

Save writes the cache to io.Writer.

func (*Cache) SaveToFile

func (c *Cache) SaveToFile(file string) error

SaveToFile saves the cache to a local file.

func (*Cache) Set

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

Set sets an item whether it exists.

func (*Cache) StopGc

func (c *Cache) StopGc()

StopGc stops gcLoop.

type Item

type Item struct {
	Object     interface{} // Data
	Expiration int64       // Expiration time
}

Item is where the cache data item stored.

func (Item) Expired

func (item Item) Expired() bool

Expired returns true if the item has expired.

Jump to

Keyboard shortcuts

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