|
| 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