cache

package
v0.0.0-...-335f5af Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCacheType set default cache type for NewDefaultCache function
	DefaultCacheType = LRUCache
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Put puts an item into cache.
	Put(key uint64, value interface{})
	// Get retrives an item from cache.
	Get(key uint64) (interface{}, bool)
	// Peek reads an item from cache. The action is no considered 'Use'.
	Peek(key uint64) (interface{}, bool)
	// Remove eliminates an item from cache.
	Remove(key uint64)
	// Elems return all items in cache.
	Elems() []*Item
	// Len returns current cache size
	Len() int
}

Cache is an interface for cache system

func NewCache

func NewCache(size int, cacheType Type) Cache

NewCache create Cache instance by CacheType

func NewDefaultCache

func NewDefaultCache(size int) Cache

NewDefaultCache create Cache instance by default cache type

type FIFO

type FIFO struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

FIFO is 'First-In-First-Out' cache.

func NewFIFO

func NewFIFO(maxCount int) *FIFO

NewFIFO returns a new FIFO cache.

func (*FIFO) Elems

func (c *FIFO) Elems() []*Item

Elems returns all items in cache.

func (*FIFO) FromElems

func (c *FIFO) FromElems(key uint64) []*Item

FromElems returns all items that has a key greater than the specified one.

func (*FIFO) Len

func (c *FIFO) Len() int

Len returns current cache size.

func (*FIFO) Put

func (c *FIFO) Put(key uint64, value interface{})

Put puts an item into cache.

func (*FIFO) Remove

func (c *FIFO) Remove()

Remove takes the oldest item out.

type Item

type Item struct {
	Key   uint64
	Value interface{}
}

Item is the cache entry.

type LRU

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

LRU is 'Least-Recently-Used' cache.

func (*LRU) Elems

func (c *LRU) Elems() []*Item

Elems return all items in cache.

func (*LRU) Get

func (c *LRU) Get(key uint64) (interface{}, bool)

Get retrives an item from cache.

func (*LRU) Len

func (c *LRU) Len() int

Len returns current cache size.

func (*LRU) Peek

func (c *LRU) Peek(key uint64) (interface{}, bool)

Peek reads an item from cache. The action is no considerd 'Use'.

func (*LRU) Put

func (c *LRU) Put(key uint64, value interface{})

Put puts an item into cache.

func (*LRU) Remove

func (c *LRU) Remove(key uint64)

Remove eliminates an item from cache.

type TTL

type TTL struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

TTL is a cache that assigns TTL(Time-To-Live) for each items.

func NewTTL

func NewTTL(gcInterval time.Duration, ttl time.Duration) *TTL

NewTTL returns a new TTL cache.

func (*TTL) Get

func (c *TTL) Get(key uint64) (interface{}, bool)

Get retrives an item from cache.

func (*TTL) Len

func (c *TTL) Len() int

Len returns current cache size.

func (*TTL) Put

func (c *TTL) Put(key uint64, value interface{})

Put puts an item into cache.

func (*TTL) PutWithTTL

func (c *TTL) PutWithTTL(key uint64, value interface{}, ttl time.Duration)

PutWithTTL puts an item into cache with specified TTL.

func (*TTL) Remove

func (c *TTL) Remove(key uint64)

Remove eliminates an item from cache.

type TTLUint64

type TTLUint64 struct {
	*TTL
}

TTLUint64 is simple TTL saves only uint64s.

func NewIDTTL

func NewIDTTL(gcInterval, ttl time.Duration) *TTLUint64

NewIDTTL creates a new TTLUint64 cache.

func (*TTLUint64) Exists

func (c *TTLUint64) Exists(id uint64) bool

Exists checks if an ID exists in cache.

func (*TTLUint64) Put

func (c *TTLUint64) Put(id uint64)

Put saves an ID in cache.

type Type

type Type int

Type is cache's type such as LRUCache and etc.

const (
	// LRUCache is LRU cache type
	LRUCache Type = 1
)

Jump to

Keyboard shortcuts

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