8000 Rename `RemoveStale` to `RemoveExpired` by erni27 · Pull Request #29 · erni27/imcache · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rename RemoveStale to RemoveExpired #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (c *Cache[K, V]) RemoveAll() {
c.removeAll(time.Now())
}

func (c *Cache[K, V]) removeStale(now time.Time) {
func (c *Cache[K, V]) removeExpired(now time.Time) {
c.mu.Lock()
// To avoid copying the expired entries if there's no eviction callback.
if c. nil {
Expand All @@ -383,11 +383,11 @@ func (c *Cache[K, V]) removeStale(now time.Time) {
}
}

// RemoveStale removes all expired entries.
// RemoveExpired removes all expired entries.
//
// If an eviction callback is set, it is called for each removed entry.
func (c *Cache[K, V]) RemoveStale() {
c.removeStale(time.Now())
func (c *Cache[K, V]) RemoveExpired() {
c.removeExpired(time.Now())
}

type kv[K comparable, V any] struct {
Expand Down Expand Up @@ -622,13 +622,13 @@ func (s *Sharded[K, V]) RemoveAll() {
}
}

// RemoveStale removes all expired entries.
// RemoveExpired removes all expired entries.
//
// If an eviction callback is set, it is called for each removed entry.
func (s *Sharded[K, V]) RemoveStale() {
func (s *Sharded[K, V]) RemoveExpired() {
now := time.Now()
for _, shard := range s.shards {
shard.removeStale(now)
shard.removeExpired(now)
}
}

Expand Down
14 changes: 7 additions & 7 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type imcache[K comparable, V any] interface {
ReplaceWithFunc(key K, f func(old V) (new V), exp Expiration) (present bool)
Remove(key K) (present bool)
RemoveAll()
RemoveStale()
RemoveExpired()
GetAll() map[K]V
Len() int
StartCleaner(interval time.Duration) error
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestImcache_RemoveAll(t *testing.T) {
}
}

func TestImcache_RemoveStale(t *testing.T) {
func TestImcache_RemoveExpired(t *testing.T) {
tests := []struct {
name string
c imcache[string, string]
Expand All @@ -677,7 +677,7 @@ func TestImcache_RemoveStale(t *testing.T) {
c.Set("foo", "foo", WithNoExpiration())
c.Set("bar", "bar", WithExpiration(time.Nanosecond))
<-time.After(time.Nanosecond)
c.RemoveStale()
c.RemoveExpired()
if _, ok := c.Get("foo"); !ok {
t.Errorf("Cache.Get(%s) = _, %t, want _, true", "foo", ok)
}
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func TestImcache_RemoveAll_EvictionCallback(t *testing.T) {
}
}

func TestImcache_RemoveStale_EvictionCallback(t *testing.T) {
func TestImcache_RemoveExpired_EvictionCallback(t *testing.T) {
evictioncMock := &evictionCallbackMock{}

tests := []struct {
Expand All @@ -1196,7 +1196,7 @@ func TestImcache_RemoveStale_EvictionCallback(t *testing.T) {
c.Set("foo", "foo", WithExpiration(time.Nanosecond))
c.Set("bar", "bar", WithNoExpiration())
<-time.After(time.Nanosecond)
c.RemoveStale()
c.RemoveExpired()
if !evictioncMock.HasBeenCalledWith("foo", "foo", EvictionReasonExpired) {
t.Errorf("want EvictionCallback called with EvictionCallback(%s, %s, %d)", "foo", "foo", EvictionReasonExpired)
}
Expand Down Expand Up @@ -1517,8 +1517,8 @@ func TestCache_MaxEntries(t *testing.T) {
t.Errorf("want EvictionCallback called with EvictionCallback(%s, %d, %d)", "fifteen", 15, EvictionReasonMaxEntriesExceeded)
}

// RemoveStale should not mess with the LRU queue.
c.RemoveStale()
// RemoveExpired should not mess with the LRU queue.
c.RemoveExpired()
// LRU queue: fourteen -> eighteen -> seventeen.
c.Set("twenty", 20, WithNoExpiration())
// LRU queue: twenty -> fourteen -> eighteen -> seventeen.
Expand Down
10 changes: 5 additions & 5 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"time"
)

// remover is an interface that wraps the RemoveStale method.
type remover interface {
RemoveStale()
// eremover is an interface that wraps the RemoveExpired method.
type eremover interface {
RemoveExpired()
}

func newCleaner() *cleaner {
Expand All @@ -25,7 +25,7 @@ type cleaner struct {
doneCh chan struct{}
}

func (c *cleaner) start(r remover, interval time.Duration) error {
func (c *cleaner) start(r eremover, interval time.Duration) error {
if interval <= 0 {
return errors.New("imcache: interval must be greater than 0")
}
Expand All @@ -44,7 +44,7 @@ func (c *cleaner) start(r remover, interval time.Duration) error {
for {
select {
case <-ticker.C:
r.RemoveStale()
r.RemoveExpired()
case <-c.stopCh:
close(c.doneCh)
return
Expand Down
0