Edited to consider max cache size