Skip to content

Commit 8bf26fb

Browse files
committed
Rename RedisKeyLifetime to be nested in RedisKey
Motivation: `RedisKeyLifetime` already has "RedisKey" as a prefix so it naturally fits as a nested type. Modifications: - Change: `RedisKeyLifetime` to be nested in `RedisKey` and named `Lifetime` - Rename: `RedisKeyLifetime.Lifetime` to `Duration` - Deprecate: `RedisKeyLifetime` and the nested type `Lifetime` Result: The global namespace is a little less cluttered with the types falling naturally where they already are.
1 parent b2367ac commit 8bf26fb

File tree

4 files changed

+115
-93
lines changed

4 files changed

+115
-93
lines changed

Sources/RediStack/Commands/BasicCommands.swift

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -159,94 +159,23 @@ extension RedisClient {
159159
/// [https://redis.io/commands/ttl](https://redis.io/commands/ttl)
160160
/// - Parameter key: The key to check the time-to-live on.
161161
/// - Returns: The number of seconds before the given key will expire.
162-
public func ttl(_ key: RedisKey) -> EventLoopFuture<RedisKeyLifetime> {
162+
public func ttl(_ key: RedisKey) -> EventLoopFuture<RedisKey.Lifetime> {
163163
let args: [RESPValue] = [RESPValue(from: key)]
164164
return self.send(command: "TTL", with: args)
165165
.tryConverting(to: Int64.self)
166-
.map { RedisKeyLifetime(seconds: $0) }
166+
.map { .init(seconds: $0) }
167167
}
168168

169169
/// Returns the remaining time-to-live (in milliseconds) of the provided key.
170170
///
171171
/// [https://redis.io/commands/pttl](https://redis.io/commands/pttl)
172172
/// - Parameter key: The key to check the time-to-live on.
173173
/// - Returns: The number of milliseconds before the given key will expire.
174-
public func pttl(_ key: RedisKey) -> EventLoopFuture<RedisKeyLifetime> {
174+
public func pttl(_ key: RedisKey) -> EventLoopFuture<RedisKey.Lifetime> {
175175
let args: [RESPValue] = [RESPValue(from: key)]
176176
return self.send(command: "PTTL", with: args)
177177
.tryConverting(to: Int64.self)
178-
.map { RedisKeyLifetime(milliseconds: $0) }
179-
}
180-
}
181-
182-
183-
/// The lifetime of a `RedisKey` as determined by `ttl` or `pttl`.
184-
public enum RedisKeyLifetime: Hashable {
185-
/// The key does not exist.
186-
case keyDoesNotExist
187-
/// The key exists but has no expiry associated with it.
188-
case unlimited
189-
/// The key exists for the given lifetime.
190-
case limited(Lifetime)
191-
}
192-
193-
extension RedisKeyLifetime {
194-
/// The lifetime for a `RedisKey` which has an expiry set.
195-
public enum Lifetime: Comparable, Hashable {
196-
/// The remaining time-to-live in seconds.
197-
case seconds(Int64)
198-
/// The remaining time-to-live in milliseconds.
199-
case milliseconds(Int64)
200-
201-
/// The remaining time-to-live.
202-
public var timeAmount: TimeAmount {
203-
switch self {
204-
case .seconds(let amount): return .seconds(amount)
205-
case .milliseconds(let amount): return .milliseconds(amount)
206-
}
207-
}
208-
209-
public static func <(lhs: Lifetime, rhs: Lifetime) -> Bool {
210-
return lhs.timeAmount < rhs.timeAmount
211-
}
212-
213-
public static func ==(lhs: Lifetime, rhs: Lifetime) -> Bool {
214-
return lhs.timeAmount == rhs.timeAmount
215-
}
216-
}
217-
}
218-
219-
extension RedisKeyLifetime {
220-
/// The remaining time-to-live for the key, or `nil` if the key does not exist or will not expire.
221-
public var timeAmount: TimeAmount? {
222-
switch self {
223-
case .keyDoesNotExist, .unlimited: return nil
224-
case .limited(let lifetime): return lifetime.timeAmount
225-
}
226-
}
227-
}
228-
229-
extension RedisKeyLifetime {
230-
internal init(seconds: Int64) {
231-
switch seconds {
232-
case -2:
233-
self = .keyDoesNotExist
234-
case -1:
235-
self = .unlimited
236-
default:
237-
self = .limited(.seconds(seconds))
238-
}
239-
}
240-
241-
internal init(milliseconds: Int64) {
242-
switch milliseconds {
243-
case -2:
244-
self = .keyDoesNotExist
245-
case -1:
246-
self = .unlimited
247-
default:
248-
self = .limited(.milliseconds(milliseconds))
249-
}
178+
.map { .init(milliseconds: $0) }
250179
}
251180
}
252181

Sources/RediStack/RedisKey+TTL.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the RediStack open source project
4+
//
5+
// Copyright (c) 2020 RediStack project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of RediStack project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIO
16+
17+
extension RedisKey.Lifetime {
18+
/// The lifetime duration for a `RedisKey` which has an expiry set.
19+
public enum Duration: Comparable, Hashable {
20+
/// The remaining time-to-live in seconds.
21+
case seconds(Int64)
22+
/// The remaining time-to-live in milliseconds.
23+
case milliseconds(Int64)
24+
25+
/// The remaining time-to-live.
26+
public var timeAmount: TimeAmount {
27+
switch self {
28+
case .seconds(let amount): return .seconds(amount)
29+
case .milliseconds(let amount): return .milliseconds(amount)
30+
}
31+
}
32+
33+
public static func <(lhs: Duration, rhs: Duration) -> Bool {
34+
return lhs.timeAmount < rhs.timeAmount
35+
}
36+
37+
public static func ==(lhs: Duration, rhs: Duration) -> Bool {
38+
return lhs.timeAmount == rhs.timeAmount
39+
}
40+
}
41+
}
42+
43+
extension RedisKey {
44+
/// The lifetime of a `RedisKey` as determined by `ttl` or `pttl`.
45+
public enum Lifetime: Hashable {
46+
/// The key does not exist.
47+
case keyDoesNotExist
48+
/// The key exists but has no expiry associated with it.
49+
case unlimited
50+
/// The key exists for the given lifetime.
51+
case limited(Duration)
52+
53+
/// The remaining time-to-live for the key, or `nil` if the key does not exist or will not expire.
54+
public var timeAmount: TimeAmount? {
55+
switch self {
56+
case .keyDoesNotExist, .unlimited: return nil
57+
case .limited(let lifetime): return lifetime.timeAmount
58+
}
59+
}
60+
61+
internal init(seconds: Int64) {
62+
switch seconds {
63+
case -2:
64+
self = .keyDoesNotExist
65+
case -1:
66+
self = .unlimited
67+
default:
68+
self = .limited(.seconds(seconds))
69+
}
70+
}
71+
72+
internal init(milliseconds: Int64) {
73+
switch milliseconds {
74+
case -2:
75+
self = .keyDoesNotExist
76+
case -1:
77+
self = .unlimited
78+
default:
79+
self = .limited(.milliseconds(milliseconds))
80+
}
81+
}
82+
}
83+
}
84+

Sources/RediStack/_Deprecations.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,12 @@ extension RedisConnectionPool {
130130
)
131131
}
132132
}
133+
134+
// MARK: - RedisKeyLifetime
135+
@available(*, deprecated, message: "renamed to RedisKey.Lifetime")
136+
public typealias RedisKeyLifetime = RedisKey.Lifetime
137+
138+
extension RedisKey.Lifetime {
139+
@available(*, deprecated, message: "renamed to Duration")
140+
public typealias Lifetime = Duration
141+
}

