Because ristretto lets us build a cache with different types as the value…If we use a struct as a value, how do we access struct and the struct’s fields? Is this possible?
Let’s say I have struct:
type bucket struct {
tokens uint
time time.Time
}
And my ristretto cache:
buckets, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
panic(err)
}
Wich sets a with key:value of [string]struct{}:
ip := "10.108.1.1"
buckets.Set(ip, bucket{tokens: max -1 , time: time.Now()}, 1)
buckets.Wait()
Question one: How can i retrieve only the tokens
value? Trying to use Get
will only return what i think is some numbers and memory addresses:
test, ok := buckets.Get(ip)
if !ok {
panic("missing value")
}
fmt.Println(test)
Question 2: How can i update only the field tokens
of the cache’s bucket
struct?
buckets.Set(ip, bucket{tokens: 3}, 1)
test, ok = buckets.Get(ip)
if !ok {
panic("missing value")
}
This overwrites the entire key.