zoukankan      html  css  js  c++  java
  • 清除缓存的实现

     // 在子线程计算缓存大小
            dispatch_async(dispatch_get_global_queue(0, 0), ^{// 获得缓存文件夹路径
                unsigned long long size = file.fileSize;
                size += [SDImageCache sharedImageCache].getSize;
                NSString *sizeText = nil;
                if (size >= pow(10, 9)) { // size >= 1GB
                    sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
                } else if (size >= pow(10, 6)) { // 1GB > size >= 1MB
                    sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
                } else if (size >= pow(10, 3)) { // 1MB > size >= 1KB
                    sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
                } else { // 1KB > size
                    sizeText = [NSString stringWithFormat:@"%zdB", size];
                }
    // 生成文字
                NSString *text = [NSString stringWithFormat:@"清除缓存(%@)", sizeText];
                
                // 回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 设置文字
                  // 恢复点击事件
                    self.userInteractionEnabled = YES;
                });
            });
        }
        return self;
    }
    /**
     *  清除缓存
     */
    - (void)clearCache
    {
        // 弹出指示器
        [SVProgressHUD showWithStatus:@"正在清除缓存..." maskType:SVProgressHUDMaskTypeBlack];
        
        // 删除SDWebImage的缓存
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            // 删除自定义的缓存
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSFileManager *mgr = [NSFileManager defaultManager];
                [mgr removeItemAtPath:file error:nil];
                [mgr createDirectoryAtPath:XMGCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
                // 所有的缓存都清除完毕
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 隐藏指示器
                    [SVProgressHUD dismiss];
                    
                    // 设置文字
                    self.textLabel.text = @"清除缓存(0B)";
                });
            });
        }];
    }
  • 相关阅读:
    .NET技术对软件行业的积极作用
    ADO.NET Entityframework MYSQL provider
    用杯子量水问题通用解法
    详解.NET异步
    说 框架、架构、模式、重构
    谈 三层结构与MVC模式的区别
    面试题一例分享
    LINQ notes2 lambda expression
    毕业论文B.3 DTW算法
    LINQ notes1 intro
  • 原文地址:https://www.cnblogs.com/yintingting/p/5467817.html
Copyright © 2011-2022 走看看