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

Implement LRU cache with TTL

Open yuva29 opened this issue 7 years ago • 6 comments

This implements LRU with TTL:

  1. Each item is added with lastAccessedTime which is used to remove the items once it meets TTL.

  2. This ensures eventual consistency. Some items may continue to exists until the cleanup routine or GET call cleans it up. Please note that other calls Contains, Peek, Len, etc. will continue to include the expired item in their result unless otherwise it is removed by either GET or cleanup routine.

  3. Cleanup on the GET ensures that the user never access the expired item.

Follow up:

  • Move the interface from simple LRU to the top level and make sure lru.go and lru_ttl.go implements the interface. Now, that TTL is at the outer level, interface is not required in simplelru.
  • Use the testutils in lru_test.go

Signed-off-by: Yuva Shankar [email protected]

yuva29 avatar Feb 13 '18 22:02 yuva29

@armon Please take a look and let me know your comments.

yuva29 avatar Feb 13 '18 22:02 yuva29

ping

ppwfx avatar Nov 07 '18 12:11 ppwfx

ping

ppwfx avatar Jan 19 '19 09:01 ppwfx

Why is lastAccessTime updated only on Add? I assume what you mean that value to do is instead slide the TTL window for expiry, but the variable name makes it sound like it should be updated by Get.

I think that brings up an open question on the fundamental design: is the right behavior that a value should be expired from the cache after it hasn't been accessed for a period of time, or hasn't been updated for a period of time? I think depending on the use-case you could easily want one or the other.

Another issue I see is that the underlying simplelru still has a size cap, so now you have a cache that actually may expire/remove things prior to the given time because there are too many entries. That seems hard to rely on.

jefferai avatar May 20 '19 14:05 jefferai

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Dec 06 '19 12:12 hashicorp-cla

Closing, as #116 has achieved this.

irenarindos avatar Mar 26 '24 12:03 irenarindos