NSCache auto-removal policy

前端 未结 2 737
野性不改
野性不改 2021-02-08 23:36

What are some of the NSCache\'s auto-removal policies? Apple\'s documentation does not mention them, and I experimentally discovered that NSCache does not respond to memory war

2条回答
  •  独厮守ぢ
    2021-02-09 00:09

    You're best off treating NSCache as a black box, as much as you can.

    From Caching and Purgeable Memory (emphasis mine):

    When adding items to a cache, you can specify a cost value to be associated with each key-value pair. Call the setTotalCostLimit: method to set the maximum value for the sum of all the cached objects’ costs. Thus, when an object is added that pushes the totalCost above the totalCostLimit, the cache could automatically evict some of its objects in order to get back below the threshold. This eviction process is not guaranteed, so trying to manipulate the cost values to achieve specific behavior could be detrimental to the performance of the cache. Pass in 0 for the cost if you have nothing useful, or use the setObject:forKey: method, which does not require a cost to be passed in.

    Note: The count limit and the total-cost limit are not strictly enforced. That is, when the cache goes over one of its limits, some of its objects might get evicted immediately, later, or never, all depending on the implementation details of the cache.

提交回复
热议问题