-
Notifications
You must be signed in to change notification settings - Fork 46
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
Request Get Expiration With Get #56
Comments
Hi, thanks for the issue. I was going to rework the API a bit later. For now you can use something like this. package main
import (
"time"
"github.com/maypok86/otter"
)
type WithTime[V any] struct {
Value V
UpdatedAt time.Time // or in seconds
}
type Cache[K comparable, V any] struct {
cache otter.Cache[K, WithTime[V]]
}
func NewCache[K comparable, V any](capacity int) (*Cache[K, V], error) {
b, err := otter.NewBuilder[K, WithTime[V]](capacity)
if err != nil {
return nil, err
}
c, err := b.WithTTL(time.Hour).Build()
if err != nil {
return nil, err
}
return &Cache[K, V]{
cache: c,
}, nil
}
func (c *Cache[K, V]) Get(key K) (WithTime[V], bool) {
return c.cache.Get(key)
}
func (c *Cache[K, V]) Set(key K, value V) bool {
updatedAt := time.Now()
return c.cache.Set(key, WithTime[V]{
Value: value,
UpdatedAt: updatedAt,
})
} |
What do you think of this solution to this problem? It's a bit non-standard for Go, but allows otter to hide a lot of future features. c, err := MustBuilder[int, int](256).
WithTTL(time.Hour).
Build()
...
// if you want to get the value without updating the eviction policy
value, ok := c.Advanced().GetQuietly(1)
...
// if you want to get an entry
entry, ok := c.Advanced().GetEntry(1)
...
// if you want to get an entry without updating the eviction policy
entry, ok := c.Advanced().GetEntryQuietly(1)
...
key := entry.Key()
value := entry.Value()
cost := entry.Cost()
expiration := entry.Expiration()
ttl := entry.TTL()
hasExpired := entry.HasExpired() |
@maypok86 your proposed API can hide underneath a lot of features. what do you expect to user if user get
And I think |
I assumed something like this key := 1
...
func (c *Cache[K, V]) GetCost(key K) (uint32, error) {
entry, ok := c.Advanced().GetEntryQuietly(key)
if !ok {
return 0, errors.New("a cache entry was not found")
}
return entry.Cost(), nil
}
In all cases, 1 is just a key.
This is a difficult moment. It is unclear which name is better to give. I was also thinking about something like |
how about
I mean what is use case if user can get |
Hmm, it looks interesting. But probably just |
@proost So, I think we've come to an agreement, right? |
@maypok86 I apologize for not being able to answer. Speaking for myself, this is a good solution. But I didn't understand the meaning of updating eviction policy. |
@MrSametBurgazoglu During a |
@maypok86 I agree. Thank you for a new feature! |
Clarification and motivation
I have a script that updating some variables and users get them from cache. But I want to get expiration for showing them last update time.
Acceptance criteria
Get expiration time with get function or new function with expiration time
The text was updated successfully, but these errors were encountered: