Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to RediStack 2.0 #203

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.5
import PackageDescription

let package = Package(
Expand All @@ -10,7 +10,7 @@ let package = Package(
.library(name: "Redis", targets: ["Redis"])
],
dependencies: [
.package(url: "https://gitlab.com/mordil/RediStack.git", from: "1.1.0"),
.package(url: "https://gitlab.com/mordil/RediStack.git", .revisionItem("0465b34ef3f45c45d751100ef572c1afb4b1b50c")),
.package(url: "https://github.com/vapor/vapor.git", from: "4.50.0"),
],
targets: [
Expand Down
37 changes: 22 additions & 15 deletions Sources/Redis/Application+Redis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,66 @@ extension Application {

// MARK: RedisClient
extension Application.Redis: RedisClient {
public var eventLoop: EventLoop {
self.application.eventLoopGroup.next()
}
public var eventLoop: EventLoop { self.application.eventLoopGroup.next() }
public var defaultLogger: Logger? { self.application.logger }

public func logging(to logger: Logger) -> RedisClient {
self.application.redis(self.id)
.pool(for: self.eventLoop)
.logging(to: logger)
}

public func send(command: String, with arguments: [RESPValue]) -> EventLoopFuture<RESPValue> {
public func send<CommandResult>(
_ command: RedisCommand<CommandResult>,
eventLoop: EventLoop? = nil,
logger: Logger? = nil
) -> EventLoopFuture<CommandResult> {
self.application.redis(self.id)
.pool(for: self.eventLoop)
.logging(to: self.application.logger)
.send(command: command, with: arguments)
.send(command, eventLoop: eventLoop, logger: logger)
}

public func subscribe(
to channels: [RedisChannelName],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) -> EventLoopFuture<Void> {
self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.subscribe(to: channels, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.subscribe(to: channels, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
}

public func unsubscribe(from channels: [RedisChannelName]) -> EventLoopFuture<Void> {
public func unsubscribe(from channels: [RedisChannelName], eventLoop: EventLoop? = nil, logger: Logger? = nil) -> EventLoopFuture<Void> {
self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.unsubscribe(from: channels)
.unsubscribe(from: channels, eventLoop: eventLoop, logger: logger)
}

public func psubscribe(
to patterns: [String],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) -> EventLoopFuture<Void> {
self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.psubscribe(to: patterns, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.psubscribe(to: patterns, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
}

public func punsubscribe(from patterns: [String]) -> EventLoopFuture<Void> {
public func punsubscribe(from patterns: [String], eventLoop: EventLoop? = nil, logger: Logger? = nil) -> EventLoopFuture<Void> {
self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.punsubscribe(from: patterns)
.punsubscribe(from: patterns, eventLoop: eventLoop, logger: logger)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Redis/Redis+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private struct RedisCache<CacheEncoder: RedisCacheEncoder, CacheDecoder: RedisCa
.tryFuture { try self.encoder.encode(value) }
.flatMap {
if let expirationTime = expirationTime {
return self.client.setex(RedisKey(key), to: $0, expirationInSeconds: expirationTime.seconds)
return self.client.send(.setex(RedisKey(key), to: $0, expirationInSeconds: expirationTime.seconds), eventLoop: nil, logger: nil)
} else {
return self.client.set(RedisKey(key), to: $0)
}
Expand Down
70 changes: 43 additions & 27 deletions Sources/Redis/Redis+Concurrency.swift
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
#if compiler(>=5.5) && canImport(_Concurrency)
#if canImport(_Concurrency)
import NIOCore
import Vapor

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
extension Application.Redis {
public func send(command: String, with arguments: [RESPValue]) async throws -> RESPValue {
public func send<CommandResult>(
_ command: RedisCommand<CommandResult>,
eventLoop: EventLoop? = nil,
logger: Logger? = nil
) async throws -> CommandResult {
try await self.application.redis(self.id)
.pool(for: self.eventLoop)
.logging(to: self.application.logger)
.send(command: command, with: arguments).get()
.send(command, eventLoop: eventLoop, logger: logger)
.get()
}

public func subscribe(
to channels: [RedisChannelName],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) async throws {
try await self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.subscribe(to: channels, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.subscribe(to: channels, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.get()
}

public func unsubscribe(from channels: [RedisChannelName]) async throws {
public func unsubscribe(from channels: [RedisChannelName], eventLoop: EventLoop?, logger: Logger?) async throws {
try await self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.unsubscribe(from: channels)
.unsubscribe(from: channels, eventLoop: eventLoop, logger: logger)
.get()
}

public func psubscribe(
to patterns: [String],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) async throws {
try await self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.psubscribe(to: patterns, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.psubscribe(to: patterns, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.get()
}

public func punsubscribe(from patterns: [String]) async throws {
public func punsubscribe(from patterns: [String], eventLoop: EventLoop? = nil, logger: Logger? = nil) async throws {
try await self.application.redis(self.id)
.pubsubClient
.logging(to: self.application.logger)
.punsubscribe(from: patterns)
.punsubscribe(from: patterns, eventLoop: eventLoop, logger: logger)
.get()
}
}
Expand All @@ -75,62 +84,69 @@ extension RedisClient {
public func setex<E>(_ key: RedisKey, toJSON entity: E, expirationInSeconds expiration: Int) async throws
where E: Encodable
{
try await self.setex(key, to: JSONEncoder().encode(entity), expirationInSeconds: expiration).get()
try await self.send(.setex(key, to: JSONEncoder().encode(entity), expirationInSeconds: expiration), eventLoop: nil, logger: nil).get()
}
}

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
extension Request.Redis {
public func send(command: String, with arguments: [RESPValue]) async throws -> RESPValue {
public func send<CommandResult>(
_ command: RedisCommand<CommandResult>,
eventLoop: EventLoop? = nil,
logger: Logger? = nil
) async throws -> CommandResult {
try await self.request.application.redis(self.id)
.pool(for: self.eventLoop)
.logging(to: self.request.logger)
.send(command: command, with: arguments)
.send(command, eventLoop: eventLoop, logger: logger)
.get()
}

public func subscribe(
to channels: [RedisChannelName],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) async throws {
try await self.request.application.redis(self.id)
.pubsubClient
.logging(to: self.request.logger)
.subscribe(to: channels, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.subscribe(to: channels, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.get()
}

public func unsubscribe(from channels: [RedisChannelName]) async throws {
public func unsubscribe(from channels: [RedisChannelName], eventLoop: EventLoop? = nil, logger: Logger? = nil) async throws {
try await self.request.application.redis(self.id)
.pubsubClient
.logging(to: self.request.logger)
.unsubscribe(from: channels)
.unsubscribe(from: channels, eventLoop: eventLoop, logger: logger)
.get()
}

public func psubscribe(
to patterns: [String],
eventLoop: EventLoop? = nil,
logger: Logger? = nil,
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?
onSubscribe subscribeHandler: RedisSubscribeHandler?,
onUnsubscribe unsubscribeHandler: RedisUnsubscribeHandler?
) async throws {
try await self.request.application.redis(self.id)
.pubsubClient
.logging(to: self.request.logger)
.psubscribe(to: patterns, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.psubscribe(to: patterns, eventLoop: eventLoop, logger: logger, messageReceiver: receiver, onSubscribe: subscribeHandler, onUnsubscribe: unsubscribeHandler)
.get()
}

public func punsubscribe(from patterns: [String]) async throws {
public func punsubscribe(from patterns: [String], eventLoop: EventLoop? = nil, logger: Logger? = nil) async throws {
try await self.request.application.redis(self.id)
.pubsubClient
.logging(to: self.request.logger)
.punsubscribe(from: patterns)
.punsubscribe(from: patterns, eventLoop: eventLoop, logger: logger)
.get()
}
}


#endif
35 changes: 0 additions & 35 deletions Sources/Redis/RedisClient+Codable.swift

This file was deleted.

Loading