Use of the DDGDataCache_OC library for advanced caching of iOS files

DDGDataCache

SWIFT version synchronous issue Please move: DDGDataCache_Swift may require that the last data to be displayed in the offline state be cached first. This involves caching, but caching can be classified into file caching, archiving, etc. In reference to other people’s cache open source project, the function is too simple, for example: I want to delete the cache information under a certain file, most of them only provide to delete all the cache, can not be deleted by directory. I have compiled a library that can be asynchronously deleted by directory. Simple and convenient, fast, is a necessary tool for home iOS code farmers, first look at the functional effect. Simple, convenient, fast, home iOS code farmers essential tools, the specific function modules are as follows.

// Build mock data /** * Mock data request URL */let URL = "https://github.com/dudongge"/** * responseObject = ["sex" : "Male"."city" : "Shanghai"."logintime" : "1445267749"."name" : "Master of the East Pavilion"."group" : "3"."loginhit" : "4"."id" : "234328"."QQ" : "532835032" ]
Copy the code

1. Synchronous write

-parameter jsonResponse: data to be written (JSON) -parameter URL: Data request url-parameter path: indicates the path of the level-1 folder. Parameter subPath: indicates the path of the level-2 folder. SubPath (configurable - Optional) - RETURNS: indicates the path of the level-2 folder. BOOL written success result = [DDGDataCache saveJsonResponseToCacheFile: self. ResponseObject andURL: self. The URL path: @"appCahe" andSubPath:@""];
    if (result) {
        NSString *logStr = @"😆 synchronous write to main directory) saved/updated successfully";
    } else {
        NSString *logStr = @"😤(synchronous write to main directory) save/update failed";
    }
Copy the code

1. Asynchronous write

Write/update cache (asynchronous) [Cache by APP version, different APP versions, the same interface cache data does not interfere with each other] -parameter jsonResponse: data to be written (JSON) -parameter URL: Data request url-parameter subPath: path of the secondary folder subPath (configurable - optional) -parameter completed: Asynchronous completion callback (main thread callback) [DDGDataCache save_asyncJsonResponseToCacheFile: self. ResponseObject andURL: self. The URL path: @"appCahe" andSubPath:@"MaName" completed:^(BOOL   result) {
        if (result) {
            NSString *logStr = @"📷 synchronous write/update cache successful (with secondary directory)";
        } else {
            NSString *logStr = @"😤(synchronous write/update cache (with secondary directory) save/update failed"; }}];Copy the code

2. Obtain the cached data

-parameter URL: indicates the data request url-parameter subPath: indicates the path of the secondary folder subPath (configurable - optional) - RETURNS: indicates the path of the secondary folder. Cache object id json = [DDGDataCache cacheJsonWithURL: self. The URL subPath: @""];
    NSString *jsonStr = [NSString stringWithFormat:@"% @",json]; [self updadateLog: jsonStr]; / / access to specify cache json data id = [DDGDataCache cacheJsonWithURL: self. The URL subPath: @"MaName"];
    NSString *jsonStr = [NSString stringWithFormat:@"% @",json];

Copy the code

3. Obtain the size of cached data

// Get the total cache sizefloat size = [DDGDataCache cacheAllSize];
    [self updadateLog: [NSString stringWithFormat:@"Full cache size :%fM",size]];
    print("Cache size (M)=\(size)"// Get the cache size of the specified path (pass secondary path)float size = [DDGDataCache cacheSizeWithSubPath:@"MaName"];
    [self updadateLog: [NSString stringWithFormat:@"Full cache size: %fM",size]];
    print("Cache size (M)=\(allSize)")
Copy the code

Delete the cache

BOOL result = [DDGDataCache clearAllCache];if(result) {
        print("😆 Cache cleared successfully")}else {
        print("😤 failed to clear cache"BOOL result = [DDGDataCache clearCacheWithSubPath:@"userInfo"];
    if (result) {
        print("😆 Cache cleared successfully")}else {
        print("😤 failed to clear cache")}Copy the code

Just drag the code in

Directly drag the files in the DDGDataCache folder into the folderCopy the code

License

DDGDataCache is available under the MIT license. See the LICENSE file for more info. If you have any questions, welcome to QQ: 532835032, if it is helpful to you, I hope you move the mouse, don’t hesitate to give a star.

Instructions (Note)

DDGDataCache_Swift: DDGDataCache_Swift: DDGDataCache_Swift: DDGDataCache_Swift