Class: NSCache

Inherits:
NSObject show all

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)

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.

Returns:

  • (Integer)

    The maximum number of objects that the cache can currently hold.

- (Object) delegate

Returns the cache’s delegate. The delegate object is expected to conform to the NSCacheDelegate protocol.

Returns:

  • (Object)

    The application delegate object.

- (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.

Returns:

  • (Boolean)

    YES if the cache will evict the object after it is discarded; otherwise, NO.

- (String) name

Returns the name of the cache. Returns the empty string if no name is specified.

Returns:

  • (String)

    The name of the cache.

- (Object) objectForKey(key)

Returns the value associated with a given key.

Parameters:

  • key (Object)

    An object identifying the value.

Returns:

  • (Object)

    The value associated with key, or NULL if no value is associated with key. The caller does not have to release the value returned to it.

- (Object) removeAllObjects

Empties the cache.

Returns:

- (Object) removeObjectForKey(key)

Removes the value of the specified key in the cache.

Parameters:

  • key (Object)

    The key identifying the value to be removed.

Returns:

- (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.

Parameters:

  • lim (Integer)

    The maximum number of objects that the cache will be allowed to hold.

Returns:

- (Object) setDelegate(del)

Makes the given object the cache’s delegate. The delegate object is expected to conform to the NSCacheDelegate protocol.

Parameters:

  • del (Object)

    The object to be registered as the delegate.

Returns:

- (Object) setEvictsObjectsWithDiscardedContent(b)

Sets whether the cache will automatically evict NSDiscardableContent objects after the object’s content has been discarded.

Parameters:

  • b (Boolean)

    If YES, the cache evicts NSDiscardableContent objects after the object’s contents has been discarded; if NO the cache does not evict these objects.

Returns:

- (Object) setName(cacheName)

Sets the cache’s name attribute to a specific string.

Parameters:

  • cacheName (String)

    The new name for the cache.

Returns:

- (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.

Parameters:

  • obj (Object)

    The object to be stored in the cache.

  • key (Object)

    The key with which to associate the value.

Returns:

- (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.

Parameters:

  • obj (Object)

    The object to store in the cache.

  • key (Object)

    The key with which to associate the value.

  • num (Integer)

    The cost with which to associate the key-value pair.

Returns:

- (Object) setTotalCostLimit(lim)

Sets the maximum total cost that the cache can have before it starts evicting objects.

Parameters:

  • lim (Integer)

    The maximum total cost that the cache can have before it starts evicting objects.

Returns:

- (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.

Returns:

  • (Integer)

    The current maximum cost that the cache can have before it starts evicting objects.