{"action":"created","issue":{"url":"https://api.github.com/repos/ibireme/YYKit/issues/134","repository_url":"https://api.github.com/repos/ibireme/YYKit","labels_url":"https://api.github.com/repos/ibireme/YYKit/issues/134/labels{/name}","comments_url":"https://api.github.com/repos/ibireme/YYKit/issues/134/comments","events_url":"https://api.github.com/repos/ibireme/YYKit/issues/134/events","html_url":"https://github.com/ibireme/YYKit/issues/134","id":140868476,"number":134,"title":"尝试使用 YYDiskCache 做持久化遇到的一个疑问","user":{"login":"KittenYang","id":5517281,"avatar_url":"https://avatars.githubusercontent.com/u/5517281?v=3","gravatar_id":"","url":"https://api.github.com/users/KittenYang","html_url":"https://github.com/KittenYang","followers_url":"https://api.github.com/users/KittenYang/followers","following_url":"https://api.github.com/users/KittenYang/following{/other_user}","gists_url":"https://api.github.com/users/KittenYang/gists{/gist_id}","starred_url":"https://api.github.com/users/KittenYang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KittenYang/subscriptions","organizations_url":"https://api.github.com/users/KittenYang/orgs","repos_url":"https://api.github.com/users/KittenYang/repos","events_url":"https://api.github.com/users/KittenYang/events{/privacy}","received_events_url":"https://api.github.com/users/KittenYang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-03-15T04:24:21Z","updated_at":"2016-03-16T03:38:47Z","closed_at":null,"body":"您好，我尝试使用 YYDiskCache 给网络请求到的微博数据做持久化，我是直接存的 layout 对象，所以必须给 layout 类实现 NSCoding 方法，layout 还有 status ,status 里又有 user，所以每个类我都要实现 NSCoding 方法，繁多的属性重复同一句代码麻烦不说，关键我发现再次打开从磁盘读取的速度非常慢。我不知道是不是我不应该直接存 layout 这个自定义对象，而是应该存基本数据类型（NSString *name,NSString *text ....）这些到数据库，然后每次从数据库读取这些基本类型后再算出相应的 layout。另外我使用的是 Swift 进行开发，但我感觉关系不大。还望指点。"},"comment":{"url":"https://api.github.com/repos/ibireme/YYKit/issues/comments/197138461","html_url":"https://github.com/ibireme/YYKit/issues/134#issuecomment-197138461","issue_url":"https://api.github.com/repos/ibireme/YYKit/issues/134","id":197138461,"user":{"login":"KittenYang","id":5517281,"avatar_url":"https://avatars.githubusercontent.com/u/5517281?v=3","gravatar_id":"","url":"https://api.github.com/users/KittenYang","html_url":"https://github.com/KittenYang","followers_url":"https://api.github.com/users/KittenYang/followers","following_url":"https://api.github.com/users/KittenYang/following{/other_user}","gists_url":"https://api.github.com/users/KittenYang/gists{/gist_id}","starred_url":"https://api.github.com/users/KittenYang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KittenYang/subscriptions","organizations_url":"https://api.github.com/users/KittenYang/orgs","repos_url":"https://api.github.com/users/KittenYang/repos","events_url":"https://api.github.com/users/KittenYang/events{/privacy}","received_events_url":"https://api.github.com/users/KittenYang/received_events","type":"User","site_admin":false},"created_at":"2016-03-16T03:38:47Z","updated_at":"2016-03-16T03:38:47Z","body":"恩，实测发现都消耗在了 Archive/Unarchive 上了。然后我阅读了下 diskCache 的源码，之后又对比了 FMDB，有一点非常疑惑。\r\n首先，您在存简单数据`(NSNumber)`的时候，做法是用 `[NSKeyedArchiver archivedDataWithRootObject:object] ` 把 object 转成 NSData（实际上我发现你把所有 object 都转成了 NSData），然后统一使用 `sqlite3_bind_blob(stmt, 7, extendedData.bytes, (int)extendedData.length, 0);` 进行数据库存储。但就像你说的：\r\n>\"Archive/Unarchive 确实比较慢，而且还需要消耗更多的磁盘空间和 I/O。\"\r\n\r\n所以我又去翻看了[FMDB](https://github.com/ccgus/fmdb) 的源码，发现它进行了区分：\r\n```objc\r\n    else if ([obj isKindOfClass:[NSNumber class]]) {\r\n        if (strcmp([obj objCType], @encode(char)) == 0) {\r\n            sqlite3_bind_int(pStmt, idx, [obj charValue]);\r\n        }\r\n        else if (strcmp([obj objCType], @encode(unsigned char)) == 0) {\r\n            sqlite3_bind_int(pStmt, idx, [obj unsignedCharValue]);\r\n        }\r\n        else if (strcmp([obj objCType], @encode(short)) == 0) {\r\n            sqlite3_bind_int(pStmt, idx, [obj shortValue]);\r\n        }\r\n        else if (strcmp([obj objCType], @encode(unsigned short)) == 0) {\r\n            sqlite3_bind_int(pStmt, idx, [obj unsignedShortValue]);\r\n        }\r\n        else if (strcmp([obj objCType], @encode(int)) == 0) {\r\n            sqlite3_bind_int(pStmt, idx, [obj intValue]);\r\n        }\r\n        .....\r\n```\r\n可以看到，FMDB 对于基本类型的 object 存储的都是基本类型本身。\r\n这方面还请 YY 解惑一下。\r\n\r\n"}}