mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
cache
This commit is contained in:
52
cache/tiny_lfu.go
vendored
Normal file
52
cache/tiny_lfu.go
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/charlienet/go-mixed/locker"
|
||||
"github.com/vmihailenco/go-tinylfu"
|
||||
)
|
||||
|
||||
type TinyLFU struct {
|
||||
mu locker.Locker
|
||||
lfu *tinylfu.T
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewTinyLFU(size int, ttl time.Duration) *TinyLFU {
|
||||
return &TinyLFU{
|
||||
mu: locker.NewLocker(),
|
||||
lfu: tinylfu.New(size, 100000),
|
||||
ttl: ttl,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TinyLFU) Set(key string, b []byte, expire time.Duration) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.lfu.Set(&tinylfu.Item{
|
||||
Key: key,
|
||||
Value: b,
|
||||
ExpireAt: time.Now().Add(c.ttl),
|
||||
})
|
||||
}
|
||||
|
||||
func (c *TinyLFU) Get(key string) ([]byte, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
val, ok := c.lfu.Get(key)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return val.([]byte), true
|
||||
}
|
||||
|
||||
func (c *TinyLFU) Del(key string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.lfu.Del(key)
|
||||
}
|
Reference in New Issue
Block a user