zoukankan      html  css  js  c++  java
  • Swift项目开发中缓存计算以及清除

    //
    //  KMCacheTool.swift
    //  StopSmokingPrograms
    //
    //  Created by Fran on 15/10/15.
    //  Copyright © 2015年 kimree. All rights reserved.
    //
    
    import UIKit
    
    class KMCacheTool: NSObject {
        // 计算缓存大小
        static var cacheSize: String{
            get{
                let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
                let fileManager = NSFileManager.defaultManager()
                
                func caculateCache() -> Float{
                    var total: Float = 0
                    if fileManager.fileExistsAtPath(basePath!){
                        let childrenPath = fileManager.subpathsAtPath(basePath!)
                        if childrenPath != nil{
                            for path in childrenPath!{
                                let childPath = basePath!.stringByAppendingString("/").stringByAppendingString(path)
                                do{
                                    let attr = try fileManager.attributesOfItemAtPath(childPath)
                                    let fileSize = attr["NSFileSize"] as! Float
                                    total += fileSize
                                    
                                }catch _{
                                    
                                }
                            }
                        }
                    }
                    
                    return total
                }
                
                
                let totalCache = caculateCache()
                return NSString(format: "%.2f MB", totalCache / 1024.0 / 1024.0 ) as String
            }
        }
        
        // 清除缓存
        class func clearCache() -> Bool{
            var result = true
            let basePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
            let fileManager = NSFileManager.defaultManager()
            if fileManager.fileExistsAtPath(basePath!){
                let childrenPath = fileManager.subpathsAtPath(basePath!)
                for childPath in childrenPath!{
                    let cachePath = basePath?.stringByAppendingString("/").stringByAppendingString(childPath)
                    do{
                        try fileManager.removeItemAtPath(cachePath!)
                    }catch _{
                        result = false
                    }
                }
            }
            
            return result
        }
    }
    

      

  • 相关阅读:
    exec系列函数和system函数
    fork函数相关总结
    文件的内核结构file和dup实现重定向
    进程基本概述
    fcntl 函数与文件锁
    文件的属性
    目录的操作
    文件的读取写入
    文件的打开关闭
    浅谈原始套接字 SOCK_RAW 的内幕及其应用(port scan, packet sniffer, syn flood, icmp flood)
  • 原文地址:https://www.cnblogs.com/FranZhou/p/5066899.html
Copyright © 2011-2022 走看看