Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yysskk committed Mar 25, 2019
1 parent f90b0cd commit 9a498b1
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 326 deletions.
16 changes: 14 additions & 2 deletions MemoryCache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
9801072D222CD10A00A8FDBB /* Expiration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9801072C222CD10A00A8FDBB /* Expiration.swift */; };
9801072F222CF52800A8FDBB /* MemoryCacheError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9801072E222CF52800A8FDBB /* MemoryCacheError.swift */; };
98010732222CFCFE00A8FDBB /* MemoryCacheError+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98010730222CFCE100A8FDBB /* MemoryCacheError+.swift */; };
985964582248D80400958D3D /* LRUCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 985964552248D7D400958D3D /* LRUCacheSpec.swift */; };
98F67C99222E52FF0024E426 /* CacheDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98F67C98222E52FF0024E426 /* CacheDelegate.swift */; };
EE3123AA224650BA0007697A /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE3123A9224650BA0007697A /* LinkedList.swift */; };
EE3123AC224650D30007697A /* LRUCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE3123AB224650D30007697A /* LRUCache.swift */; };
Expand Down Expand Up @@ -53,6 +54,7 @@
9801072C222CD10A00A8FDBB /* Expiration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Expiration.swift; sourceTree = "<group>"; };
9801072E222CF52800A8FDBB /* MemoryCacheError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryCacheError.swift; sourceTree = "<group>"; };
98010730222CFCE100A8FDBB /* MemoryCacheError+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MemoryCacheError+.swift"; sourceTree = "<group>"; };
985964552248D7D400958D3D /* LRUCacheSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LRUCacheSpec.swift; sourceTree = "<group>"; };
98F67C98222E52FF0024E426 /* CacheDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheDelegate.swift; sourceTree = "<group>"; };
EE3123A9224650BA0007697A /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = "<group>"; };
EE3123AB224650D30007697A /* LRUCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LRUCache.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -128,9 +130,8 @@
980106DF221F9BC400A8FDBB /* Animals.swift */,
980106C5221C172B00A8FDBB /* Info.plist */,
EE3123B5224714760007697A /* MemoryCache+.swift */,
EE3123B222470F130007697A /* MemoryCacheDelegateSpec.swift */,
98010730222CFCE100A8FDBB /* MemoryCacheError+.swift */,
980106C3221C172B00A8FDBB /* MemoryCacheSpec.swift */,
985964572248D7E600958D3D /* Specs */,
);
path = Tests;
sourceTree = "<group>";
Expand All @@ -144,6 +145,16 @@
name = Frameworks;
sourceTree = "<group>";
};
985964572248D7E600958D3D /* Specs */ = {
isa = PBXGroup;
children = (
985964552248D7D400958D3D /* LRUCacheSpec.swift */,
EE3123B222470F130007697A /* MemoryCacheDelegateSpec.swift */,
980106C3221C172B00A8FDBB /* MemoryCacheSpec.swift */,
);
path = Specs;
sourceTree = "<group>";
};
EE3123B12247097D0007697A /* LRU */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -306,6 +317,7 @@
980106E1221F9BD200A8FDBB /* Animals.swift in Sources */,
98010732222CFCFE00A8FDBB /* MemoryCacheError+.swift in Sources */,
EE3123B422470F9A0007697A /* MemoryCacheDelegateSpec.swift in Sources */,
985964582248D80400958D3D /* LRUCacheSpec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 10 additions & 3 deletions Sources/LRU/LRUCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class LRUCache<Key, Value> where Key: Hashable {
return map.count
}

var isEmpty: Bool {
return list.isEmpty
}

private var map = [Key: LinkedList<Entry>.Node]()
private let list = LinkedList<Entry>()
private let lock = NSLock()
Expand All @@ -51,6 +55,7 @@ final class LRUCache<Key, Value> where Key: Hashable {

let cache = Entry(key: key, value: value, cost: cost)
map[key] = list.append(cache)
totalCost += cost
_sync()
}

Expand Down Expand Up @@ -80,6 +85,7 @@ final class LRUCache<Key, Value> where Key: Hashable {

private func _remove(_ node: LinkedList<Entry>.Node, for key: Key) {
list.remove(node)
totalCost -= node.value.cost
delegate?.cache(willEvict: node.value.value)
map.removeValue(forKey: key)
}
Expand All @@ -90,19 +96,20 @@ final class LRUCache<Key, Value> where Key: Hashable {

map = [:]
list.removeAll()
totalCost = 0
}

private func _sync() {
if totalCostLimit > 0 {
_sync(while: { totalCostLimit > totalCost })
_sync(while: { totalCostLimit < totalCost })
}
if countLimit > 0 {
_sync(while: { countLimit > count })
_sync(while: { countLimit < count })
}
}

private func _sync(while condition: () -> Bool) {
guard !condition(),
guard condition(),
let node = list.last else { return }
_remove(node, for: node.value.key)
_sync(while: condition)
Expand Down
14 changes: 13 additions & 1 deletion Sources/MemoryCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ open class MemoryCache {
}
}

public var totalCost: Int {
return cache.totalCost
}

public var count: Int {
return cache.count
}

public var isEmpty: Bool {
return cache.isEmpty
}

private let cache: LRUCache<AnyKey, AnyCache>
private let _delegate = CacheDlegate()

Expand All @@ -73,7 +85,7 @@ open class MemoryCache {
switch value {
case let .some(wrapped):
let anyCache = AnyCache(value: wrapped, expiration: expiration)
cache.set(anyCache, for: AnyKey(key: key))
cache.set(anyCache, for: AnyKey(key: key), cost: cost)
case .none:
remove(for: key)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MemoryCache+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ extension MemoryCache {
enum Key {
static let dog = StringKey<Dog>("dog")
static let dog2 = StringKey<Dog>("dog2")
static let cat = StringKey<Cat>("cat")
static let cat = HashKey<Cat>("cat".hashValue)
}
}
Loading

0 comments on commit 9a498b1

Please sign in to comment.