golang-lru
golang-lru copied to clipboard
Implement LRU cache with TTL
This implements LRU with TTL:
-
Each item is added with
lastAccessedTime
which is used to remove the items once it meets TTL. -
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. -
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]
@armon Please take a look and let me know your comments.
ping
ping
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.
Closing, as #116 has achieved this.