Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom disk cache and memory cache #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Haneke/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ extension HanekeGlobals {

}

public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable> {
public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable>: HanekeCache<T, DiskCache, NSCache>{

public override init(name: String) {
super.init(name: name)
}
}

public class HanekeCache<T: DataConvertible, DiskCacheT, MemoryCacheT where T.Result == T, T : DataRepresentable, DiskCacheT: DiskCache, MemoryCacheT: NSCache> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having MemoryCacheT: NSCache as generic constraint means that the memory cache must be a NSCache subclass, while subclassing NSCache might be not the main preference of a developer while building a custom memory caching system. I recommend to protocols DiskCache and MemoryCache to give complete implementation freedom to the developer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct. but I need someone to ensure that this PR is going to be merged before I spend time on this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fair. I'm not the maintainer so I cannot give you this guarantee, but the swift 4 branch was just merged.


let name: String

Expand Down Expand Up @@ -76,7 +83,7 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable
}

public func fetch(key key: String, formatName: String = HanekeGlobals.Cache.OriginalFormatName, failure fail : Fetch<T>.Failer? = nil, success succeed : Fetch<T>.Succeeder? = nil) -> Fetch<T> {
let fetch = Cache.buildFetch(failure: fail, success: succeed)
let fetch = HanekeCache.buildFetch(failure: fail, success: succeed)
if let (format, memoryCache, diskCache) = self.formats[formatName] {
if let wrapper = memoryCache.objectForKey(key) as? ObjectWrapper, let result = wrapper.value as? T {
fetch.succeed(result)
Expand All @@ -101,7 +108,7 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable

public func fetch(fetcher fetcher : Fetcher<T>, formatName: String = HanekeGlobals.Cache.OriginalFormatName, failure fail : Fetch<T>.Failer? = nil, success succeed : Fetch<T>.Succeeder? = nil) -> Fetch<T> {
let key = fetcher.key
let fetch = Cache.buildFetch(failure: fail, success: succeed)
let fetch = HanekeCache.buildFetch(failure: fail, success: succeed)
self.fetch(key: key, formatName: formatName, failure: { error in
if error?.code == HanekeGlobals.Cache.ErrorCode.FormatNotFound.rawValue {
fetch.fail(error)
Expand Down Expand Up @@ -167,20 +174,20 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable

// MARK: Formats

var formats : [String : (Format<T>, NSCache, DiskCache)] = [:]
var formats : [String : (Format<T>, MemoryCacheT, DiskCacheT)] = [:]

public func addFormat(format : Format<T>) {
let name = format.name
let formatPath = self.formatPath(formatName: name)
let memoryCache = NSCache()
let diskCache = DiskCache(path: formatPath, capacity : format.diskCapacity)
let memoryCache = MemoryCacheT()
let diskCache = DiskCacheT(path: formatPath, capacity : format.diskCapacity)
self.formats[name] = (format, memoryCache, diskCache)
}

// MARK: Internal

lazy var cachePath: String = {
let basePath = DiskCache.basePath()
let basePath = DiskCacheT.basePath()
let cachePath = (basePath as NSString).stringByAppendingPathComponent(self.name)
return cachePath
}()
Expand All @@ -204,7 +211,7 @@ public class Cache<T: DataConvertible where T.Result == T, T : DataRepresentable
return value.asData()
}

private func fetchFromDiskCache(diskCache : DiskCache, key: String, memoryCache : NSCache, failure fail : ((NSError?) -> ())?, success succeed : (T) -> ()) {
private func fetchFromDiskCache(diskCache : DiskCacheT, key: String, memoryCache : MemoryCacheT, failure fail : ((NSError?) -> ())?, success succeed : (T) -> ()) {
diskCache.fetchData(key: key, failure: { error in
if let block = fail {
if (error?.code == NSFileReadNoSuchFileError) {
Expand Down
2 changes: 1 addition & 1 deletion Haneke/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DiskCache {
return cacheQueue
}()

public init(path: String, capacity: UInt64 = UINT64_MAX) {
public required init(path: String, capacity: UInt64 = UINT64_MAX) {
self.path = path
self.capacity = capacity
dispatch_async(self.cacheQueue, {
Expand Down