feat: cache add single-flight and timing-wheel.

This commit is contained in:
Gordon 2024-01-15 10:54:15 +08:00
parent 1f1ab65375
commit 166d51ab23

View File

@ -16,7 +16,7 @@ type Cache[V any] interface {
Del(ctx context.Context, key ...string) Del(ctx context.Context, key ...string)
} }
func New[V any](opts ...Option) Cache[V] { func NewCache[V any](expire time.Duration, opts ...Option) Cache[V] {
opt := defaultOption() opt := defaultOption()
for _, o := range opts { for _, o := range opts {
o(opt) o(opt)
@ -27,6 +27,7 @@ func New[V any](opts ...Option) Cache[V] {
n: uint64(opt.localSlotNum), n: uint64(opt.localSlotNum),
} }
c.timingWheel = NewTimeWheel[string, V](TimingWheelSize, time.Second, c.exec) c.timingWheel = NewTimeWheel[string, V](TimingWheelSize, time.Second, c.exec)
go c.timingWheel.Start()
for i := 0; i < opt.localSlotNum; i++ { for i := 0; i < opt.localSlotNum; i++ {
c.slots[i] = NewLRU[string, V](opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict) c.slots[i] = NewLRU[string, V](opt.localSlotSize, opt.localSuccessTTL, opt.localFailedTTL, opt.target, c.onEvict)
} }
@ -39,6 +40,7 @@ func New[V any](opts ...Option) Cache[V] {
type cache[V any] struct { type cache[V any] struct {
n uint64 n uint64
slots []*LRU[string, V] slots []*LRU[string, V]
expire time.Duration
opt *option opt *option
link link.Link link link.Link
timingWheel *TimeWheel[string, V] timingWheel *TimeWheel[string, V]
@ -97,6 +99,10 @@ func (c *cache[V]) Del(ctx context.Context, key ...string) {
c.del(key...) c.del(key...)
} }
} }
func (c *cache[V]) exec(key string, value V) {
func (c *cache[V]) set(key string, value V) {
} }
func (c *cache[V]) exec(key string, _ V) {
c.del(key)
}