Class: NSCache
Overview
An NSCache object is a collection-like container, or cache, that stores key-value pairs, similar to the NSDictionary class. Developers often incorporate caches to temporarily store objects with transient data that are expensive to create. Reusing these objects can provide performance benefits, because their values do not have to be recalculated. However, the objects are not critical to the application and can be discarded if memory is tight. If discarded, their values will have to be recomputed again when needed.
Instance Method Summary (collapse)
-
- countLimit
Returns the maximum number of objects that the cache can currently hold.
-
- delegate
Returns the cache’s delegate.
-
- evictsObjectsWithDiscardedContent
Returns whether or not the cache will automatically evict discardable-content objects whose content has been discarded.
-
- name
Returns the name of the cache.
-
- objectForKey:
Returns the value associated with a given key.
-
- removeAllObjects
Empties the cache.
-
- removeObjectForKey:
Removes the value of the specified key in the cache.
-
- setCountLimit:
Sets the maximum number of objects that the cache can hold.
-
- setDelegate:
Makes the given object the cache’s delegate.
-
- setEvictsObjectsWithDiscardedContent:
Sets whether the cache will automatically evict NSDiscardableContent objects after the object’s content has been discarded.
-
- setName:
Sets the cache’s name attribute to a specific string.
-
- setObject:forKey:
Sets the value of the specified key in the cache.
-
- setObject:forKey:cost:
Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.
-
- setTotalCostLimit:
Sets the maximum total cost that the cache can have before it starts evicting objects.
-
- totalCostLimit
Returns the maximum total cost that the cache can have before it starts evicting objects.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Method Details
- (Integer) countLimit
Returns the maximum number of objects that the cache can currently hold. By default, countLimit will be set to 0. Any countLimit less than or equal to 0 has no effect on the number of allowed entries in the cache. This limit is not a strict limit, and if the cache goes over the limit, an object in the cache could be evicted instantly, later, or possibly never, all depending on the implementation details of the cache.
- (Object) delegate
Returns the cache’s delegate. The delegate object is expected to conform to the NSCacheDelegate protocol.
- (Boolean) evictsObjectsWithDiscardedContent
Returns whether or not the cache will automatically evict discardable-content objects whose content has been discarded. By default, evictsObjectsWithDiscardedContent is set to YES.
- (String) name
Returns the name of the cache. Returns the empty string if no name is specified.
- (Object) objectForKey(key)
Returns the value associated with a given key.
- (Object) removeAllObjects
Empties the cache.
- (Object) removeObjectForKey(key)
Removes the value of the specified key in the cache.
- (Object) setCountLimit(lim)
Sets the maximum number of objects that the cache can hold. Setting the count limit to a number less than or equal to 0 will have no effect on the maximum size of the cache.
- (Object) setDelegate(del)
Makes the given object the cache’s delegate. The delegate object is expected to conform to the NSCacheDelegate protocol.
- (Object) setEvictsObjectsWithDiscardedContent(b)
Sets whether the cache will automatically evict NSDiscardableContent objects after the object’s content has been discarded.
- (Object) setName(cacheName)
Sets the cache’s name attribute to a specific string.
- (Object) setObject(obj, forKey:key)
Sets the value of the specified key in the cache. Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
- (Object) setObject(obj, forKey:key, cost:num)
Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost. The cost value is used to compute a sum encompassing the costs of all the objects in the cache. When memory is limited or when the total cost of the cache eclipses the maximum allowed total cost, the cache could begin an eviction process to remove some of its elements. However, this eviction process is not in a guaranteed order. As a consequence, if you try to manipulate the cost values to achieve some specific behavior, the consequences could be detrimental to your program. Typically, the obvious cost is the size of the value in bytes. If that information is not readily available, you should not go through the trouble of trying to compute it, as doing so will drive up the cost of using the cache. Pass in 0 for the cost value if you otherwise have nothing useful to pass, or simply use the setObject:forKey: method, which does not require a cost value to be passed in.Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
- (Object) setTotalCostLimit(lim)
Sets the maximum total cost that the cache can have before it starts evicting objects.
- (Integer) totalCostLimit
Returns the maximum total cost that the cache can have before it starts evicting objects. The default value is 0, which means there is no limit on the size of the cache. If you add an object to the cache, you may pass in a specified cost for the object, such as the size in bytes of the object. If adding this object to the cache causes the cache’s total cost to rise above totalCostLimit, the cache could automatically evict some of its objects until its total cost falls below totalCostLimit. The order in which the cache evicts objects is not guaranteed. This limit is not a strict limit, and if the cache goes over the limit, an object in the cache could be evicted instantly, at a later point in time, or possibly never, all depending on the implementation details of the cache.