golang-lru icon indicating copy to clipboard operation
golang-lru copied to clipboard

Added AddOrUpdate method to lru

Open gkoutsou opened this issue 3 years ago • 1 comments

It behaves similar to the standard Add method with the difference that it can also take an update method that will be performed in case the key already exists.

For example:

value1 := []string{"some_value"}
value2 := []string{"some_value_2"}

l.Add(1, value1)
l.AddOrUpdate(1, value2, func(v1, v2 interface{}) interface{} {
	slice1 := v1.([]string)
	slice2 := v2.([]string)
	return append(slice1, slice2...)
})

Then l.Get(1) will result to []string{"some_value", "some_value_2"}

gkoutsou avatar May 19 '22 16:05 gkoutsou

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar May 19 '22 16:05 hashicorp-cla