jellydator / ttlcache

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

Proposal: Present cache items in order of oldest to newest #125

Open k1LoW opened 4 months ago

k1LoW commented 4 months ago

Hello everyone.

Thank you for the great cache package.

Feature request

In some cases, we want to delete the oldest cache first, when an external limit is reached, which is different from the capacity of the ttlcache.

Usecase

Specifically, we are creating a disk cache package to be used as a proxy cache.

https://github.com/2manymws/rcutil

It monitors the file size of the entire cache file while storing the cache file key in ttlcache.

When the file size limit is reached, we want to delete the cache in order of oldest to newest from the information held by ttlcache.

In https://github.com/2manymws/rcutil/pull/48, we modified it to have its own LRU, but we realized that if we could refer to the Cache.items.lru of ttlcache, we would not need to make such a complicated modification.

Proposal

We thought of two implementations.

If you choose, we can present the implementation via Pull Request.

  1. Provide a function that returns Items in reverse order to Cache.Range
    • Cache.RangeInReverse or Cache.Reverse ?
  2. Extend Cache.Range to allow passing the option to reverse the order.
    • func (c *Cache[K, V]) Range(fn func(item *Item[K, V], opts ...RangeOption) bool)

Thank you!