implemented cache with a limited size to avoid running out of memory