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

item == nil, is it true for expired cache.Get("user:4") too? #23

Closed
unisqu opened this issue Nov 30, 2018 · 4 comments
Closed

item == nil, is it true for expired cache.Get("user:4") too? #23

unisqu opened this issue Nov 30, 2018 · 4 comments

Comments

@unisqu
Copy link

unisqu commented Nov 30, 2018

item == nil, is it true for expired cache.Get("user:4") too?

  item := cache.Get("user:4")
  if item == nil {
    //handle
  } else {
    user := item.Value().(*User)
  }

i would like to check if item is empty. how do i check that? will expired item be emptied?

@karlseguin
Copy link
Owner

There's 3 cases:

if item == nil {
   ...
} else if item.Expired() {
  .. // can use item.TTL() to see how stale it is
} else {
   .. / item not expired
}```

The `item.Expired() == true` case happens when the item has expired, but it hasn't been removed from the cache (because the size of the cache is still smaller than the configured max). The benefit of this approach is that it gives you more flexibility by giving you the option to use stale data if you want.

@unisqu
Copy link
Author

unisqu commented Dec 27, 2018

thx

@unisqu unisqu closed this as completed Dec 27, 2018
@unisqu unisqu reopened this Dec 27, 2018
@unisqu
Copy link
Author

unisqu commented Dec 27, 2018

if item == nil { ... } else if item.Expired() { .. // can use item.TTL() to see how stale it is } else { .. / item not expired }```

my problem with this statement is... "see how stale it is?"
let's say in the next nanosecond, the cache is exceeded and item is evicted/flushed,
let's say i dont do TTL() but would like to serve that stale item (but the last nanosecond is evicted/flush), won't that be "dangerous" as my item will be nil.

@karlseguin
Copy link
Owner

No.

It might get evicted from the cache, but item still holds a reference to value so it won't be garbage collected until item falls out of scope.

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

No branches or pull requests

2 participants