amphp / cache

A fiber-aware cache API based on Amp and Revolt.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add maxSize option to ArrayCache

kelunik opened this issue · comments

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.

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

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

$this->sharedState->cache[$key] = $value;

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.

@kelunik Got it.

@kelunik here is the PR for this #5

Resolved via #5.