Skip to content

Commit

Permalink
Merge pull request #12 from hyperoslo/feature/cache
Browse files Browse the repository at this point in the history
Feature: cache aggregator
  • Loading branch information
zenangst committed Nov 14, 2015
2 parents 71971b7 + 544c924 commit 0faa84f
Show file tree
Hide file tree
Showing 17 changed files with 511 additions and 86 deletions.
114 changes: 79 additions & 35 deletions Pod/CacheTests.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

31 changes: 0 additions & 31 deletions Pod/Tests/Specs/CachableSpec.swift

This file was deleted.

67 changes: 67 additions & 0 deletions Pod/Tests/Specs/Cache/CacheFactorySpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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())
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class DiskCacheSpec: QuickSpec {
}

it("saves an object") {
let expectation = self.expectationWithDescription(
"Save Object Expectation")
let expectation = self.expectationWithDescription("Save Expectation")

cache.add(key, object: object) {
let fileExist = fileManager.fileExistsAtPath(cache.filePath(key))
Expand All @@ -67,8 +66,7 @@ class DiskCacheSpec: QuickSpec {

describe("#object") {
it("resolves cached object") {
let expectation = self.expectationWithDescription(
"Object Expectation")
let expectation = self.expectationWithDescription("Object Expectation")

cache.add(key, object: object) {
cache.object(key) { (receivedObject: User?) in
Expand All @@ -84,8 +82,7 @@ class DiskCacheSpec: QuickSpec {

describe("#remove") {
it("removes cached object") {
let expectation = self.expectationWithDescription(
"Remove Expectation")
let expectation = self.expectationWithDescription("Remove Expectation")

cache.add(key, object: object)
cache.remove(key) {
Expand Down Expand Up @@ -133,8 +130,7 @@ class DiskCacheSpec: QuickSpec {

describe("#clear") {
it("clears cache directory") {
let expectation = self.expectationWithDescription(
"Clear Expectation")
let expectation = self.expectationWithDescription("Clear Expectation")

cache.add(key, object: object)
cache.clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class MemoryCacheSpec: QuickSpec {
}
}

self.waitForExpectationsWithTimeout(2.0, handler:nil)
self.waitForExpectationsWithTimeout(4.0, handler:nil)
}

it("don't remove not expired object") {
Expand All @@ -111,7 +111,7 @@ class MemoryCacheSpec: QuickSpec {
}
}

self.waitForExpectationsWithTimeout(2.0, handler:nil)
self.waitForExpectationsWithTimeout(4.0, handler:nil)
}
}

Expand Down
145 changes: 145 additions & 0 deletions Pod/Tests/Specs/CacheSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import Quick
import Nimble

class CacheSpec: QuickSpec {

override func spec() {
describe("Cache") {
let name = "WeirdoCache"
let key = "alongweirdkey"
let object = User(firstName: "John", lastName: "Snow")
var cache: Cache<User>!

beforeEach {
cache = Cache<User>(name: name)
}

afterEach {
cache.clear()
}

describe("#init") {

it("sets a name") {
expect(cache.name).to(equal(name))
}

it("sets a config") {
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.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())
}
}

describe("#add") {
it("saves an object to memory and disk") {
let expectation1 = self.expectationWithDescription("Save Expectation")
let expectation2 = self.expectationWithDescription("Save To Memory Expectation")
let expectation3 = self.expectationWithDescription("Save To Disk Expectation")

cache.add(key, object: object) {
cache.object(key) { (receivedObject: User?) in
expect(receivedObject).toNot(beNil())
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
expect(receivedObject).toNot(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
expect(receivedObject).toNot(beNil())
expectation3.fulfill()
}
}

self.waitForExpectationsWithTimeout(8.0, handler:nil)
}
}

describe("#object") {
it("resolves cached object") {
let expectation = self.expectationWithDescription("Object Expectation")

cache.add(key, object: object) {
cache.object(key) { (receivedObject: User?) in
expect(receivedObject?.firstName).to(equal(object.firstName))
expect(receivedObject?.lastName).to(equal(object.lastName))
expectation.fulfill()
}
}

self.waitForExpectationsWithTimeout(4.0, handler:nil)
}
}

describe("#remove") {
it("removes cached object from memory and disk") {
let expectation1 = self.expectationWithDescription("Remove Expectation")
let expectation2 = self.expectationWithDescription("Remove From Memory Expectation")
let expectation3 = self.expectationWithDescription("Remove From Disk Expectation")

cache.add(key, object: object)

cache.remove(key) {
cache.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation3.fulfill()
}
}

self.waitForExpectationsWithTimeout(8.0, handler:nil)
}
}

describe("#clear") {
it("clears memory and disk cache") {
let expectation1 = self.expectationWithDescription("Clear Expectation")
let expectation2 = self.expectationWithDescription("Clear Memory Expectation")
let expectation3 = self.expectationWithDescription("Clear Disk Expectation")

cache.add(key, object: object)

cache.clear() {
cache.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation3.fulfill()
}
}

self.waitForExpectationsWithTimeout(8.0, handler:nil)
}
}
}
}
}
21 changes: 21 additions & 0 deletions Pod/Tests/Specs/ConfigSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Quick
import Nimble

class ConfigSpec: QuickSpec {

override func spec() {
describe("Config") {

describe(".defaultConfig") {
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.expiry.date).to(equal(Expiry.Never.date))
expect(config.maxSize).to(equal(0))
}
}
}
}
}
21 changes: 21 additions & 0 deletions Pod/Tests/Specs/DataStructures/CacheKindSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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"))
}
}
}
}
}
File renamed without changes.
Loading

0 comments on commit 0faa84f

Please sign in to comment.