Skip to content
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

fix:more accurate comment;more efficient slice #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cacheitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func NewCacheItem(key interface{}, lifeSpan time.Duration, data interface{}) *Ca
t := time.Now()
return &CacheItem{
key: key,
data: data,
lifeSpan: lifeSpan,
createdOn: t,
accessedOn: t,
accessCount: 0,
aboutToExpire: nil,
data: data,
}
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (item *CacheItem) AddAboutToExpireCallback(f func(interface{})) {
item.aboutToExpire = append(item.aboutToExpire, f)
}

// RemoveAboutToExpireCallback empties the about to expire callback queue
// RemoveAboutToExpireCallback empties the AboutToExpire callback queue
func (item *CacheItem) RemoveAboutToExpireCallback() {
item.Lock()
defer item.Unlock()
Expand Down
29 changes: 16 additions & 13 deletions cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (table *CacheTable) Count() int {

// Foreach all items
func (table *CacheTable) Foreach(trans func(key interface{}, item *CacheItem)) {
table.RLock()
defer table.RUnlock()
table.Lock() // trans may modify the *item
defer table.Unlock()

for k, v := range table.items {
trans(k, v)
Expand All @@ -76,7 +76,7 @@ func (table *CacheTable) SetAddedItemCallback(f func(*CacheItem)) {
table.addedItem = append(table.addedItem, f)
}

//AddAddedItemCallback appends a new callback to the addedItem queue
// AddAddedItemCallback appends a new callback to the addedItem queue
func (table *CacheTable) AddAddedItemCallback(f func(*CacheItem)) {
table.Lock()
defer table.Unlock()
Expand Down Expand Up @@ -108,7 +108,7 @@ func (table *CacheTable) AddAboutToDeleteItemCallback(f func(*CacheItem)) {
table.aboutToDeleteItem = append(table.aboutToDeleteItem, f)
}

// RemoveAboutToDeleteItemCallback empties the about to delete item callback queue
// RemoveAboutToDeleteItemCallback empties the AboutToDeleteItem callback queue
func (table *CacheTable) RemoveAboutToDeleteItemCallback() {
table.Lock()
defer table.Unlock()
Expand All @@ -135,7 +135,7 @@ func (table *CacheTable) expirationCheck() {
}

// To be more accurate with timers, we would need to update 'now' on every
// loop iteration. Not sure it's really efficient though.
// loop iteration. Not sure if it's really efficient though.
now := time.Now()
smallestDuration := 0 * time.Second
for key, item := range table.items {
Expand All @@ -149,7 +149,7 @@ func (table *CacheTable) expirationCheck() {
continue
}
if now.Sub(accessedOn) >= lifeSpan {
// Item has excessed its lifespan.
// Item has exhausted its lifespan.
table.deleteInternal(key)
} else {
// Find the item chronologically closest to its end-of-lifespan.
Expand Down Expand Up @@ -344,18 +344,21 @@ func (table *CacheTable) MostAccessed(count int64) []*CacheItem {
}
sort.Sort(p)

var r []*CacheItem
c := int64(0)
for _, v := range p {
if c >= count {
break
}
// count may greater than the table item length
if count > int64(len(table.items)) {
count = int64(len(table.items))
}

var r = make([]*CacheItem, 0, int(count))
for _, v := range p {
item, ok := table.items[v.Key]
if ok {
r = append(r, item)
}
c++

if len(r) >= int(count) {
break
}
}

return r
Expand Down