Skip to content

Commit

Permalink
Add subscript to MemoryCache
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed May 16, 2016
1 parent e449240 commit 5828a2b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/MemoryCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ public final class MemoryCache<T>: Cache {
cache.removeAllObjects()
completion?()
}


// MARK: - Synchronous

public subscript(key: String) -> T? {
get {
return (cache.objectForKey(key) as? Box<T>)?.value
}

set(newValue) {
if let newValue = newValue {
cache.setObject(Box(newValue), forKey: key)
} else {
cache.removeObjectForKey(key)
}
}
}
}

0 comments on commit 5828a2b

Please sign in to comment.