@@ -35,6 +35,16 @@ public protocol PersistDriver: Service {
3535 /// - as: Type you want value to be returned as. If it cannot be returned as this value then nil will be returned
3636 func get< Object: Codable & Sendable > ( key: String , as: Object . Type ) async throws -> Object ?
3737
38+ /// get value and time to live for key
39+ ///
40+ /// There is a default version of this function which only returns the value. Persist drivers have to opt
41+ /// into providing the time to live. Both Valkey and Postgres drivers will return the time to live.
42+ ///
43+ /// - Parameters:
44+ /// - key: Key used to look for value
45+ /// - as: Type you want value to be returned as. If it cannot be returned as this value then nil will be returned
46+ func getWithTTL< Object: Codable & Sendable > ( key: String , as: Object . Type ) async throws -> ( object: Object , ttl: Duration ? ) ?
47+
3848 /// remove value associated with key
3949 /// - Parameters:
4050 /// - key: Key used to look for value
@@ -62,6 +72,15 @@ extension PersistDriver {
6272 try await self . set ( key: key, value: value, expires: nil )
6373 }
6474
75+ /// get value and time to live for key
76+ /// - Parameters:
77+ /// - key: Key used to look for value
78+ /// - type: Type you want value to be returned as. If it cannot be returned as this value then nil will be returned
79+ public func getWithTTL< Object: Codable & Sendable > ( key: String , as type: Object . Type ) async throws -> ( object: Object , ttl: Duration ? ) ? {
80+ let value = try await get ( key: key, as: type)
81+ return value. map { ( $0, nil ) }
82+ }
83+
6584 public func run( ) async throws {
6685 // ignore cancellation error as we need to shutdown
6786 try ? await gracefulShutdown ( )
0 commit comments