cache

package
v1.0.51 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 6 Imported by: 0

README

cache

简单的内存缓存

特点

  • 线程安全
  • 支持泛型
  • 支持LRU
  • 删除过期数据时可触发回调方法
  • 主动清理过期数据
  • 不使用后台携程清理过期数据

缺点

不自动清理过期数据,需要主动调用清理方法或获取某个过期数据时,才清理

Documentation

Overview

Package cache 简单的内存缓存.

如果需要复杂的功能可以使用 github.com/allegro/bigcache 或 https://github.com/coocood/freecache.

Index

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]) Clean

func (p *Cache[K, V]) Clean(overdue time.Time) error

Clean overdue.

func (*Cache[K, V]) Close

func (p *Cache[K, V]) Close()

func (*Cache[K, V]) Del

func (p *Cache[K, V]) Del(keys ...K) error

Del key.

func (*Cache[K, V]) Get

func (p *Cache[K, V]) Get(key K) (V, bool)

Get 获取值并修改过期时间.

func (*Cache[K, V]) GetOnly

func (p *Cache[K, V]) GetOnly(key K) (V, bool)

GetOnly 只获取不修改过期时间.

func (*Cache[K, V]) GetOrLoad

func (p *Cache[K, V]) GetOrLoad(key K, loader func(key K) V) (V, bool)

GetOrLoad 获取或加载.

func (*Cache[K, V]) GetOrSet

func (p *Cache[K, V]) GetOrSet(key K, value V) (V, bool)

GetOrSet 获取或设置.

func (*Cache[K, V]) GetString

func (p *Cache[K, V]) GetString(key K) (string, bool)

GetString by key.

func (*Cache[K, V]) Keys

func (p *Cache[K, V]) Keys() []K

Keys by map.

func (*Cache[K, V]) Overdue

func (p *Cache[K, V]) Overdue(overdue time.Time) []K

Overdue keys.

func (*Cache[K, V]) Reset

func (p *Cache[K, V]) Reset(key K) error

Reset expire by key.

func (*Cache[K, V]) Set

func (p *Cache[K, V]) Set(key K, value V) error

Set value by key.

func (*Cache[K, V]) SetByDuration

func (p *Cache[K, V]) SetByDuration(key K, value V, expire time.Duration) error

SetByDuration value by key.

func (*Cache[K, V]) SetByTime

func (p *Cache[K, V]) SetByTime(key K, value V, expire time.Time) error

SetByTime value by key.

func (*Cache[K, V]) Size

func (p *Cache[K, V]) Size() int

Size by Cache.

Jump to

Keyboard shortcuts

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