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

Delete from the cache on Get if the item expired (to trigger onEvicted) #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fdurand
Copy link

@fdurand fdurand commented Jan 3, 2018

When we Get from the cache and if the item expired then Delete it to be sure that onEvicted is triggered.

@@ -128,6 +128,7 @@ func (c *cache) Get(k string) (interface{}, bool) {
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
c.mu.RUnlock()
c.Delete(k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding this change also in the GetWithExpiration https://github.com/patrickmn/go-cache/pull/75/files#diff-56ffa6659747ba7a23d2b5597a4cda5dR143 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll only want to call Delete if the cache has an eviction function.

@aapolkovsky
Copy link

This will strongly affect on Get operation performance, because mutex will be locked for RW-access (another concurrent read operations will be also locked). Nevertheless, janitor will also lock cache while deleting other expired items.

@ekinanp
Copy link

ekinanp commented Mar 9, 2019

Any updates on this PR?

Repository owner deleted a comment from pablodz Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants