Fix bug and optimize allocation of cache data