cache

package
v1.0.18 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultExpiration 默认过期时间.
	DefaultExpiration time.Duration = 0
	// NoExpiration 不过期.
	NoExpiration time.Duration = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache 缓存.

func New

func New[K comparable, V any](defaultExpiration, interval time.Duration) *Cache[K, V]

New 新建缓存, 设置默认过期时间和过期检查周期.

Example

ExampleNew is an example function.

package main

import (
	"fmt"
	"time"

	"github.com/xuender/kit/cache"
)

func main() {
	cac := cache.New[int, int](time.Millisecond, time.Millisecond)
	cac.SetDuration(1, 1, time.Millisecond*3)
	cac.Set(2, 1)

	fmt.Println(cac.GetNoExtension(1))
	fmt.Println(cac.GetNoExtension(3))
	fmt.Println(cac.Get(3))
	fmt.Println(cac.Len())

	time.Sleep(time.Millisecond * 2)
	fmt.Println(cac.Len())
	cac.Delete(1)
	fmt.Println(cac.Len())

}
Output:

1 true
0 false
0 false
2
1
0

func NewStringKey

func NewStringKey[V any](defaultExpiration, interval time.Duration) *Cache[string, V]

NewStringKey 新建字符串键值的缓存.

Example

ExampleNewStringKey is an example function.

package main

import (
	"fmt"
	"time"

	"github.com/xuender/kit/cache"
)

func main() {
	cac := cache.NewStringKey[int](time.Millisecond, time.Millisecond)
	cac.SetDuration("key1", 1, time.Millisecond*3)
	cac.Set("key2", 1)

	fmt.Println(cac.Get("key2"))
	fmt.Println(cac.Len())

	time.Sleep(time.Millisecond * 2)
	fmt.Println(cac.Len())

}
Output:

1 true
2
1

func (*Cache[K, V]) Delete

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

func (Cache) DeleteExpired

func (p Cache) DeleteExpired()

func (*Cache[K, V]) Get

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

Get 获取元素并刷新.

func (*Cache[K, V]) GetNoExtension added in v1.0.18

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

GetNoExtension 读元素不延期.

func (*Cache[K, V]) Iterate

func (p *Cache[K, V]) Iterate(yield func(K, V) error) error

Iterate 迭代.

func (*Cache[K, V]) Len

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

Len 元素数量.

func (*Cache[K, V]) Set

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

Set 设置元素.

func (*Cache[K, V]) SetDuration

func (p *Cache[K, V]) SetDuration(key K, value V, expiration time.Duration)

SetDuration 设置元素及过期时间.

Jump to

Keyboard shortcuts

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