memoize function with cachesize limit