mailgun / groupcache

Clone of golang/groupcache with TTL and Item Removal support
Apache License 2.0
484 stars 72 forks source link

Can you update key? #57

Open kingcw opened 1 year ago

kingcw commented 1 year ago

Can you update a key's value directly? Or have to delete the key first, then set "key: new value"?

sabouaram commented 1 year ago

// Check if the key exists in the cache var value []byte err = h.Group.Get(nil, k, groupcache.AllocatingByteSliceSink(&value)) if err == nil { // Key exists in the cache, compare with the new value if !reflect.DeepEqual(value, v) { // Values are different, update the cache jsonValue, err := json.Marshal(v) if err != nil { return err } expireTime := time.Now().Add(time.Duration(h.cacheTTL) * time.Minute) err = h.Group.Set(context.Background(), k, jsonValue, expireTime, false) if err != nil { return err } } }