@@ -18,9 +18,35 @@ import NIOCore
1818
1919/// In memory driver for persist system for storing persistent cross request key/value pairs
2020public actor MemoryPersistDriver < C: Clock > : PersistDriver where C. Duration == Duration {
21+ public struct Configuration : Sendable {
22+ /// amount of time between each call to tidy
23+ public var tidyFrequency : Duration
24+
25+ /// Initialize MemoryPersistDriver configuration
26+ /// - Parameter tidyFrequency:
27+ public init ( tidyFrequency: Duration = . seconds( 600 ) ) {
28+ self . tidyFrequency = tidyFrequency
29+ }
30+ }
31+
32+ /// Initialize MemoryPersistDriver
33+ /// - Parameters:
34+ /// - clock: Clock to use when calculating expiration dates
35+ @_disfavoredOverload
2136 public init ( _ clock: C = . continuous) {
2237 self . values = [ : ]
2338 self . clock = clock
39+ self . configuration = . init( )
40+ }
41+
42+ /// Initialize MemoryPersistDriver
43+ /// - Parameters:
44+ /// - clock: Clock to use when calculating expiration dates
45+ /// - configuration: Configuration of driver
46+ public init ( _ clock: C = . continuous, configuration: Configuration = . init( ) ) {
47+ self . values = [ : ]
48+ self . clock = clock
49+ self . configuration = configuration
2450 }
2551
2652 public func create( key: String , value: some Codable & Sendable , expires: Duration ? ) async throws {
@@ -51,7 +77,7 @@ public actor MemoryPersistDriver<C: Clock>: PersistDriver where C.Duration == Du
5177 let now = self . clock. now
5278 self . values = self . values. compactMapValues {
5379 if let expires = $0. expires {
54- if expires > now {
80+ if expires < now {
5581 return nil
5682 }
5783 }
@@ -72,7 +98,7 @@ public actor MemoryPersistDriver<C: Clock>: PersistDriver where C.Duration == Du
7298 }
7399
74100 public func run( ) async throws {
75- let timerSequence = AsyncTimerSequence ( interval: . seconds ( 600 ) , clock: . suspending )
101+ let timerSequence = AsyncTimerSequence ( interval: self . configuration . tidyFrequency , clock: self . clock )
76102 . cancelOnGracefulShutdown ( )
77103 for try await _ in timerSequence {
78104 self . tidy ( )
@@ -81,4 +107,5 @@ public actor MemoryPersistDriver<C: Clock>: PersistDriver where C.Duration == Du
81107
82108 var values : [ String : Item ]
83109 let clock : C
110+ let configuration : Configuration
84111}
0 commit comments