Documentation
¶
Index ¶
- Constants
- type Cache
- func (p *Cache[K, V]) Delete(key K)
- func (p Cache) DeleteExpired()
- func (p *Cache[K, V]) Get(key K) (V, bool)
- func (p *Cache[K, V]) Iterate(yield func(K, V) error) error
- func (p *Cache[K, V]) Len() int
- func (p *Cache[K, V]) Set(key K, value V)
- func (p *Cache[K, V]) SetDuration(key K, value V, expiration time.Duration)
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.Get(1))
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 2 1 0
func NewStringKey ¶
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("key1"))
fmt.Println(cac.Len())
time.Sleep(time.Millisecond * 2)
fmt.Println(cac.Len())
}
Output: 1 true 2 1
func (Cache) DeleteExpired ¶
func (p Cache) DeleteExpired()
func (*Cache[K, V]) SetDuration ¶
SetDuration 设置元素及过期时间.
Click to show internal directories.
Click to hide internal directories.