diff --git a/Pod/CacheTests.xcodeproj/project.pbxproj b/Pod/CacheTests.xcodeproj/project.pbxproj index d1ced0d6..75f4ccd6 100644 --- a/Pod/CacheTests.xcodeproj/project.pbxproj +++ b/Pod/CacheTests.xcodeproj/project.pbxproj @@ -16,21 +16,22 @@ D5219A061BF55AAB009D44DF /* CapsuleSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A051BF55AAB009D44DF /* CapsuleSpec.swift */; }; D5219A081BF55BD7009D44DF /* ExpirySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A071BF55BD7009D44DF /* ExpirySpec.swift */; }; D5219A0B1BF55E63009D44DF /* DefaultCacheConverterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A0A1BF55E63009D44DF /* DefaultCacheConverterSpec.swift */; }; - D5219A111BF60C09009D44DF /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A101BF60C09009D44DF /* Cache.swift */; }; - D5219A151BF62329009D44DF /* DiskCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A131BF62329009D44DF /* DiskCache.swift */; }; - D5219A161BF62329009D44DF /* MemoryCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A141BF62329009D44DF /* MemoryCache.swift */; }; - D5219A181BF6234D009D44DF /* CacheAware.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A171BF6234D009D44DF /* CacheAware.swift */; }; D5219A1A1BF62371009D44DF /* CacheTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A191BF62371009D44DF /* CacheTask.swift */; }; - D5219A1E1BF623F7009D44DF /* DiskCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A1C1BF623F7009D44DF /* DiskCacheSpec.swift */; }; - D5219A1F1BF623F7009D44DF /* MemoryCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A1D1BF623F7009D44DF /* MemoryCacheSpec.swift */; }; D5219A211BF6240D009D44DF /* CacheTaskSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A201BF6240D009D44DF /* CacheTaskSpec.swift */; }; D5219A231BF625CA009D44DF /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A221BF625CA009D44DF /* Config.swift */; }; - D5219A251BF62AA8009D44DF /* CacheFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A241BF62AA8009D44DF /* CacheFactory.swift */; }; - D5219A271BF6663D009D44DF /* CacheFactorySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A261BF6663D009D44DF /* CacheFactorySpec.swift */; }; - D5219A291BF66BD9009D44DF /* CacheKind.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A281BF66BD9009D44DF /* CacheKind.swift */; }; - D5219A2B1BF66C0A009D44DF /* CacheKindSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A2A1BF66C0A009D44DF /* CacheKindSpec.swift */; }; + D5219A291BF66BD9009D44DF /* StorageKind.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A281BF66BD9009D44DF /* StorageKind.swift */; }; + D5219A2B1BF66C0A009D44DF /* StorageKindSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A2A1BF66C0A009D44DF /* StorageKindSpec.swift */; }; D5219A2D1BF66CDC009D44DF /* ConfigSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A2C1BF66CDC009D44DF /* ConfigSpec.swift */; }; D5219A2F1BF66E8E009D44DF /* CacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5219A2E1BF66E8E009D44DF /* CacheSpec.swift */; }; + D538305C1BFA1BE600F43D58 /* StorageAware.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830571BFA1BE600F43D58 /* StorageAware.swift */; }; + D538305D1BFA1BE600F43D58 /* StorageFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830581BFA1BE600F43D58 /* StorageFactory.swift */; }; + D538305E1BFA1BE600F43D58 /* DiskStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830591BFA1BE600F43D58 /* DiskStorage.swift */; }; + D538305F1BFA1BE600F43D58 /* MemoryStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D538305A1BFA1BE600F43D58 /* MemoryStorage.swift */; }; + D53830631BFA245900F43D58 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830621BFA245900F43D58 /* Cache.swift */; }; + D53830681BFA29E100F43D58 /* DiskStorageSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830651BFA29E100F43D58 /* DiskStorageSpec.swift */; }; + D53830691BFA29E100F43D58 /* MemoryStorageSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830661BFA29E100F43D58 /* MemoryStorageSpec.swift */; }; + D538306A1BFA29E100F43D58 /* StorageFactorySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D53830671BFA29E100F43D58 /* StorageFactorySpec.swift */; }; + D5684E861BFA33AA00488B96 /* HybridCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5684E851BFA33AA00488B96 /* HybridCache.swift */; }; D5704C531BC84DAA0089F388 /* String+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5704C521BC84DAA0089F388 /* String+Cache.swift */; }; D5CD0F731BCAE5F6004C0B32 /* SpecHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5CD0F721BCAE5F6004C0B32 /* SpecHelper.swift */; }; D5EA236B1BD5C21200FEABC3 /* String+CacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5EA236A1BD5C21200FEABC3 /* String+CacheSpec.swift */; }; @@ -56,21 +57,22 @@ D5219A051BF55AAB009D44DF /* CapsuleSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapsuleSpec.swift; sourceTree = ""; }; D5219A071BF55BD7009D44DF /* ExpirySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpirySpec.swift; sourceTree = ""; }; D5219A0A1BF55E63009D44DF /* DefaultCacheConverterSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultCacheConverterSpec.swift; sourceTree = ""; }; - D5219A101BF60C09009D44DF /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = ""; }; - D5219A131BF62329009D44DF /* DiskCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskCache.swift; sourceTree = ""; }; - D5219A141BF62329009D44DF /* MemoryCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryCache.swift; sourceTree = ""; }; - D5219A171BF6234D009D44DF /* CacheAware.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheAware.swift; sourceTree = ""; }; D5219A191BF62371009D44DF /* CacheTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheTask.swift; sourceTree = ""; }; - D5219A1C1BF623F7009D44DF /* DiskCacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskCacheSpec.swift; sourceTree = ""; }; - D5219A1D1BF623F7009D44DF /* MemoryCacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryCacheSpec.swift; sourceTree = ""; }; D5219A201BF6240D009D44DF /* CacheTaskSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheTaskSpec.swift; sourceTree = ""; }; D5219A221BF625CA009D44DF /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = ""; }; - D5219A241BF62AA8009D44DF /* CacheFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheFactory.swift; sourceTree = ""; }; - D5219A261BF6663D009D44DF /* CacheFactorySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheFactorySpec.swift; sourceTree = ""; }; - D5219A281BF66BD9009D44DF /* CacheKind.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheKind.swift; sourceTree = ""; }; - D5219A2A1BF66C0A009D44DF /* CacheKindSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheKindSpec.swift; sourceTree = ""; }; + D5219A281BF66BD9009D44DF /* StorageKind.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageKind.swift; sourceTree = ""; }; + D5219A2A1BF66C0A009D44DF /* StorageKindSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageKindSpec.swift; sourceTree = ""; }; D5219A2C1BF66CDC009D44DF /* ConfigSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigSpec.swift; sourceTree = ""; }; D5219A2E1BF66E8E009D44DF /* CacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheSpec.swift; sourceTree = ""; }; + D53830571BFA1BE600F43D58 /* StorageAware.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageAware.swift; sourceTree = ""; }; + D53830581BFA1BE600F43D58 /* StorageFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageFactory.swift; sourceTree = ""; }; + D53830591BFA1BE600F43D58 /* DiskStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskStorage.swift; sourceTree = ""; }; + D538305A1BFA1BE600F43D58 /* MemoryStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryStorage.swift; sourceTree = ""; }; + D53830621BFA245900F43D58 /* Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cache.swift; sourceTree = ""; }; + D53830651BFA29E100F43D58 /* DiskStorageSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskStorageSpec.swift; sourceTree = ""; }; + D53830661BFA29E100F43D58 /* MemoryStorageSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryStorageSpec.swift; sourceTree = ""; }; + D53830671BFA29E100F43D58 /* StorageFactorySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageFactorySpec.swift; sourceTree = ""; }; + D5684E851BFA33AA00488B96 /* HybridCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HybridCache.swift; sourceTree = ""; }; D5704C521BC84DAA0089F388 /* String+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Cache.swift"; sourceTree = ""; }; D5CD0F721BCAE5F6004C0B32 /* SpecHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpecHelper.swift; sourceTree = ""; }; D5EA236A1BD5C21200FEABC3 /* String+CacheSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+CacheSpec.swift"; sourceTree = ""; }; @@ -142,12 +144,12 @@ 14C1365B1AB784BC00B7B07A /* Source */ = { isa = PBXGroup; children = ( + D53830611BFA1CC900F43D58 /* Cache */, + D53830561BFA1BE600F43D58 /* Storage */, D52199EF1BF51CD7009D44DF /* DataStructures */, - D5219A121BF6230F009D44DF /* Cache */, D5704C511BC84D990089F388 /* Extensions */, D52199FD1BF53F98009D44DF /* Library */, D5219A221BF625CA009D44DF /* Config.swift */, - D5219A101BF60C09009D44DF /* Cache.swift */, ); name = Source; path = ../Source; @@ -174,7 +176,7 @@ isa = PBXGroup; children = ( D52199F21BF51D03009D44DF /* Cachable.swift */, - D5219A281BF66BD9009D44DF /* CacheKind.swift */, + D5219A281BF66BD9009D44DF /* StorageKind.swift */, D52199F01BF51D03009D44DF /* Capsule.swift */, D52199F11BF51D03009D44DF /* Expiry.swift */, ); @@ -193,7 +195,7 @@ D5219A041BF55A95009D44DF /* DataStructures */ = { isa = PBXGroup; children = ( - D5219A2A1BF66C0A009D44DF /* CacheKindSpec.swift */, + D5219A2A1BF66C0A009D44DF /* StorageKindSpec.swift */, D5219A051BF55AAB009D44DF /* CapsuleSpec.swift */, D5219A071BF55BD7009D44DF /* ExpirySpec.swift */, ); @@ -209,27 +211,36 @@ path = Library; sourceTree = ""; }; - D5219A121BF6230F009D44DF /* Cache */ = { + D53830561BFA1BE600F43D58 /* Storage */ = { isa = PBXGroup; children = ( - D5219A171BF6234D009D44DF /* CacheAware.swift */, - D5219A131BF62329009D44DF /* DiskCache.swift */, - D5219A141BF62329009D44DF /* MemoryCache.swift */, - D5219A241BF62AA8009D44DF /* CacheFactory.swift */, + D53830571BFA1BE600F43D58 /* StorageAware.swift */, + D53830581BFA1BE600F43D58 /* StorageFactory.swift */, + D53830591BFA1BE600F43D58 /* DiskStorage.swift */, + D538305A1BFA1BE600F43D58 /* MemoryStorage.swift */, ); - path = Cache; + path = Storage; sourceTree = ""; }; - D5219A1B1BF6239A009D44DF /* Cache */ = { + D53830611BFA1CC900F43D58 /* Cache */ = { isa = PBXGroup; children = ( - D5219A1C1BF623F7009D44DF /* DiskCacheSpec.swift */, - D5219A1D1BF623F7009D44DF /* MemoryCacheSpec.swift */, - D5219A261BF6663D009D44DF /* CacheFactorySpec.swift */, + D53830621BFA245900F43D58 /* Cache.swift */, + D5684E851BFA33AA00488B96 /* HybridCache.swift */, ); path = Cache; sourceTree = ""; }; + D53830641BFA29E100F43D58 /* Storage */ = { + isa = PBXGroup; + children = ( + D53830651BFA29E100F43D58 /* DiskStorageSpec.swift */, + D53830661BFA29E100F43D58 /* MemoryStorageSpec.swift */, + D53830671BFA29E100F43D58 /* StorageFactorySpec.swift */, + ); + path = Storage; + sourceTree = ""; + }; D5704C511BC84D990089F388 /* Extensions */ = { isa = PBXGroup; children = ( @@ -250,8 +261,8 @@ D5EA23641BD5BDE700FEABC3 /* Specs */ = { isa = PBXGroup; children = ( + D53830641BFA29E100F43D58 /* Storage */, D5219A041BF55A95009D44DF /* DataStructures */, - D5219A1B1BF6239A009D44DF /* Cache */, D5EA23691BD5C1E600FEABC3 /* Extensions */, D5219A091BF55E4F009D44DF /* Library */, D5219A2C1BF66CDC009D44DF /* ConfigSpec.swift */, @@ -389,32 +400,33 @@ buildActionMask = 2147483647; files = ( D5219A081BF55BD7009D44DF /* ExpirySpec.swift in Sources */, - D5219A1F1BF623F7009D44DF /* MemoryCacheSpec.swift in Sources */, + D53830691BFA29E100F43D58 /* MemoryStorageSpec.swift in Sources */, + D5684E861BFA33AA00488B96 /* HybridCache.swift in Sources */, D52199F51BF51D03009D44DF /* Cachable.swift in Sources */, D5EA236B1BD5C21200FEABC3 /* String+CacheSpec.swift in Sources */, D5219A211BF6240D009D44DF /* CacheTaskSpec.swift in Sources */, - D5219A181BF6234D009D44DF /* CacheAware.swift in Sources */, + D538305D1BFA1BE600F43D58 /* StorageFactory.swift in Sources */, D5219A1A1BF62371009D44DF /* CacheTask.swift in Sources */, + D538305E1BFA1BE600F43D58 /* DiskStorage.swift in Sources */, D52199FF1BF53FA8009D44DF /* DefaultCacheConverter.swift in Sources */, - D5219A111BF60C09009D44DF /* Cache.swift in Sources */, D5704C531BC84DAA0089F388 /* String+Cache.swift in Sources */, D5219A061BF55AAB009D44DF /* CapsuleSpec.swift in Sources */, - D5219A151BF62329009D44DF /* DiskCache.swift in Sources */, - D5219A291BF66BD9009D44DF /* CacheKind.swift in Sources */, + D5219A291BF66BD9009D44DF /* StorageKind.swift in Sources */, D52199F41BF51D03009D44DF /* Expiry.swift in Sources */, - D5219A251BF62AA8009D44DF /* CacheFactory.swift in Sources */, D5219A031BF557DC009D44DF /* NSDate+CacheSpec.swift in Sources */, D5219A2F1BF66E8E009D44DF /* CacheSpec.swift in Sources */, D5219A011BF5502E009D44DF /* NSDate+Cache.swift in Sources */, + D538305C1BFA1BE600F43D58 /* StorageAware.swift in Sources */, + D538305F1BFA1BE600F43D58 /* MemoryStorage.swift in Sources */, D52199F31BF51D03009D44DF /* Capsule.swift in Sources */, - D5219A271BF6663D009D44DF /* CacheFactorySpec.swift in Sources */, - D5219A1E1BF623F7009D44DF /* DiskCacheSpec.swift in Sources */, + D538306A1BFA29E100F43D58 /* StorageFactorySpec.swift in Sources */, D5219A2D1BF66CDC009D44DF /* ConfigSpec.swift in Sources */, D5219A231BF625CA009D44DF /* Config.swift in Sources */, - D5219A161BF62329009D44DF /* MemoryCache.swift in Sources */, - D5219A2B1BF66C0A009D44DF /* CacheKindSpec.swift in Sources */, + D5219A2B1BF66C0A009D44DF /* StorageKindSpec.swift in Sources */, D5219A0B1BF55E63009D44DF /* DefaultCacheConverterSpec.swift in Sources */, D5CD0F731BCAE5F6004C0B32 /* SpecHelper.swift in Sources */, + D53830631BFA245900F43D58 /* Cache.swift in Sources */, + D53830681BFA29E100F43D58 /* DiskStorageSpec.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Pod/Tests/Specs/Cache/CacheFactorySpec.swift b/Pod/Tests/Specs/Cache/CacheFactorySpec.swift deleted file mode 100644 index 13cd81e2..00000000 --- a/Pod/Tests/Specs/Cache/CacheFactorySpec.swift +++ /dev/null @@ -1,67 +0,0 @@ -import Quick -import Nimble - -class CacheFactorySpec: QuickSpec { - - override func spec() { - describe("CacheFactory") { - - describe(".register") { - it("adds an item to the list of registered caches") { - CacheFactory.register(.Memory, cache: MemoryCache.self) - let resolvedCache = CacheFactory.resolve("Test", kind: .Memory) - - expect(resolvedCache is MemoryCache).to(beTrue()) - } - - it("overrides an item in the list of registered caches") { - CacheFactory.register(.Disk, cache: MemoryCache.self) - let resolvedCache = CacheFactory.resolve("Test", kind: .Disk) - - expect(resolvedCache is MemoryCache).to(beTrue()) - } - } - - describe(".resolve") { - it("returns previously registerd cache") { - let kind: CacheKind = .Custom("Cloud") - CacheFactory.register(kind, cache: DiskCache.self) - let resolvedCache = CacheFactory.resolve("Test", kind: kind) - - expect(resolvedCache is DiskCache).to(beTrue()) - } - - it("returns default registered caches") { - CacheFactory.reset() - let memoryCache = CacheFactory.resolve("Test", kind: .Memory) - let diskCache = CacheFactory.resolve("Test", kind: .Disk, maxSize: 0) - - expect(memoryCache is MemoryCache).to(beTrue()) - expect(diskCache is DiskCache).to(beTrue()) - } - - it("returns memory cache for unresolved kind") { - CacheFactory.reset() - let resolvedCache = CacheFactory.resolve("Test", kind: .Custom("Weirdo")) - - expect(resolvedCache is MemoryCache).to(beTrue()) - } - - it("returns a cache with specified maxSize") { - let resolvedCache = CacheFactory.resolve("Test", kind: .Memory, maxSize: 1000) - expect(resolvedCache.maxSize).to(equal(1000)) - } - } - - describe(".reset") { - it("resets to defaults") { - CacheFactory.register(.Disk, cache: MemoryCache.self) - CacheFactory.reset() - let resolvedCache = CacheFactory.resolve("Test", kind: .Disk) - - expect(resolvedCache is DiskCache).to(beTrue()) - } - } - } - } -} diff --git a/Pod/Tests/Specs/CacheSpec.swift b/Pod/Tests/Specs/CacheSpec.swift index 433d7224..9740dc4c 100644 --- a/Pod/Tests/Specs/CacheSpec.swift +++ b/Pod/Tests/Specs/CacheSpec.swift @@ -28,15 +28,14 @@ class CacheSpec: QuickSpec { let defaultConfig = Config.defaultConfig expect(cache.config.frontKind.name).to(equal(defaultConfig.frontKind.name)) - expect(cache.config.backKind!.name).to(equal(defaultConfig.backKind!.name)) + expect(cache.config.backKind.name).to(equal(defaultConfig.backKind.name)) expect(cache.config.expiry.date).to(equal(defaultConfig.expiry.date)) expect(cache.config.maxSize).to(equal(defaultConfig.maxSize)) } it("sets the front cache as a memory cache") { - expect(cache.frontCache.self is MemoryCache).to(beTrue()) - expect(cache.backCache).toNot(beNil()) - expect(cache.backCache!.self is DiskCache).to(beTrue()) + expect(cache.frontStorage.self is MemoryStorage).to(beTrue()) + expect(cache.backStorage.self is DiskStorage).to(beTrue()) } } @@ -52,12 +51,12 @@ class CacheSpec: QuickSpec { expectation1.fulfill() } - cache.frontCache.object(key) { (receivedObject: User?) in + cache.frontStorage.object(key) { (receivedObject: User?) in expect(receivedObject).toNot(beNil()) expectation2.fulfill() } - cache.backCache?.object(key) { (receivedObject: User?) in + cache.backStorage.object(key) { (receivedObject: User?) in expect(receivedObject).toNot(beNil()) expectation3.fulfill() } @@ -97,12 +96,12 @@ class CacheSpec: QuickSpec { expectation1.fulfill() } - cache.frontCache.object(key) { (receivedObject: User?) in + cache.frontStorage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation2.fulfill() } - cache.backCache?.object(key) { (receivedObject: User?) in + cache.backStorage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation3.fulfill() } @@ -126,12 +125,12 @@ class CacheSpec: QuickSpec { expectation1.fulfill() } - cache.frontCache.object(key) { (receivedObject: User?) in + cache.frontStorage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation2.fulfill() } - cache.backCache?.object(key) { (receivedObject: User?) in + cache.backStorage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation3.fulfill() } diff --git a/Pod/Tests/Specs/ConfigSpec.swift b/Pod/Tests/Specs/ConfigSpec.swift index 7749ed91..534da812 100644 --- a/Pod/Tests/Specs/ConfigSpec.swift +++ b/Pod/Tests/Specs/ConfigSpec.swift @@ -10,8 +10,8 @@ class ConfigSpec: QuickSpec { it("returns the correct default config") { let config = Config.defaultConfig - expect(config.frontKind.name).to(equal(CacheKind.Memory.name)) - expect(config.backKind!.name).to(equal(CacheKind.Disk.name)) + expect(config.frontKind.name).to(equal(StorageKind.Memory.name)) + expect(config.backKind.name).to(equal(StorageKind.Disk.name)) expect(config.expiry.date).to(equal(Expiry.Never.date)) expect(config.maxSize).to(equal(0)) } diff --git a/Pod/Tests/Specs/DataStructures/CacheKindSpec.swift b/Pod/Tests/Specs/DataStructures/CacheKindSpec.swift deleted file mode 100644 index 64416e62..00000000 --- a/Pod/Tests/Specs/DataStructures/CacheKindSpec.swift +++ /dev/null @@ -1,21 +0,0 @@ -import Quick -import Nimble - -class CacheKindSpec: QuickSpec { - - override func spec() { - describe("CacheKind") { - - describe("#name") { - it("returns the correct name for default values") { - expect(CacheKind.Memory.name).to(equal("Memory")) - expect(CacheKind.Disk.name).to(equal("Disk")) - } - - it("returns the correct name for custom values") { - expect(CacheKind.Custom("Weirdo").name).to(equal("Weirdo")) - } - } - } - } -} diff --git a/Pod/Tests/Specs/DataStructures/StorageKindSpec.swift b/Pod/Tests/Specs/DataStructures/StorageKindSpec.swift new file mode 100644 index 00000000..54bc9587 --- /dev/null +++ b/Pod/Tests/Specs/DataStructures/StorageKindSpec.swift @@ -0,0 +1,21 @@ +import Quick +import Nimble + +class StorageKindSpec: QuickSpec { + + override func spec() { + describe("StorageKind") { + + describe("#name") { + it("returns the correct name for default values") { + expect(StorageKind.Memory.name).to(equal("Memory")) + expect(StorageKind.Disk.name).to(equal("Disk")) + } + + it("returns the correct name for custom values") { + expect(StorageKind.Custom("Weirdo").name).to(equal("Weirdo")) + } + } + } + } +} diff --git a/Pod/Tests/Specs/Cache/DiskCacheSpec.swift b/Pod/Tests/Specs/Storage/DiskStorageSpec.swift similarity index 68% rename from Pod/Tests/Specs/Cache/DiskCacheSpec.swift rename to Pod/Tests/Specs/Storage/DiskStorageSpec.swift index 054fed20..03bd8d4b 100644 --- a/Pod/Tests/Specs/Cache/DiskCacheSpec.swift +++ b/Pod/Tests/Specs/Storage/DiskStorageSpec.swift @@ -1,23 +1,23 @@ import Quick import Nimble -class DiskCacheSpec: QuickSpec { +class DiskStorageSpec: QuickSpec { override func spec() { - describe("DiskCache") { - let name = "DudeDiskCache" + describe("DiskStorage") { + let name = "Floppy" let key = "youknownothing" let object = User(firstName: "John", lastName: "Snow") - var cache: DiskCache! + var storage: DiskStorage! let fileManager = NSFileManager() beforeEach { - cache = DiskCache(name: name) + storage = DiskStorage(name: name) } afterEach { do { - try fileManager.removeItemAtPath(cache.path) + try fileManager.removeItemAtPath(storage.path) } catch {} } @@ -25,15 +25,15 @@ class DiskCacheSpec: QuickSpec { it("returns the correct path") { let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true) - let path = "\(paths.first!)/\(DiskCache.prefix).\(name.capitalizedString)" + let path = "\(paths.first!)/\(DiskStorage.prefix).\(name.capitalizedString)" - expect(cache.path).to(equal(path)) + expect(storage.path).to(equal(path)) } } describe("#maxSize") { it("returns the default maximum size of a cache") { - expect(cache.maxSize).to(equal(0)) + expect(storage.maxSize).to(equal(0)) } } @@ -42,8 +42,8 @@ class DiskCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Create Cache Directory Expectation") - cache.add(key, object: object) { - let fileExist = fileManager.fileExistsAtPath(cache.path) + storage.add(key, object: object) { + let fileExist = fileManager.fileExistsAtPath(storage.path) expect(fileExist).to(beTrue()) expectation.fulfill() } @@ -54,8 +54,8 @@ class DiskCacheSpec: QuickSpec { it("saves an object") { let expectation = self.expectationWithDescription("Save Expectation") - cache.add(key, object: object) { - let fileExist = fileManager.fileExistsAtPath(cache.filePath(key)) + storage.add(key, object: object) { + let fileExist = fileManager.fileExistsAtPath(storage.filePath(key)) expect(fileExist).to(beTrue()) expectation.fulfill() } @@ -68,8 +68,8 @@ class DiskCacheSpec: QuickSpec { it("resolves cached object") { let expectation = self.expectationWithDescription("Object Expectation") - cache.add(key, object: object) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject?.firstName).to(equal(object.firstName)) expect(receivedObject?.lastName).to(equal(object.lastName)) expectation.fulfill() @@ -84,9 +84,9 @@ class DiskCacheSpec: QuickSpec { it("removes cached object") { let expectation = self.expectationWithDescription("Remove Expectation") - cache.add(key, object: object) - cache.remove(key) { - let fileExist = fileManager.fileExistsAtPath(cache.filePath(key)) + storage.add(key, object: object) + storage.remove(key) { + let fileExist = fileManager.fileExistsAtPath(storage.filePath(key)) expect(fileExist).to(beFalse()) expectation.fulfill() } @@ -101,9 +101,9 @@ class DiskCacheSpec: QuickSpec { "Remove If Expired Expectation") let expiry: Expiry = .Date(NSDate().dateByAddingTimeInterval(-100000)) - cache.add(key, object: object, expiry: expiry) - cache.removeIfExpired(key) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object, expiry: expiry) + storage.removeIfExpired(key) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation.fulfill() } @@ -116,9 +116,9 @@ class DiskCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Don't Remove If Not Expired Expectation") - cache.add(key, object: object) - cache.removeIfExpired(key) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) + storage.removeIfExpired(key) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).notTo(beNil()) expectation.fulfill() } @@ -132,9 +132,9 @@ class DiskCacheSpec: QuickSpec { it("clears cache directory") { let expectation = self.expectationWithDescription("Clear Expectation") - cache.add(key, object: object) - cache.clear() { - let fileExist = fileManager.fileExistsAtPath(cache.path) + storage.add(key, object: object) + storage.clear() { + let fileExist = fileManager.fileExistsAtPath(storage.path) expect(fileExist).to(beFalse()) expectation.fulfill() } @@ -145,14 +145,14 @@ class DiskCacheSpec: QuickSpec { describe("#fileName") { it("returns a correct file name") { - expect(cache.fileName(key)).to(equal(key.base64())) + expect(storage.fileName(key)).to(equal(key.base64())) } } describe("#filePath") { it("returns a correct file path") { - let filePath = "\(cache.path)/\(cache.fileName(key))" - expect(cache.filePath(key)).to(equal(filePath)) + let filePath = "\(storage.path)/\(storage.fileName(key))" + expect(storage.filePath(key)).to(equal(filePath)) } } } diff --git a/Pod/Tests/Specs/Cache/MemoryCacheSpec.swift b/Pod/Tests/Specs/Storage/MemoryStorageSpec.swift similarity index 70% rename from Pod/Tests/Specs/Cache/MemoryCacheSpec.swift rename to Pod/Tests/Specs/Storage/MemoryStorageSpec.swift index ae6c5aa0..4e84ea95 100644 --- a/Pod/Tests/Specs/Cache/MemoryCacheSpec.swift +++ b/Pod/Tests/Specs/Storage/MemoryStorageSpec.swift @@ -1,34 +1,34 @@ import Quick import Nimble -class MemoryCacheSpec: QuickSpec { +class MemoryStorageSpec: QuickSpec { override func spec() { - describe("MemoryCache") { - let name = "DudeMemoryCache" + describe("MemoryStorage") { + let name = "Brain" let key = "youknownothing" let object = User(firstName: "John", lastName: "Snow") - var cache: MemoryCache! + var storage: MemoryStorage! beforeEach { - cache = MemoryCache(name: name) + storage = MemoryStorage(name: name) } afterEach { - cache.clear() + storage.clear() } describe("#path") { it("returns the correct path") { - let path = "\(MemoryCache.prefix).\(name.capitalizedString)" + let path = "\(MemoryStorage.prefix).\(name.capitalizedString)" - expect(cache.path).to(equal(path)) + expect(storage.path).to(equal(path)) } } describe("#maxSize") { it("returns the default maximum size of a cache") { - expect(cache.maxSize).to(equal(0)) + expect(storage.maxSize).to(equal(0)) } } @@ -37,8 +37,8 @@ class MemoryCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Save Object Expectation") - cache.add(key, object: object) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).toNot(beNil()) expectation.fulfill() } @@ -53,8 +53,8 @@ class MemoryCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Object Expectation") - cache.add(key, object: object) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject?.firstName).to(equal(object.firstName)) expect(receivedObject?.lastName).to(equal(object.lastName)) expectation.fulfill() @@ -70,9 +70,9 @@ class MemoryCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Remove Expectation") - cache.add(key, object: object) - cache.remove(key) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) + storage.remove(key) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation.fulfill() } @@ -88,9 +88,9 @@ class MemoryCacheSpec: QuickSpec { "Remove If Expired Expectation") let expiry: Expiry = .Date(NSDate().dateByAddingTimeInterval(-100000)) - cache.add(key, object: object, expiry: expiry) - cache.removeIfExpired(key) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object, expiry: expiry) + storage.removeIfExpired(key) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation.fulfill() } @@ -103,9 +103,9 @@ class MemoryCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Don't Remove If Not Expired Expectation") - cache.add(key, object: object) - cache.removeIfExpired(key) { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) + storage.removeIfExpired(key) { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).notTo(beNil()) expectation.fulfill() } @@ -120,9 +120,9 @@ class MemoryCacheSpec: QuickSpec { let expectation = self.expectationWithDescription( "Clear Expectation") - cache.add(key, object: object) - cache.clear() { - cache.object(key) { (receivedObject: User?) in + storage.add(key, object: object) + storage.clear() { + storage.object(key) { (receivedObject: User?) in expect(receivedObject).to(beNil()) expectation.fulfill() } diff --git a/Pod/Tests/Specs/Storage/StorageFactorySpec.swift b/Pod/Tests/Specs/Storage/StorageFactorySpec.swift new file mode 100644 index 00000000..33298b40 --- /dev/null +++ b/Pod/Tests/Specs/Storage/StorageFactorySpec.swift @@ -0,0 +1,67 @@ +import Quick +import Nimble + +class StorageFactorySpec: QuickSpec { + + override func spec() { + describe("StorageFactory") { + + describe(".register") { + it("adds an item to the list of registered storages") { + StorageFactory.register(.Memory, storage: MemoryStorage.self) + let resolvedStorage = StorageFactory.resolve("Test", kind: .Memory) + + expect(resolvedStorage is MemoryStorage).to(beTrue()) + } + + it("overrides an item in the list of registered storages") { + StorageFactory.register(.Disk, storage: MemoryStorage.self) + let resolvedStorage = StorageFactory.resolve("Test", kind: .Disk) + + expect(resolvedStorage is MemoryStorage).to(beTrue()) + } + } + + describe(".resolve") { + it("returns previously registerd storage") { + let kind: StorageKind = .Custom("Cloud") + StorageFactory.register(kind, storage: DiskStorage.self) + let resolvedStorage = StorageFactory.resolve("Test", kind: kind) + + expect(resolvedStorage is DiskStorage).to(beTrue()) + } + + it("returns default registered storages") { + StorageFactory.reset() + let memoryStorage = StorageFactory.resolve("Test", kind: .Memory) + let diskStorage = StorageFactory.resolve("Test", kind: .Disk, maxSize: 0) + + expect(memoryStorage is MemoryStorage).to(beTrue()) + expect(diskStorage is DiskStorage).to(beTrue()) + } + + it("returns memory storage for unresolved kind") { + StorageFactory.reset() + let resolvedStorage = StorageFactory.resolve("Test", kind: .Custom("Weirdo")) + + expect(resolvedStorage is MemoryStorage).to(beTrue()) + } + + it("returns a storage with specified maxSize") { + let resolvedStorage = StorageFactory.resolve("Test", kind: .Memory, maxSize: 1000) + expect(resolvedStorage.maxSize).to(equal(1000)) + } + } + + describe(".reset") { + it("resets to defaults") { + StorageFactory.register(.Disk, storage: MemoryStorage.self) + StorageFactory.reset() + let resolvedStorage = StorageFactory.resolve("Test", kind: .Disk) + + expect(resolvedStorage is DiskStorage).to(beTrue()) + } + } + } + } +} diff --git a/Source/Cache.swift b/Source/Cache.swift deleted file mode 100644 index f0a22958..00000000 --- a/Source/Cache.swift +++ /dev/null @@ -1,83 +0,0 @@ -import Foundation - -public class Cache { - - public let name: String - - let config: Config - let frontCache: CacheAware - var backCache: CacheAware? - - // MARK: - Inititalization - - public init(name: String, config: Config = Config.defaultConfig) { - self.name = name - self.config = config - - frontCache = CacheFactory.resolve(name, kind: config.frontKind, maxSize: config.maxSize) - if let backKind = config.backKind { - backCache = CacheFactory.resolve(name, kind: backKind, maxSize: config.maxSize) - } - } - - // MARK: - Caching - - func add(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { - let expiry = expiry ?? config.expiry - - frontCache.add(key, object: object, expiry: expiry) { [weak self] in - guard let weakSelf = self, backCache = weakSelf.backCache else { - completion?() - return - } - - backCache.add(key, object: object, expiry: expiry) { - completion?() - } - } - } - - func object(key: String, completion: (object: T?) -> Void) { - frontCache.object(key) { [weak self] (object: T?) in - if let object = object { - completion(object: object) - return - } - - guard let weakSelf = self, backCache = weakSelf.backCache else { - completion(object: object) - return - } - - backCache.object(key) { (object: T?) in - completion(object: object) - } - } - } - - func remove(key: String, completion: (() -> Void)? = nil) { - frontCache.remove(key) { [weak self] in - guard let weakSelf = self, backCache = weakSelf.backCache else { - completion?() - return - } - - backCache.remove(key) { - completion?() - } - } - } - - func clear(completion: (() -> Void)? = nil) { - frontCache.clear() { [weak self] in - guard let weakSelf = self, backCache = weakSelf.backCache else { - completion?() - return - } - - backCache.clear() { - completion?() - } - } - } -} diff --git a/Source/Cache/Cache.swift b/Source/Cache/Cache.swift new file mode 100644 index 00000000..1fa57620 --- /dev/null +++ b/Source/Cache/Cache.swift @@ -0,0 +1,18 @@ +import Foundation + +public class Cache: HybridCache { + + public override init(name: String, config: Config = Config.defaultConfig) { + super.init(name: name, config: config) + } + + // MARK: - Caching + + public override func add(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { + super.add(key, object: object, expiry: expiry, completion: completion) + } + + public override func object(key: String, completion: (object: T?) -> Void) { + super.object(key, completion: completion) + } +} diff --git a/Source/Cache/CacheFactory.swift b/Source/Cache/CacheFactory.swift deleted file mode 100644 index dc869669..00000000 --- a/Source/Cache/CacheFactory.swift +++ /dev/null @@ -1,26 +0,0 @@ -public class CacheFactory { - - private static var DefaultCache: CacheAware.Type = MemoryCache.self - - private static var defaultCaches: [String: CacheAware.Type] = [ - CacheKind.Memory.name : MemoryCache.self, - CacheKind.Disk.name : DiskCache.self - ] - - private static var caches = defaultCaches - - // MARK: - Factory - - static func register(kind: CacheKind, cache: T.Type) { - caches[kind.name] = cache - } - - static func resolve(name: String, kind: CacheKind, maxSize: UInt = 0) -> CacheAware { - let Cache: CacheAware.Type = caches[kind.name] ?? DefaultCache - return Cache.init(name: name, maxSize: maxSize) - } - - static func reset() { - caches = defaultCaches - } -} \ No newline at end of file diff --git a/Source/Cache/HybridCache.swift b/Source/Cache/HybridCache.swift new file mode 100644 index 00000000..8bd1571d --- /dev/null +++ b/Source/Cache/HybridCache.swift @@ -0,0 +1,81 @@ +import Foundation + +public class HybridCache { + + public let name: String + + let config: Config + let frontStorage: StorageAware + var backStorage: StorageAware + + // MARK: - Inititalization + + public init(name: String, config: Config = Config.defaultConfig) { + self.name = name + self.config = config + + frontStorage = StorageFactory.resolve(name, kind: config.frontKind, maxSize: config.maxSize) + backStorage = StorageFactory.resolve(name, kind: config.backKind, maxSize: config.maxSize) + } + + // MARK: - Caching + + public func add(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) { + let expiry = expiry ?? config.expiry + + frontStorage.add(key, object: object, expiry: expiry) { [weak self] in + guard let weakSelf = self else { + completion?() + return + } + + weakSelf.backStorage.add(key, object: object, expiry: expiry) { + completion?() + } + } + } + + public func object(key: String, completion: (object: T?) -> Void) { + frontStorage.object(key) { [weak self] (object: T?) in + if let object = object { + completion(object: object) + return + } + + guard let weakSelf = self else { + completion(object: object) + return + } + + weakSelf.backStorage.object(key) { (object: T?) in + completion(object: object) + } + } + } + + public func remove(key: String, completion: (() -> Void)? = nil) { + frontStorage.remove(key) { [weak self] in + guard let weakSelf = self else { + completion?() + return + } + + weakSelf.backStorage.remove(key) { + completion?() + } + } + } + + public func clear(completion: (() -> Void)? = nil) { + frontStorage.clear() { [weak self] in + guard let weakSelf = self else { + completion?() + return + } + + weakSelf.backStorage.clear() { + completion?() + } + } + } +} diff --git a/Source/Config.swift b/Source/Config.swift index 0b45f1fa..01d59256 100644 --- a/Source/Config.swift +++ b/Source/Config.swift @@ -1,13 +1,13 @@ public struct Config { - public let frontKind: CacheKind - public let backKind: CacheKind? + public let frontKind: StorageKind + public let backKind: StorageKind public let expiry: Expiry public let maxSize: UInt // MARK: - Initialization - public init(frontKind: CacheKind, backKind: CacheKind?, expiry: Expiry, maxSize: UInt) { + public init(frontKind: StorageKind, backKind: StorageKind, expiry: Expiry, maxSize: UInt) { self.frontKind = frontKind self.backKind = backKind self.expiry = expiry diff --git a/Source/DataStructures/CacheKind.swift b/Source/DataStructures/StorageKind.swift similarity index 91% rename from Source/DataStructures/CacheKind.swift rename to Source/DataStructures/StorageKind.swift index 993acaa8..b48f099a 100644 --- a/Source/DataStructures/CacheKind.swift +++ b/Source/DataStructures/StorageKind.swift @@ -1,4 +1,4 @@ -public enum CacheKind { +public enum StorageKind { case Memory case Disk case Custom(String) diff --git a/Source/Cache/DiskCache.swift b/Source/Storage/DiskStorage.swift similarity index 92% rename from Source/Cache/DiskCache.swift rename to Source/Storage/DiskStorage.swift index d1cb716b..1ee50c21 100644 --- a/Source/Cache/DiskCache.swift +++ b/Source/Storage/DiskStorage.swift @@ -1,6 +1,6 @@ import Foundation -public class DiskCache: CacheAware { +public class DiskStorage: StorageAware { public static let prefix = "no.hyper.Cache.Disk" @@ -22,10 +22,10 @@ public class DiskCache: CacheAware { let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true) - path = "\(paths.first!)/\(DiskCache.prefix).\(cacheName)" - writeQueue = dispatch_queue_create("\(DiskCache.prefix).\(cacheName).WriteQueue", + path = "\(paths.first!)/\(DiskStorage.prefix).\(cacheName)" + writeQueue = dispatch_queue_create("\(DiskStorage.prefix).\(cacheName).WriteQueue", DISPATCH_QUEUE_SERIAL) - readQueue = dispatch_queue_create("\(DiskCache.prefix).\(cacheName).ReadQueue", + readQueue = dispatch_queue_create("\(DiskStorage.prefix).\(cacheName).ReadQueue", DISPATCH_QUEUE_SERIAL) } diff --git a/Source/Cache/MemoryCache.swift b/Source/Storage/MemoryStorage.swift similarity index 95% rename from Source/Cache/MemoryCache.swift rename to Source/Storage/MemoryStorage.swift index ddbe5c58..96090bf6 100644 --- a/Source/Cache/MemoryCache.swift +++ b/Source/Storage/MemoryStorage.swift @@ -1,6 +1,6 @@ import Foundation -public class MemoryCache: CacheAware { +public class MemoryStorage: StorageAware { public static let prefix = "no.hyper.Cache.Memory" @@ -22,7 +22,7 @@ public class MemoryCache: CacheAware { public required init(name: String, maxSize: UInt = 0) { self.maxSize = maxSize - cache.name = "\(MemoryCache.prefix).\(name.capitalizedString)" + cache.name = "\(MemoryStorage.prefix).\(name.capitalizedString)" writeQueue = dispatch_queue_create("\(cache.name).WriteQueue", DISPATCH_QUEUE_SERIAL) readQueue = dispatch_queue_create("\(cache.name).ReadQueue", DISPATCH_QUEUE_SERIAL) } @@ -107,4 +107,4 @@ public class MemoryCache: CacheAware { completion?() } } -} +} \ No newline at end of file diff --git a/Source/Cache/CacheAware.swift b/Source/Storage/StorageAware.swift similarity index 92% rename from Source/Cache/CacheAware.swift rename to Source/Storage/StorageAware.swift index fb1e5b27..b7368026 100644 --- a/Source/Cache/CacheAware.swift +++ b/Source/Storage/StorageAware.swift @@ -2,17 +2,20 @@ import Foundation public protocol CacheAware { + func add(key: String, object: T, expiry: Expiry, completion: (() -> Void)?) + func object(key: String, completion: (object: T?) -> Void) + func remove(key: String, completion: (() -> Void)?) + func removeIfExpired(key: String, completion: (() -> Void)?) + func clear(completion: (() -> Void)?) +} + +public protocol StorageAware: CacheAware { static var prefix: String { get } + var path: String { get } var maxSize: UInt { get set } var writeQueue: dispatch_queue_t { get } var readQueue: dispatch_queue_t { get } init(name: String, maxSize: UInt) - - func add(key: String, object: T, expiry: Expiry, completion: (() -> Void)?) - func object(key: String, completion: (object: T?) -> Void) - func remove(key: String, completion: (() -> Void)?) - func removeIfExpired(key: String, completion: (() -> Void)?) - func clear(completion: (() -> Void)?) } diff --git a/Source/Storage/StorageFactory.swift b/Source/Storage/StorageFactory.swift new file mode 100644 index 00000000..61e14f83 --- /dev/null +++ b/Source/Storage/StorageFactory.swift @@ -0,0 +1,26 @@ +public class StorageFactory { + + private static var DefaultStorage: StorageAware.Type = MemoryStorage.self + + private static var defaultStorages: [String: StorageAware.Type] = [ + StorageKind.Memory.name : MemoryStorage.self, + StorageKind.Disk.name : DiskStorage.self + ] + + private static var storages = defaultStorages + + // MARK: - Factory + + static func register(kind: StorageKind, storage: T.Type) { + storages[kind.name] = storage + } + + static func resolve(name: String, kind: StorageKind, maxSize: UInt = 0) -> StorageAware { + let StorageType: StorageAware.Type = storages[kind.name] ?? DefaultStorage + return StorageType.init(name: name, maxSize: maxSize) + } + + static func reset() { + storages = defaultStorages + } +} \ No newline at end of file