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.
@zhikiri Awesome! For limiting the number of items in the cache (not its exact size), it just needs a few small changes:
Line 89 in d6ab81c
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.
Resolved via #5.