You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine you have a cache containing items that are themselves maps or objects that are passed by reference.
If you want to modify one of the entries/elements in the cached object, you need to Get the item, copy it, and then Set to avoid modifying the shared reference returned by Get.
Adding a way to read/modify an entry within the same lock would help with this use-case:
cache.Map("key", func(result (interface{}, bool)) interface{} {
// do stuff with item and return it
}, cache.DefaultExpiration);
Implementation example (I apologize if the syntax is wrong):
func (c*cache) Map(kstring, mapFnfunc(item (interface{}, bool)) interface{} , d time.Duration) {
// "Inlining" of setvareint64ifd==DefaultExpiration {
d=c.defaultExpiration
}
ifd>0 {
e=time.Now().Add(d).UnixNano()
}
c.mu.Lock()
c.items[k] =Item{
Object: mapFn(c.get(k)),
Expiration: e,
}
// TODO: Calls to mu.Unlock are currently not deferred because defer// adds ~200 ns (as of go1.)c.mu.Unlock()
}
So now we have an easy way to Read and Replace a complex object using only one lock and not risking accidental mutation of a shared reference.
Alternatively, exporting Lock/Unlock functions for consumers of this library would make it easier to write functions that perform bulk modifications to cache without as much locking.
The text was updated successfully, but these errors were encountered:
Imagine you have a cache containing items that are themselves maps or objects that are passed by reference.
If you want to modify one of the entries/elements in the cached object, you need to
Get
the item, copy it, and thenSet
to avoid modifying the shared reference returned byGet
.Adding a way to read/modify an entry within the same lock would help with this use-case:
Implementation example (I apologize if the syntax is wrong):
So now we have an easy way to Read and Replace a complex object using only one lock and not risking accidental mutation of a shared reference.
Alternatively, exporting
Lock/Unlock
functions for consumers of this library would make it easier to write functions that perform bulk modifications to cache without as much locking.The text was updated successfully, but these errors were encountered: