NSCache
誰かが文字列をキャッシュする方法の例を挙げてもらえますか? または、誰かが良い説明へのリンクを持っていますか? 私は何も見つからないようです..
5 に答える
を使用するのと同じ方法で使用しますNSMutableDictionary
。違いは、NSCache
が過剰なメモリ プレッシャ (つまり、あまりにも多くの値をキャッシュしている) を検出すると、それらの値の一部を解放してスペースを空けることです。
実行時にこれらの値を再作成できる場合 (インターネットからダウンロードしたり、計算を行ったりして) 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"];
Swift で NSCache を使用して文字列をキャッシュするサンプル コード:
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"
サンプル プロジェクト サンプル プロジェクト の CacheController.h および .m ファイルをプロジェクトに追加します。データをキャッシュしたいクラスに、以下のコードを入れます。
[[CacheController storeInstance] setCache:@"object" forKey:@"objectforkey" ];
これを使用して任意のオブジェクトを設定できます
[[CacheController storeInstance] getCacheForKey:@"objectforkey" ];
取得する
重要: NSCache クラスには、さまざまな自動削除ポリシーが組み込まれています。データを永続的にキャッシュしたい場合、または特定の時間にキャッシュされたデータを削除したい場合は、 この回答を参照してください。
キャッシュされたオブジェクトは NSDiscardableContent プロトコルを実装すべきではありませんか?
NSCache クラス リファレンスから: NSCache オブジェクトに格納される一般的なデータ型は、NSDiscardableContent プロトコルを実装するオブジェクトです。このタイプのオブジェクトをキャッシュに格納すると、不要になったときにコンテンツを破棄できるため、メモリを節約できるというメリットがあります。デフォルトでは、コンテンツが破棄された場合、キャッシュ内の NSDiscardableContent オブジェクトはキャッシュから自動的に削除されますが、この自動削除ポリシーは変更できます。NSDiscardableContent オブジェクトがキャッシュに入れられた場合、キャッシュはその削除時にその上で discardContentIfPossible を呼び出します。