Skip to content

Commit

Permalink
makeItem方法私有化
Browse files Browse the repository at this point in the history
  • Loading branch information
xuning888 committed Feb 20, 2024
1 parent 53a8120 commit 01881f7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions datastruct/ttl/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *SimpleCache) Expire(key string, expireTime time.Time) {
s.heap.update(item, key, expireTime)
return
}
item = MakeItem(key, expireTime)
item = makeItem(key, expireTime)
s.ttlMap[key] = item
heap.Push(&s.heap, item)
}
Expand All @@ -66,7 +66,7 @@ func (s *SimpleCache) IsExpired(key string) (expired bool, exists bool) {
func (s *SimpleCache) Remove(key string) {
item, ok := s.ttlMap[key]
if ok {
heap.Remove(&s.heap, item.Index)
heap.Remove(&s.heap, item.index)
delete(s.ttlMap, key)
}
}
Expand Down
14 changes: 7 additions & 7 deletions datastruct/ttl/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type Item struct {
Key string
ExpireTime time.Time
ExpireTimestamp int64
Index int
index int
}

func MakeItem(key string, expiryTime time.Time) *Item {
func makeItem(key string, expiryTime time.Time) *Item {
return &Item{
Key: key,
ExpireTime: expiryTime,
Expand All @@ -40,14 +40,14 @@ func (t ttlHeap) Less(i, j int) bool {

func (t ttlHeap) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
t[i].Index = i
t[j].Index = j
t[i].index = i
t[j].index = j
}

func (t *ttlHeap) Push(x interface{}) {
n := len(*t)
ele := x.(*Item)
ele.Index = n
ele.index = n
*t = append(*t, ele)
}

Expand All @@ -56,7 +56,7 @@ func (t *ttlHeap) Pop() interface{} {
n := len(old)
ele := old[n-1]
old[n-1] = nil // avoid memory leak
ele.Index = -1 // for safety
ele.index = -1 // for safety
*t = old[0 : n-1]
return ele
}
Expand All @@ -76,5 +76,5 @@ func (t *ttlHeap) update(item *Item, key string, expiryTime time.Time) {
item.Key = key
item.ExpireTime = expiryTime
item.ExpireTimestamp = expiryTime.UnixMilli()
heap.Fix(t, item.Index)
heap.Fix(t, item.index)
}
6 changes: 3 additions & 3 deletions datastruct/ttl/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestRandomOps(t *testing.T) {
pq := make(ttlHeap, len(items))
i := 0
for key, expiryTime := range items {
item := MakeItem(key, expiryTime)
item.Index = i
item := makeItem(key, expiryTime)
item.index = i
pq[i] = item
i++
}
heap.Init(&pq)

ele := MakeItem("2", now.Add(2*time.Second))
ele := makeItem("2", now.Add(2*time.Second))
heap.Push(&pq, ele)
pq.update(ele, ele.Key, time.Now().Add(1*time.Second))

Expand Down
4 changes: 4 additions & 0 deletions persistence/aof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package persistence
// Aof persistence
type Aof struct {
}

func NewAof() *Aof {
return &Aof{}
}

0 comments on commit 01881f7

Please sign in to comment.