Merge pull request #4 from skelpo/develop

Storage Subscripts and Request.storage Method
This commit is contained in:
Caleb Kleveter 2018-05-09 16:25:09 -05:00 committed by GitHub
commit 8b78db172e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View File

@ -39,4 +39,12 @@ extension Request {
let storage = try self.privateContainer.make(Storage.self)
storage.cache[key] = value
}
/// Gets the `Storage` instance from the
/// request's private container.
///
/// - Returns: The request's private `Storage` instance.
public func storage()throws -> Storage {
return try self.privateContainer.make(Storage.self)
}
}

View File

@ -11,4 +11,54 @@ public class Storage: Service {
///
public init() {}
/// Allows accessing stored values with
/// `storage[key]` syntax.
///
/// - Parameter key: The key of the value to fetch.
///
/// - Returns: The value stored for the key passed in.
public subscript (_ key: String) -> Any? {
get {
return self.cache[key] ?? nil
}
set {
self.cache[key] = newValue
}
}
/// Allows accessing stored values with
/// `storage[key]` syntax as a specific type.
///
/// - Parameters:
/// - key: The key of the value to fetch.
/// - type: The type to convert the stored value to.
///
/// - Returns: The value stored for the key passed in.
public subscript <T>(_ key: String, as type: T.Type) -> T? {
get {
let value = self.cache[key] ?? nil
return value as? T
}
set {
self.cache[key] = newValue
}
}
/// Allows accessing stored values with
/// `storage[key]` syntax as a specific type.
///
/// - Parameters:
/// - key: The key of the value to fetch.
///
/// - Returns: The value stored for the key passed in.
public subscript <T>(_ key: String) -> T? {
get {
let value = self.cache[key] ?? nil
return value as? T
}
set {
self.cache[key] = newValue
}
}
}