lrucache

package module
v0.0.0-...-d065d2e Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: MIT Imports: 3 Imported by: 1

README

  • 不再维护,使用更好的平替🦄【ecache 】轻量本地内存缓存
    • 🤏 代码量<300行、30s完成接入
    • 🚀 高性能、极简设计、并发安全
    • 🏳️‍🌈 支持LRULRU-2两种模式
    • 🦖 额外小组件支持分布式一致性

  • No more maintenance, please use better 🦄【ecache】 instead.
    • 🤏 Less than 300 lines, cost only ~30s to assemble
    • 🚀 Extremely easy, ultra fast and concurrency-safe
    • 🏳️‍🌈 Support both LRU mode and LRU-2 mode inside
    • 🦖 Extra plugin that support distributed consistency

lrucache

license Go Report Card

Result at leetcode.com

Usage

	lc := lrucache.New(5)
	lc.Put(4, "1")
	lc.Put(1, "2")
	lc.Put(2, "3")
	lc.Put(3, "4")
	lc.Put(4, "5")

	fmt.Println("Range demo:")
	i := 1
	lc.Range(func(key, value interface{}) bool {
		fmt.Printf("[%d] %d => %s\r\n", i, key.(int), value.(string))
		i++
		return true
	})

	fmt.Println("Get demo:")
	if e, ok := lc.Get(1); ok {
		fmt.Printf("%d => %s\r\n", 1, e.(string))
	}

	fmt.Println("Reverse iteration after Get demo:")
	i = 1
	for e := lc.Back(); e != nil; e = e.Prev() {
		fmt.Printf("[%d] %d => %s\r\n", lc.Len()-i+1, e.Key.(int), e.Value.(string))
		i++
	}

	lc.Delete(4)
	fmt.Println("Iteration after Delete demo:")
	i = 1
	for e := lc.Front(); e != nil; e = e.Next() {
		fmt.Printf("[%d] %d => %s\r\n", i, e.Key.(int), e.Value.(string))
		i++
	}

Output:


Range demo:
[1] 4 => 5
[2] 3 => 4
[3] 2 => 3
[4] 1 => 2
Get demo:
1 => 2
Reverse iteration after Get demo:
[4] 2 => 3
[3] 3 => 4
[2] 4 => 5
[1] 1 => 2
Iteration after Delete demo:
[1] 1 => 2
[2] 3 => 4
[3] 2 => 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	Key   interface{}
	Value interface{}
	// contains filtered or unexported fields
}

Element - node to store cache item

func (*Element) Next

func (e *Element) Next() *Element

Next - fetch older element

func (*Element) Prev

func (e *Element) Prev() *Element

Prev - fetch newer element

type LRUCache

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

LRUCache - a data structure that is efficient to insert/fetch/delete cache items [both O(1) time complexity]

func New

func New(capacity int) *LRUCache

New - create a new lru cache object

func (*LRUCache) Back

func (lc *LRUCache) Back() *Element

Back - get back element of lru cache

func (*LRUCache) Capacity

func (lc *LRUCache) Capacity() int

Capacity - capacity of lru cache

func (*LRUCache) Delete

func (lc *LRUCache) Delete(key interface{})

Delete - delete item by key from lru cache

func (*LRUCache) Front

func (lc *LRUCache) Front() *Element

Front - get front element of lru cache

func (*LRUCache) Get

func (lc *LRUCache) Get(key interface{}) (interface{}, bool)

Get - get value of key from lru cache with result

func (*LRUCache) Len

func (lc *LRUCache) Len() int

Len - length of lru cache

func (*LRUCache) Put

func (lc *LRUCache) Put(key interface{}, value interface{})

Put - put a cache item into lru cache

func (*LRUCache) Range

func (lc *LRUCache) Range(f func(key, value interface{}) bool)

Range - calls f sequentially for each key and value present in the lru cache

func (*LRUCache) Update

func (lc *LRUCache) Update(key interface{}, f func(value *interface{}))

Update - inplace update

type SyncCache

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

SyncCache - concurrent cache structure

func NewSyncCache

func NewSyncCache(capacity int, bucket int, timeout int64) *SyncCache

NewSyncCache - create sync cache `capacity` is lru cache length of each bucket store `capacity * bucket` count of element in SyncCache at most `timeout` is in seconds

func (*SyncCache) Delete

func (sc *SyncCache) Delete(key string)

Delete - delete item by key from sync cache

func (*SyncCache) Get

func (sc *SyncCache) Get(key string) (interface{}, bool)

Get - get value of key from sync cache with result

func (*SyncCache) Put

func (sc *SyncCache) Put(key string, value interface{})

Put - put a cache item into sync cache

Jump to

Keyboard shortcuts

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