How to use NSCache

Can someone give an example on how to use NSCache to cache a string? Or anyone has a link to a good explanation? I can't seem to find any..

62810 次浏览

您使用它的方式与使用 NSMutableDictionary的方式相同。不同之处在于,当 NSCache检测到内存压力过大(即缓存了太多值)时,它会释放一些值以腾出空间。

如果您可以在运行时重新创建这些值(通过从 Internet 下载、进行计算等方式) ,那么 NSCache可能适合您的需要。如果数据不能被重新创建(例如,它的用户输入,它是时间敏感的,等等) ,那么你不应该把它存储在 NSCache中,因为它会在那里被销毁。

例如,不考虑线程安全性:

// Your cache should have a lifetime beyond the method or handful of methods
// that use it. For example, you could make it a field of your application
// delegate, or of your view controller, or something like that. Up to you.
NSCache *myCache = ...;
NSAssert(myCache != nil, @"cache object is missing");


// Try to get the existing object out of the cache, if it's there.
Widget *myWidget = [myCache objectForKey: @"Important Widget"];
if (!myWidget) {
// It's not in the cache yet, or has been removed. We have to
// create it. Presumably, creation is an expensive operation,
// which is why we cache the results. If creation is cheap, we
// probably don't need to bother caching it. That's a design
// decision you'll have to make yourself.
myWidget = [[[Widget alloc] initExpensively] autorelease];


// Put it in the cache. It will stay there as long as the OS
// has room for it. It may be removed at any time, however,
// at which point we'll have to create it again on next use.
[myCache setObject: myWidget forKey: @"Important Widget"];
}


// myWidget should exist now either way. Use it here.
if (myWidget) {
[myWidget runOrWhatever];
}
@implementation ViewController
{
NSCache *imagesCache;
}




- (void)viewDidLoad
{
imagesCache = [[NSCache alloc] init];
}




// How to save and retrieve NSData into NSCache
NSData *imageData = [imagesCache objectForKey:@"KEY"];
[imagesCache setObject:imageData forKey:@"KEY"];

样本计划 添加 CacheController.h 和。M 文件从示例项目到您的项目。在要缓存数据的类中,放置以下代码。

[[CacheController storeInstance] setCache:@"object" forKey:@"objectforkey" ];

你可以设置任何对象

[[CacheController storeInstance] getCacheForKey:@"objectforkey" ];

to retrive

重要提示: NSCache 类包含各种自动删除策略。如果希望将数据缓存为永久的,或者希望在特定时间内删除缓存的数据,请执行以下操作。

Sample code for caching a string using NSCache in Swift:

var cache = NSCache()
cache.setObject("String for key 1", forKey: "Key1")
var result = cache.objectForKey("Key1") as String
println(result) // Prints "String for key 1"

要创建单个应用程序范围的 NSCache 实例(单例) ,可以轻松扩展 NSCache 以添加 sharedInstance 属性。只需将下面的代码放入一个名为 NSCache + Singleton.swift 的文件中:

import Foundation


extension NSCache {
class var sharedInstance : NSCache {
struct Static {
static let instance : NSCache = NSCache()
}
return Static.instance
}
}

然后你可以在应用程序的任何地方使用缓存:

NSCache.sharedInstance.setObject("String for key 2", forKey: "Key2")
var result2 = NSCache.sharedInstance.objectForKey("Key2") as String
println(result2) // Prints "String for key 2"

缓存的对象不应该实现 NSDisardableContent 协议吗?

来自 NSCache 类引用: 存储在 NSCache 对象中的公共数据类型是实现 NSDiscarableContent 协议的对象。将这种类型的对象存储在缓存中是有好处的,因为它的内容可以在不再需要时丢弃,从而节省内存。默认情况下,如果丢弃缓存中的 NSDisardableContent 对象的内容,则该对象将自动从缓存中删除,不过可以更改此自动删除策略。如果将 NSDisardableContent 对象放入缓存,则缓存在删除该对象时将对其调用抛弃 ContentIfCan。