如果没有使用数据或手动将数据存放了很长时间,是否可以清除URLCache中存储的数据响应?

时间:2019-05-13 22:59:54

标签: ios swift nsurlcache nscache

我有一个为URLCache提供一些配置的类,我在寻找一种方法来手动清除缓存中存储的数据,以防长时间不使用该数据。

    class DataCacher {

private var observer: NSObjectProtocol!

let defaultMaxMemoryLimit = 30

static var shared = DataCacher()

var sharedCache = URLCache()


init() {

   didreceiveMemoryWarning()
  URLCache.shared  = sharedCache
   }

init(maxMemoryLimit: Int) {
     didreceiveMemoryWarning()
      sharedCache =  URLCache(
        memoryCapacity: maxMemoryLimit*1024*1024,
        diskCapacity: 0,
        diskPath: nil
    )

    URLCache.shared  = sharedCache

}

private func didreceiveMemoryWarning(){

observer = NotificationCenter.default.addObserver(forName: UIApplication.didReceiveMemoryWarningNotification, object: nil, queue: nil, using: {_ in
    self.sharedCache.removeAllCachedResponses()
    })}

}

0 个答案:

没有答案