cache

package
v0.0.0-...-16f4573 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2015 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	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
}

func NewCache

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

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func (Cache) Add

func (c Cache) Add(k string, x 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) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

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) ItemCount

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up. Equivalent to len(c.Items()).

func (Cache) Items

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

Returns the items in the cache. This may include items that have expired, but have not yet been cleaned up. If this is significant, the Expiration fields of the items should be checked. Note that explicit synchronization is needed to use a cache and its corresponding Items() return value at the same time, as the map is shared.

func (Cache) Load

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

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Refresh

func (c Cache) Refresh(k string, d time.Duration) bool

Refresh expiration attribute for the cache key only if it already exists. Returns an error otherwise.

func (Cache) RegExpiredHandler

func (c Cache) RegExpiredHandler(eh ExpiredHandler)

func (Cache) Save

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

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Set

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

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

type ExpiredHandler

type ExpiredHandler func(dm map[string]interface{})

type Item

type Item struct {
	Object     interface{}
	Expiration *time.Time
}

func (*Item) Expired

func (item *Item) Expired() bool

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