cache

package
v0.0.0-...-0378f4a Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2018 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedResponse

type CachedResponse struct {
	Resp *http.Response
}

func (*CachedResponse) Size

func (cp *CachedResponse) Size() int

type Item

type Item struct {
	Key   string
	Value CachedResponse
}

Item is what is stored in the cache

type LRUCache

type LRUCache struct {
	OnEviction func(key string)
	// contains filtered or unexported fields
}

LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the Size() of each item.

func NewLRUCache

func NewLRUCache(capacity int64, expiry time.Duration) *LRUCache

NewLRUCache creates a new empty cache with the given capacity.

func (*LRUCache) Capacity

func (lru *LRUCache) Capacity() int64

Capacity returns the cache maximum capacity.

func (*LRUCache) Delete

func (lru *LRUCache) Delete(key string) bool

Delete removes an entry from the cache, and returns if the entry existed.

func (*LRUCache) Get

func (lru *LRUCache) Get(key string) (v *CachedResponse, ok bool)

Get returns a value from the cache, and marks the entry as most recently used.

func (*LRUCache) Length

func (lru *LRUCache) Length() int64

Length returns how many elements are in the cache

func (*LRUCache) Reset

func (lru *LRUCache) Reset()

rest deletes all the entries from the cache.

func (*LRUCache) Set

func (lru *LRUCache) Set(key string, value *CachedResponse)

Set sets a value in the cache.

func (*LRUCache) SetIfAbsent

func (lru *LRUCache) SetIfAbsent(key string, value *CachedResponse)

SetIfAbsent will set the value in the cache if not present. If the value exists in the cache, we don't set it.

func (*LRUCache) Size

func (lru *LRUCache) Size() int64

Size returns the sum of the objects' Size() method.

func (*LRUCache) StartGC

func (c *LRUCache) StartGC()

StartGC starts running a routine ticking at expiry interval, on each interval this routine does a sweep across the cache entries and garbage collects all the expired entries.

func (*LRUCache) Stats

func (lru *LRUCache) Stats() (length, size, capacity int64, oldest time.Time)

Stats returns a few stats on the cache.

func (*LRUCache) StopGC

func (c *LRUCache) StopGC()

StopGC sends a message to the expiry routine to stop expiring cached entries. NOTE: once this is called, cached entries will not be expired, be careful if you are using this.

Jump to

Keyboard shortcuts

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