amphp / cache

A fiber-aware cache API based on Amp and Revolt.
MIT License
98 stars 15 forks source link

Add maxSize option to ArrayCache #4

Closed kelunik closed 6 years ago

kelunik commented 7 years ago

We should add $maxSize to ArrayCache or provide a second implementation of ArrayCache that allows for such an option to limit the size of the cache. amphp/dns is one component that potentially benefits from such an option to limit the cache size, otherwise the cache might grow indefinitely if a client always requests new hostnames and they have a high TTL.

kyl0b1te commented 6 years ago

@kelunik hi, I can help you with this. Can you just describe how you see it ?

kelunik commented 6 years ago

@zhikiri Awesome! For limiting the number of items in the cache (not its exact size), it just needs a few small changes:

https://github.com/amphp/cache/blob/d6ab81ce35019b0fd84ca6d665fd4817bfd11701/lib/ArrayCache.php#L89

Here it needs to unset() the key before setting it, to make sure it's at the end of the array. If there's a $maxSize and the size of the array is larger than $maxSize, it needs to shift the first item in the array. That'll give us a LRU implementation.

kyl0b1te commented 6 years ago

@kelunik Got it.

kyl0b1te commented 6 years ago

@kelunik here is the PR for this https://github.com/amphp/cache/pull/5

kelunik commented 6 years ago

Resolved via #5.