cache

package
v0.0.0-...-348b0d6 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: GPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache defined the interface of cache and implemented and LRU Cache

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func(key, val interface{}) bool

CloseFunc is a func

type ICache

type ICache interface {
	Add(key, val interface{})
	Remove(key interface{}) interface{}
	Get(key interface{}) interface{}
	Size() uint
	Reset()
	ResetWithFunc(closeFunc CloseFunc) bool
}

ICache is the interface of a Cache with operations like CRUD, Size, Reset, etc.

type LRU

type LRU interface {
	ICache
	PopOldest() interface{}
}

type LRUCache

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

LRUCache is Lastest Recently Used Cache

func NewLRUCache

func NewLRUCache(capacity uint) *LRUCache

NewLRUCache create a cache and return the pointer of it

func (*LRUCache) Add

func (c *LRUCache) Add(key interface{}, obj interface{})

Add a element into Cache combile key,obj as entry, insert entry as the list.Element.Value

func (*LRUCache) Get

func (c *LRUCache) Get(key interface{}) interface{}

Get the obj by key

func (*LRUCache) PopNewest

func (c *LRUCache) PopNewest() interface{}

PopNewest pops the Newest element in LRU return nil if LRU is empty

func (*LRUCache) PopOldest

func (c *LRUCache) PopOldest() interface{}

PopOldest pops the oldest element in LRU return nil if LRU is empty

func (*LRUCache) Remove

func (c *LRUCache) Remove(key interface{}) interface{}

Remove an entry by key return the obj it add

func (*LRUCache) Reset

func (c *LRUCache) Reset()

Reset removes all elements in LRU Cache

func (*LRUCache) ResetWithFunc

func (c *LRUCache) ResetWithFunc(closeFunc CloseFunc) bool

ResetWithFunc delete elements in LRU one by one from the oldest to the lastest and calls the closeFunc if one close func returns false then ResetWithFunc returns false

func (*LRUCache) Size

func (c *LRUCache) Size() uint

Size function returns the num of elements exist in LRU

type ThreadSafeLRUWrapper

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

ThreadSafeLRUWrapper is simply a lru plus one mutex to make it thread safe

func NewThreadSafeLRU

func NewThreadSafeLRU(lru LRU) *ThreadSafeLRUWrapper

NewThreadSafeLRU init a lru

func (*ThreadSafeLRUWrapper) Add

func (l *ThreadSafeLRUWrapper) Add(key, obj interface{})

Add an obj with key

func (*ThreadSafeLRUWrapper) Get

func (l *ThreadSafeLRUWrapper) Get(key interface{}) interface{}

Get an obj by key return nil if key is not in LRU

func (*ThreadSafeLRUWrapper) PopOldest

func (l *ThreadSafeLRUWrapper) PopOldest() interface{}

PopOldest pops the oldest element in LRU return nil if LRU is empty

func (*ThreadSafeLRUWrapper) Remove

func (l *ThreadSafeLRUWrapper) Remove(key interface{}) interface{}

Remove an entry by key return the obj it add

func (*ThreadSafeLRUWrapper) Reset

func (l *ThreadSafeLRUWrapper) Reset()

Reset simply re-init the map and the Doubly list

func (*ThreadSafeLRUWrapper) ResetWithFunc

func (l *ThreadSafeLRUWrapper) ResetWithFunc(closeFunc CloseFunc) bool

ResetWithFunc pops elements and calls closeFunc one by one

func (*ThreadSafeLRUWrapper) Size

func (l *ThreadSafeLRUWrapper) Size() uint

Size returns the size of LRU

Jump to

Keyboard shortcuts

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