jellydator / ttlcache

An in-memory cache with item expiration and generics
MIT License
883 stars 115 forks source link

make GetOrSet and GetAndDelete atomic #107

Closed DoubleDi closed 10 months ago

DoubleDi commented 1 year ago

as it is stated in #97 GetOrSet and GetAndDelete are not atomic.

This PR tries to resolve this problem

I am not sure about the reasons (maybe performance), that the lock was initially made in getWithOpts. Also a good followup question is - do we need the metricsMu at all if this PR will be merged?

DoubleDi commented 11 months ago

@swithek @davseby Hi! Please check the latest version.

Please note, that the GetOrSet func has changed. The current version, when the loader is defined returns a loaded item from the loader, and only if the loaded item is nil will set the new value. The new version, when the loader is defined does not search the loader at all if the item is not present in the cache and directly sets the new value

I assume the new logic is better, because when you call GetOrSet you already have a preloaded value, that you pass and you do not want to preload it again internally in the cache

DoubleDi commented 11 months ago

@davseby @swithek Please check when you have the time

swithek commented 11 months ago

@DoubleDi will do, later today/tomorrow.

DoubleDi commented 11 months ago

@swithek when you have time please check

swithek commented 10 months ago

Thanks for your contribution @DoubleDi. Merging :)