lru_cache

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 4 Imported by: 0

README

lru-cahce

Golang 内存缓存实现:

  • expire 过期剔除
  • capacity lru 基于容量的LRU剔除

Example

// new cache instance
c := NewCache(5*time.Minute, 10000)

// easy set
c.Set("key", "value")

// set the value, size and expiration with a key
c.SetEx("key","value", 5, 5*time.Minute)
v, exist := c.Get("key")
if !exist {
	// not exist
}
fmt.Printf("Cache hit: %d, cache miss: %d", c.HitNumber(), c.MissNumber())

Performance

Set

Cached number Concurrent Performance
500,000 100 1375 ns/op
1,000,000 100 1456 ns/op
5,000,000 100 1496 ns/op
10,000,000 100 1751 ns/op
50,000,000 100 2264 ns/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func NewCache

func NewCache(defaultExpireTime time.Duration, capacity int) *Cache

NewCache create a new Cache instance, you can set the capacity to 0 if you don't want to limit the cache size

Example
c := NewCache(5*time.Minute, 10000)
c.Set("key", "value")
v, exist := c.Get("key")
if !exist {
	// not exist
}
fmt.Println(v)
fmt.Printf("Cache hit: %d, cache miss: %d", c.HitNumber(), c.MissNumber())
Output:

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get gets the value of key. if not exist will return ( nil, false)

func (*Cache) HitNumber

func (c *Cache) HitNumber() int64

HitNumber returns the hit number.

func (*Cache) MissNumber

func (c *Cache) MissNumber() int64

MissNumber returns the miss number.

func (*Cache) Remove added in v1.0.2

func (c *Cache) Remove(key string)

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

Set is an easy set method. Set a key-value to memory with default expire time and size 1

func (*Cache) SetEx

func (c *Cache) SetEx(key string, value interface{}, size int, expire time.Duration)

SetEx sets a key-value to memory with size and expire time.

Jump to

Keyboard shortcuts

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