Skip to content

Commit

Permalink
Remove unused "forgot" field in entry struct
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Feb 13, 2024
1 parent 64fc1f2 commit e2da7a9
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions memoize.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type entry[V any] struct {
mu sync.RWMutex
val V
expiresAt time.Time
forgot bool
call *call[V]
}

Expand Down Expand Up @@ -111,12 +110,11 @@ func (g *Group[K, V]) Do(ctx context.Context, key K, fn func(ctx context.Context
// the cache is expired or unavailable.
e.mu.Lock()
c := e.call
if c == nil || e.forgot {
if c == nil {
// it is the first call.
c = new(call[V])
c.ctx, c.cancel = context.WithCancel(withoutCancel(ctx))
e.call = c
e.forgot = false
go do(g, e, c, key, fn)
}
ch := make(chan Result[V], 1)
Expand All @@ -141,7 +139,6 @@ func (g *Group[K, V]) Do(ctx context.Context, key K, fn func(ctx context.Context
// to avoid adding new channels to c.chans
if e.call == c {
e.call = nil
e.forgot = false
}
}
e.mu.Unlock()
Expand Down Expand Up @@ -187,12 +184,11 @@ func (g *Group[K, V]) DoChan(ctx context.Context, key K, fn func(ctx context.Con
// the cache is expired or unavailable.
e.mu.Lock()
c := e.call
if c == nil || e.forgot {
if c == nil {
// it is the first call.
c = new(call[V])
c.ctx, c.cancel = context.WithCancel(withoutCancel(ctx))
e.call = c
e.forgot = false
go do(g, e, c, key, fn)
}
ch := make(chan Result[V], 1)
Expand All @@ -219,13 +215,12 @@ func do[K comparable, V any](g *Group[K, V], e *entry[V], c *call[V], key K, fn
e.mu.Lock()
if e.call == c {
// save to the cache
if !e.forgot && ret.Err == nil {
if ret.Err == nil {
e.val = ret.Val
e.expiresAt = ret.ExpiresAt
}
// to avoid adding new channels to c.chans
e.call = nil
e.forgot = false
}
chans := c.chans
e.mu.Unlock()
Expand Down Expand Up @@ -267,17 +262,10 @@ func do[K comparable, V any](g *Group[K, V], e *entry[V], c *call[V], key K, fn
// to Do for this key will call the function rather than waiting for
// an earlier call to complete.
func (g *Group[K, V]) Forget(key K) {
g.mu.RLock()
defer g.mu.RUnlock()

e, ok := g.m[key]
if !ok {
return
}
e.mu.Lock()
defer e.mu.Unlock()
g.mu.Lock()
defer g.mu.Unlock()

e.forgot = true
delete(g.m, key)
}

// GC deletes the expired items from the cache.
Expand Down

0 comments on commit e2da7a9

Please sign in to comment.