Documentation ¶
Overview ¶
Package cache 简单的内存缓存.
如果需要复杂的功能可以使用 github.com/allegro/bigcache 或 https://github.com/coocood/freecache.
Index ¶
- Variables
- type Cache
- func (p *Cache[K, V]) Clean(overdue time.Time) error
- func (p *Cache[K, V]) Close()
- func (p *Cache[K, V]) Del(keys ...K) error
- func (p *Cache[K, V]) Get(key K) (V, bool)
- func (p *Cache[K, V]) GetOnly(key K) (V, bool)
- func (p *Cache[K, V]) GetOrLoad(key K, loader func(key K) V) (V, bool)
- func (p *Cache[K, V]) GetOrSet(key K, value V) (V, bool)
- func (p *Cache[K, V]) GetString(key K) (string, bool)
- func (p *Cache[K, V]) Keys() []K
- func (p *Cache[K, V]) Overdue(overdue time.Time) []K
- func (p *Cache[K, V]) Reset(key K) error
- func (p *Cache[K, V]) Set(key K, value V) error
- func (p *Cache[K, V]) SetByDuration(key K, value V, expire time.Duration) error
- func (p *Cache[K, V]) SetByTime(key K, value V, expire time.Time) error
- func (p *Cache[K, V]) Size() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrClose = errors.New("cache is close")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { Expire time.Duration Callback func(key K, value V) // contains filtered or unexported fields }
Cache support LRU (Least Recently Used). nolint
Example ¶
package main import ( "fmt" "time" "github.com/xuender/oils/dbs/cache" ) func main() { data := cache.NewCache[string, string](3 * time.Second) _ = data.Set("key1", "value1") _ = data.SetByDuration("key2", "value2", time.Second) _ = data.Set("key3", "value3") fmt.Println(data.Get("key1")) fmt.Println("init size:", data.Size()) time.Sleep(2 * time.Second) fmt.Println(data.Get("key3")) // reset expire time. fmt.Println("2 second:", data.Size()) time.Sleep(2 * time.Second) fmt.Println("4 second:", data.Size()) }
Output: value1 true init size: 3 value3 true 2 second: 2 4 second: 1
func NewCache ¶
func NewCache[K comparable, V any](expire time.Duration) *Cache[K, V]
NewCache new cache. nolint: varnamelen
func NewCallbackCache ¶
func NewCallbackCache[K comparable, V any]( expire time.Duration, callback func(key K, value V), ) *Cache[K, V]
NewCallbackCache new have del callback cache.
Example ¶
package main import ( "fmt" "time" "github.com/xuender/oils/dbs/cache" ) func main() { data := cache.NewCallbackCache(3*time.Second, func(key, value string) { fmt.Println("del:", key, value) }) _ = data.Set("key1", "value1") _ = data.SetByDuration("key2", "value2", time.Second) _ = data.Set("key3", "value3") fmt.Println(data.Get("key1")) fmt.Println("init size:", data.Size()) time.Sleep(2 * time.Second) fmt.Println(data.Get("key3")) // reset expire time. fmt.Println("2 second:", data.Size()) time.Sleep(2 * time.Second) fmt.Println("4 second:", data.Size()) }
Output: value1 true init size: 3 value3 true del: key2 value2 2 second: 2 del: key1 value1 4 second: 1
func (*Cache[K, V]) SetByDuration ¶
SetByDuration value by key.
Click to show internal directories.
Click to hide internal directories.