Documentation ¶
Overview ¶
Package cache defined the interface of cache and implemented and LRU Cache
Index ¶
- type CloseFunc
- type ICache
- type LRU
- type LRUCache
- func (c *LRUCache) Add(key interface{}, obj interface{})
- func (c *LRUCache) Get(key interface{}) interface{}
- func (c *LRUCache) PopNewest() interface{}
- func (c *LRUCache) PopOldest() interface{}
- func (c *LRUCache) Remove(key interface{}) interface{}
- func (c *LRUCache) Reset()
- func (c *LRUCache) ResetWithFunc(closeFunc CloseFunc) bool
- func (c *LRUCache) Size() uint
- type ThreadSafeLRUWrapper
- func (l *ThreadSafeLRUWrapper) Add(key, obj interface{})
- func (l *ThreadSafeLRUWrapper) Get(key interface{}) interface{}
- func (l *ThreadSafeLRUWrapper) PopOldest() interface{}
- func (l *ThreadSafeLRUWrapper) Remove(key interface{}) interface{}
- func (l *ThreadSafeLRUWrapper) Reset()
- func (l *ThreadSafeLRUWrapper) ResetWithFunc(closeFunc CloseFunc) bool
- func (l *ThreadSafeLRUWrapper) Size() uint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is Lastest Recently Used Cache
func NewLRUCache ¶
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) 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) ResetWithFunc ¶
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
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