Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is NSCache?

Can someone briefly explain to me when I would need to use NSCache? I read the documentation and I don't get it. Like, give me an example of a scenario where NSCache would be useful? Thanks.

like image 791
sumderungHAY Avatar asked Jun 22 '11 20:06

sumderungHAY


People also ask

Is NSCache thread safe?

One more thing about NSCache is, it is thread safe. We can access it from any thread without worrying about managing threads while accessing NSCache. You can set & get name for cache. The default value is an empty string (“”).

What is URLCache?

The URLCache class implements the caching of responses to URL load requests, by mapping NSURLRequest objects to CachedURLResponse objects. It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions.

What is caching in IOS?

Content caching is a service in macOS that speeds up downloading of software distributed by Apple and data that users store in iCloud by saving content that local Apple devices have already downloaded.

How do I cache API response in Swift?

Can cache many swift type, and custom class which inherit NSObject and conform NSCoding protocol. To implement: First, it use NSCache for mem cache. NSCache use like a dictionary. Second, save cache to disk, use NSFileManager methods.


1 Answers

It's more or less just like a dictionary, with the following additional things (as mentioned by the docs):

The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.

You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

Retrieving something from an NSCache object returns an autoreleased result.

Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

You might use it if your application has lots of data that it needs to work with, but you can't keep it all in memory. For example, if you had an app that pulled data from an SQL Lite database or a web service, you might store it in an NSCache after looking it up. Then, when you need it again, you can check the cache first and only need to hit the database if it isn't in the cache. The main advantage in this scenario over using a regular dictionary is that if you put too much stuff in the cache and it starts to fill up memory, it will automatically discard things to free up memory for you.

like image 154
Eric Petroelje Avatar answered Oct 05 '22 19:10

Eric Petroelje