Skip to content

Commit

Permalink
refactor(ramcache): Rename internal functions and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer committed Jun 16, 2024
1 parent a011f0f commit b6e3938
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ramcache/ramcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ func (r *ramcache) cleanupExpiredItems() {

// removeExpiredItems removes expired items from the store.
func (r *ramcache) removeExpiredItems() {
items := r.store.GetItemsSortedByExpiry()
for _, item := range items {
if item.Item.IsExpired() {
r.store.Delete(item.Key)
keyItems := r.store.KeyItemsSortedByExpiry()
for _, ki := range keyItems {
if ki.Item.IsExpired() {
r.store.Delete(ki.Key)
} else {
// Items are sorted by expiry time, so we can break early
break
Expand Down
4 changes: 2 additions & 2 deletions ramcache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type keyItem struct {
Item Item // Item is the item.
}

// GetItemsSortedByExpiry returns all items sorted by expiry time (closest to expiry first).
func (s *store) GetItemsSortedByExpiry() []keyItem {
// KeyItemsSortedByExpiry returns all key items sorted by expiry time (closest to expiry first).
func (s *store) KeyItemsSortedByExpiry() []keyItem {
s.mu.RLock()
defer s.mu.RUnlock()
items := make([]keyItem, 0, len(s.items))
Expand Down
6 changes: 3 additions & 3 deletions ramcache/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func TestStore(t *testing.T) {
}
})

t.Run("GetItemsSortedByExpiry", func(t *testing.T) {
t.Run("KeyItemsSortedByExpiry", func(t *testing.T) {
s := newStore()
s.Set("key1", Item{Value: []byte("value1"), Expiry: time.Now().Add(20 * time.Minute)})
s.Set("key2", Item{Value: []byte("value2"), Expiry: time.Now().Add(10 * time.Minute)})
items := s.GetItemsSortedByExpiry()
items := s.KeyItemsSortedByExpiry()
if len(items) != 2 || items[0].Key != "key2" || items[1].Key != "key1" {
t.Errorf("GetItemsSortedByExpiry failed. Expected [key2, key1], got [%v, %v]", items[0].Key, items[1].Key)
t.Errorf("KeyItemsSortedByExpiry failed. Expected [key2, key1], got [%v, %v]", items[0].Key, items[1].Key)
}
})
}

0 comments on commit b6e3938

Please sign in to comment.