Tests/RediStackTests/RedisKeyLifetime.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,36 @@ import XCTest
1717

1818
final class RedisKeyLifetimeTests: XCTestCase {
1919
func test_initFromSeconds() {
20-
XCTAssertEqual(RedisKeyLifetime(seconds: -2), .keyDoesNotExist)
21-
XCTAssertEqual(RedisKeyLifetime(seconds: -1), .unlimited)
22-
XCTAssertEqual(RedisKeyLifetime(seconds: 42), .limited(.seconds(42)))
20+
XCTAssertEqual(RedisKey.Lifetime(seconds: -2), .keyDoesNotExist)
21+
XCTAssertEqual(RedisKey.Lifetime(seconds: -1), .unlimited)
22+
XCTAssertEqual(RedisKey.Lifetime(seconds: 42), .limited(.seconds(42)))
2323
}
2424

2525
func test_initFromMilliseconds() {
26-
XCTAssertEqual(RedisKeyLifetime(milliseconds: -2), .keyDoesNotExist)
27-
XCTAssertEqual(RedisKeyLifetime(milliseconds: -1), .unlimited)
28-
XCTAssertEqual(RedisKeyLifetime(milliseconds: 42), .limited(.milliseconds(42)))
26+
XCTAssertEqual(RedisKey.Lifetime(milliseconds: -2), .keyDoesNotExist)
27+
XCTAssertEqual(RedisKey.Lifetime(milliseconds: -1), .unlimited)
28+
XCTAssertEqual(RedisKey.Lifetime(milliseconds: 42), .limited(.milliseconds(42)))
2929
}
3030

3131
func test_timeAmount() {
32-
XCTAssertNil(RedisKeyLifetime.keyDoesNotExist.timeAmount)
33-
XCTAssertNil(RedisKeyLifetime.unlimited.timeAmount)
32+
XCTAssertNil(RedisKey.Lifetime.keyDoesNotExist.timeAmount)
33+
XCTAssertNil(RedisKey.Lifetime.unlimited.timeAmount)
3434

35-
XCTAssertEqual(RedisKeyLifetime.limited(.seconds(42)).timeAmount, .seconds(42))
36-
XCTAssertEqual(RedisKeyLifetime.limited(.milliseconds(42)).timeAmount, .milliseconds(42))
35+
XCTAssertEqual(RedisKey.Lifetime.limited(.seconds(42)).timeAmount, .seconds(42))
36+
XCTAssertEqual(RedisKey.Lifetime.limited(.milliseconds(42)).timeAmount, .milliseconds(42))
3737
}
3838

3939
func test_lifetimeCompare() {
40-
XCTAssertLessThan(RedisKeyLifetime.Lifetime.seconds(42), .seconds(43))
41-
XCTAssertLessThan(RedisKeyLifetime.Lifetime.seconds(42), .milliseconds(42001))
42-
XCTAssertLessThan(RedisKeyLifetime.Lifetime.milliseconds(41999), .milliseconds(42000))
43-
XCTAssertLessThan(RedisKeyLifetime.Lifetime.milliseconds(41999), .seconds(42))
40+
XCTAssertLessThan(RedisKey.Lifetime.Duration.seconds(42), .seconds(43))
41+
XCTAssertLessThan(RedisKey.Lifetime.Duration.seconds(42), .milliseconds(42001))
42+
XCTAssertLessThan(RedisKey.Lifetime.Duration.milliseconds(41999), .milliseconds(42000))
43+
XCTAssertLessThan(RedisKey.Lifetime.Duration.milliseconds(41999), .seconds(42))
4444
}
4545

4646
func test_lifetimeEqual() {
47-
XCTAssertEqual(RedisKeyLifetime.Lifetime.seconds(42), .seconds(42))
48-
XCTAssertEqual(RedisKeyLifetime.Lifetime.seconds(42), .milliseconds(42000))
49-
XCTAssertEqual(RedisKeyLifetime.Lifetime.milliseconds(42000), .milliseconds(42000))
50-
XCTAssertEqual(RedisKeyLifetime.Lifetime.milliseconds(42000), .seconds(42))
47+
XCTAssertEqual(RedisKey.Lifetime.Duration.seconds(42), .seconds(42))
48+
XCTAssertEqual(RedisKey.Lifetime.Duration.seconds(42), .milliseconds(42000))
49+
XCTAssertEqual(RedisKey.Lifetime.Duration.milliseconds(42000), .milliseconds(42000))
50+
XCTAssertEqual(RedisKey.Lifetime.Duration.milliseconds(42000), .seconds(42))
5151
}
5252
}

0 commit comments

Comments
 (0)