一个简单的iOS和Mac OS X持久层,具有内存缓存、可选的持久化、预加载、命名空间和优美的语法。
存储
GBStorageSimple[@"name"] = @"Luka"; // Caches object into memory only
[GBStorageSimple saveAll]; // Persistent in memory objects to disk
读取
GBStorageSimple[@"name"]; // Returns "Luka", checks the in-memory cache first, then the disk cache. If not found returns nil
特定同步
[GBStorageSimple save:@"name"]; // Persists a specific object to disk
预加载缓存
[GBStorageSimple preload:@"bigObject"]; // Load an object into memory for fast future access
清除缓存
[GBStorageSimple removeAllFromMemory]; // Evicts entire in-memory cache, but leaves all previosuly peristed files on disk.
[GBStorageSimple removeFromMemory:@"name"]; // Evicts a single key from the in-memory cache. Keep in mind this only releases the strong pointer that GBStorage holds to the object, if your app still holds a strong pointer somewhere then the object will remain in the application memory (however it will be removed from the context of GBStorage).
删除
[GBStorageSimple removePermanently:@"bigObject"]; // Removes object from both the in-memory and on-disk cache
如果您想将GBStorage用作具有最大内存使用率的持久缓存
GBStorageSimple.maxInMemoryCacheCapacity = 1000000; // set the max in-memory cache capacity to 1MB
[GBStorageSimple setObject:@"someObject" forKey:@"key" withSize:100]; // insert an object into the cache with a known size
别忘了导入头文件
#import <GBStorage/GBStorage.h>
您可以为存储控制器指定命名空间,例如用于应用的不同部分,或用于库以避免冲突。
要使用命名空间,请使用GBStorage()语法来返回带有命名空间的GBStorageController实例。GBStorage上的所有操作都将隔离到命名空间,例如,要从名为“myLibrary”的命名空间中从内存和磁盘上删除所有键,您将使用GBStorage(NSString *namespace)
获取正确的实例,然后使用-[GBStorageController removeAllPermanently]
。示例
[GBStorage(@"myLibrary") removeAllPermanently];
保存和查询的工作方式相同
GBStorage(@"myLibrary")[@"name"] = @"Luka";
[GBStorage(@"myLibrary") save:@"name"];
命名空间是隔离的
GBStorage(@"namespace1")[@"color"] = @"blue"; // stores the object into in-memory chace of namespace1
GBStorage(@"namespace1")[@"color"]; // returns @"blue"
GBStorage(@"namespace2")[@"color"]; // returns nil
[GBStorage(@"namespace1") saveAll]; // persists all objects in the in-memory cache in namespace1, but does NOT persistent any objects in any other namespaces
如果您不想使用命名空间,或者为了与GBStorage 1.x.x的向后数据兼容,您可以传递nil
。有几种避免使用命名空间的方法,每种方法的语义相同,但语法略有不同。
GBStorage(nil)[@"someKey"] = @"someObject"; // (1) 2.x.x style syntax
GBStorageSimple[@"someKey"] = @"someObject"; // (2) 1.x.x style syntax. Designed with upgrading from 1.x.x to 2.x.x in mind, can be used in a simple find&replace.
[GBStorageController sharedControllerForNamespace:nil][@"someKey"]; // (3) Actual ObjC method implementation which styles (1) and (2) just call into. It's a little verbose so (1) is it's syntactically sugar'd up version.
存储在内存缓存中的对象只是简单地使用强指针保留。如果它们可能更改它,则传递给GBStorageController
的副本可能是一个好主意。一旦您将对象存储在GBStorageController
中,您就可以修改该对象,但您必须注意,除非您调用-[GBStorageController save]
,否则更改不会持久到磁盘。由于性能原因,对象不会自动复制。
键需要是NSString
类型。它们被自动复制,以避免在您修改它们时出现未定义的行为。
在使用命名空间的情况下,如果修改了已在多个命名空间中缓存的对象,所有命名空间都将看到新值(同样,因为它只是保存了一个对它的强指针)。
传递给GBStorageController
的对象必须遵循NSCoding
协议。如果您想的话,也可以提供自己的序列化和反序列化程序,例如用于图像或json。
该库、CocoaPod和界面都使用同一名为GBStorage
。实际上实现一切的类(只有一个)称为GBStorageController
。
无。
CocoaPod库在版本2中已从GBStorageController
更名为GBStorage
。
版权所有 2016 卢卡·米罗塞尔维亚克
根据Apache许可证版本2.0(“许可证”);除非符合许可证的要求或者以书面形式达成协议,否则不得使用本作品。您可以在LICENSE文件中获取许可证的副本,或者在此处
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或者书面同意,否则在许可证下分发的软件按“原样”提供,不提供任何明示或暗示的保证或条件。有关许可权和管理下的许可和限制,请参见许可协议。