Skip to content

Commit bc960cc

Browse files
iwecon0xTim
andauthored
add concurrency function for expire, exists, ttl, pttl (#222)
Co-authored-by: Tim Condon <[email protected]>
1 parent d822288 commit bc960cc

File tree

2 files changed

+74
-24
lines changed

2 files changed

+74
-24
lines changed

Sources/Redis/Redis+Concurrency.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,6 @@ extension Application.Redis {
5454
}
5555
}
5656

57-
extension RedisClient {
58-
/// Gets the provided key as a decodable type.
59-
public func get<D>(_ key: RedisKey, asJSON type: D.Type, jsonDecoder: JSONDecoder = JSONDecoder()) async throws -> D?
60-
where D: Decodable
61-
{
62-
let data = try await self.get(key, as: Data.self).get()
63-
return try data.flatMap { try jsonDecoder.decode(D.self, from: $0) }
64-
}
65-
66-
/// Sets key to an encodable item.
67-
public func set<E>(_ key: RedisKey, toJSON entity: E, jsonEncoder: JSONEncoder = JSONEncoder()) async throws
68-
where E: Encodable
69-
{
70-
try await self.set(key, to: jsonEncoder.encode(entity)).get()
71-
}
72-
73-
/// Sets key to an encodable item with an expiration time.
74-
public func setex<E>(_ key: RedisKey, toJSON entity: E, expirationInSeconds expiration: Int, jsonEncoder: JSONEncoder = JSONEncoder()) async throws
75-
where E: Encodable
76-
{
77-
try await self.setex(key, to: jsonEncoder.encode(entity), expirationInSeconds: expiration).get()
78-
}
79-
}
80-
8157
extension Request.Redis {
8258
public func send(command: String, with arguments: [RESPValue]) async throws -> RESPValue {
8359
try await self.request.application.redis(self.id)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import RediStack
2+
import Foundation
3+
4+
extension RedisClient {
5+
/// Gets the provided key as a decodable type.
6+
public func get<D>(_ key: RedisKey, asJSON type: D.Type, jsonDecoder: JSONDecoder = JSONDecoder()) async throws -> D?
7+
where D: Decodable
8+
{
9+
let data = try await self.get(key, as: Data.self).get()
10+
return try data.flatMap { try jsonDecoder.decode(D.self, from: $0) }
11+
}
12+
13+
/// Sets key to an encodable item.
14+
public func set<E>(_ key: RedisKey, toJSON entity: E, jsonEncoder: JSONEncoder = JSONEncoder()) async throws
15+
where E: Encodable
16+
{
17+
try await self.set(key, to: jsonEncoder.encode(entity)).get()
18+
}
19+
20+
/// Sets key to an encodable item with an expiration time.
21+
public func setex<E>(_ key: RedisKey, toJSON entity: E, expirationInSeconds expiration: Int, jsonEncoder: JSONEncoder = JSONEncoder()) async throws
22+
where E: Encodable
23+
{
24+
try await self.setex(key, to: jsonEncoder.encode(entity), expirationInSeconds: expiration).get()
25+
}
26+
27+
/// Checks the existence of the provided keys in the database.
28+
///
29+
/// [https://redis.io/commands/exists](https://redis.io/commands/exists)
30+
/// - Parameter keys: A list of keys whose existence will be checked for in the database.
31+
/// - Returns: The number of provided keys which exist in the database.
32+
public func exists(_ keys: RedisKey...) async throws -> Int {
33+
try await self.exists(keys).get()
34+
}
35+
36+
/// Checks the existence of the provided keys in the database.
37+
///
38+
/// [https://redis.io/commands/exists](https://redis.io/commands/exists)
39+
/// - Parameter keys: A list of keys whose existence will be checked for in the database.
40+
/// - Returns: The number of provided keys which exist in the database.
41+
public func exists(_ keys: [RedisKey]) async throws -> Int {
42+
try await self.exists(keys).get()
43+
}
44+
45+
/// Sets a timeout on key. After the timeout has expired, the key will automatically be deleted.
46+
/// - Note: A key with an associated timeout is often said to be "volatile" in Redis terminology.
47+
///
48+
/// [https://redis.io/commands/expire](https://redis.io/commands/expire)
49+
/// - Parameters:
50+
/// - key: The key to set the expiration on.
51+
/// - timeout: The time from now the key will expire at.
52+
/// - Returns: `true` if the expiration was set.
53+
public func expire(_ key: RedisKey, after timeout: TimeAmount) async throws -> Bool {
54+
try await self.expire(key, after: timeout).get()
55+
}
56+
57+
/// Returns the remaining time-to-live (in seconds) of the provided key.
58+
///
59+
/// [https://redis.io/commands/ttl](https://redis.io/commands/ttl)
60+
/// - Parameter key: The key to check the time-to-live on.
61+
/// - Returns: The number of seconds before the given key will expire.
62+
public func ttl(_ key: RedisKey) async throws -> RedisKey.Lifetime {
63+
try await self.ttl(key).get()
64+
}
65+
66+
/// Returns the remaining time-to-live (in milliseconds) of the provided key.
67+
///
68+
/// [https://redis.io/commands/pttl](https://redis.io/commands/pttl)
69+
/// - Parameter key: The key to check the time-to-live on.
70+
/// - Returns: The number of milliseconds before the given key will expire.
71+
public func pttl(_ key: RedisKey) async throws -> RedisKey.Lifetime {
72+
try await self.pttl(key).get()
73+
}
74+
}

0 commit comments

Comments
 (0)