ttlcache icon indicating copy to clipboard operation
ttlcache copied to clipboard

Enhancement Req - Add option to the cache to prevent updates when using Set()

Open patpicos opened this issue 2 years ago • 3 comments

Use case: I use the cache to capture data i receive over UDP. In some cases, i have competing producers that will send the same information for a Key. I want the first message to win and be the one persisted in the cache and not have further messages overwrite.

Looking at the cache.set() function, it searches for an existing element and overwrites it. https://github.com/jellydator/ttlcache/blob/master/cache.go#L134-L142

I would like a new option for the cache set on creation to prevent overwrites

cache := ttlcache.New[string, string](
		ttlcache.WithTTL[string, string](30 * time.Minute),
                ttlcache.WithoutOverwrite(),
	)

//Pseudo-code to show the expectation
cache.Set("first", "value1", ttlcache.DefaultTTL)
cache.Set("first", "value2", ttlcache.DefaultTTL)
cache.Set("first", "value3", ttlcache.DefaultTTL)

v:= cache.Get("first").Value()
assert.Equal(t, "value1",v)

patpicos avatar Jul 20 '22 12:07 patpicos

@swithek I PR'd against master. I assume master is tracking to v3 (that's the version I'm using in my project)

patpicos avatar Jul 22 '22 22:07 patpicos

@ReneKroon is this a PR you could review?

patpicos avatar Jul 27 '22 01:07 patpicos

Currently on holiday, will check back in in August

Op wo 27 jul. 2022 03:07 schreef Patrick Picard @.***>:

@ReneKroon https://github.com/ReneKroon is this a PR you could review?

— Reply to this email directly, view it on GitHub https://github.com/jellydator/ttlcache/issues/78#issuecomment-1196157554, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAW5D6GXVI7ULE6L5LACJ3LVWCDWFANCNFSM54DPAADA . You are receiving this because you were mentioned.Message ID: @.***>

ReneKroon avatar Jul 27 '22 11:07 ReneKroon

closed PR and closing issue. Code remains on fork.

patpicos avatar Aug 22 '22 14:08 patpicos