Releases: vapor/redis
Allow customizing encoding and decoding of RedisCache values
This patch was authored and released by @gwynne.
It is now possible to specify a custom Encoder
and Decoder
to use in place of JSONEncoder
and JSONDecoder
for storing and retrieving values cached in Redis via the Cache
interface.
Add support for `async`/`await`
This patch was authored and released by @0xTim.
Add async
APIs for Redis
Expose method on Redis objects to lease connections
This patch was authored and released by @Mordil.
Adds a withBorrowedClient
method on both Application.Redis
and Request.Redis
for being able to lease a RedisClient
for a specified amount of time to execute commands serially with.
This allows for a custom execution of MULTI
commands in the following way
request.redis.withBorrowedClient {
return client
.send(command: "MULTI")
.flatMap { _ in client.send(command: "PING") }
.flatMap { _ in client.send(command: "EXEC") }
}
// this is now an 'EventLoopFuture<RESPValue>' with the value being an array of RESPValue result of every queued command
This fixes #190
Add support for Vapor `Cache` protocol expiration time
This patch was authored by @madsodgaard and released by @0xTim.
Adds support for setting an expiration time when using Redis as the Vapor cache via app.caches.use(.redis)
. See v4.44.0 for more info.
This PR also fixes #187 by adding a function to set an expiration time when setting a key to JSON:
app.redis.setex("user", toJSON: User(name: "mads"), expirationInSeconds: 10)
Add cache protocol
This patch was authored and released by @tanner0101.
Add Redis implementation for Vapor's new cache protocol: vapor/vapor#2558
// Use redis as application cache
app.caches.use(.redis)
Use passed-in eventloop when acquiring RedisConnectionPool
This patch was authored and released by @siemensikkema.
Use the passed-in eventloop when acquiring RedisConnectionPool to prevent triggering a precondition related to ending up in a wrong eventloop (fixes #180, replaces: #183).
Support for Multiple Redis instances
This patch was authored by @danramteke and released by @Mordil. #120
Usage
Assuming a redis1
ID has been defined,
extension RedisID {
static var redis1: RedisID {
RedisID("redis1")
}
}
The Redis configuration in configure.swift
can look like this:
app.redis.configuration = try RedisConfiguration(url: Environment.get("REDIS_URL")!)
app.redis(.redis1).configuration = try RedisConfiguration(url: Environment.get("REDIS_URL_1")!)
app.redis(.redis2).configuration = try RedisConfiguration(url: Environment.get("REDIS_URL_2")!)
and then the routes in routes.swift
can look like this:
app.get("redis") { req in
req.redis.get("name").map { respValue in
return respValue.string ?? "name not found in default Redis"
}
}
app.get("redis1") { req in
req.redis(.redis1).get("name").map { respValue in
return respValue.string ?? "name not found in Redis 1"
}
}
Default Redis
All previous syntax is still supported. If the redis
property is used rather than the redis(_:)
method, it will always refer to the default
RedisID
.
Redis 4.0.0
Docs can be found at https://docs.vapor.codes/4.0/redis/overview/
Change Sessions driver to be generic
This patch was co-authored by @Mordil and @gwynne and released by @Mordil.
This changes the RedisSessionsDriver
to be generic over the RedisSessionsDelegate
to drop the performance overhead of the delegate existential.
In general, no one should see a breaking change unless you were passing a RedisSessionsDelegate
as an existential - as you will see the "RedisSessionsDelegate cannot be passed to a generic context" message
Change semantics of Redis sessions with key and ID generation
This patch was authored and released by @Mordil.
This addresses some semantic issues with the original implementation of SessionDriver
for Redis in #175.
Two important changes were made to RedisSessionsDelegate
:
makeNewID
does not prefix the ID withvrs-
anymore- A new
makeKey(for:)
optional method has been added to the protocol to convert an ID to aRediStack.RedisKey
to customize how the id is represented as a key.- The default implementation adds the
vrs-
prefix.
- The default implementation adds the
In addition, the protocol has seen two quality of life (breaking) changes to the required protocol methods:
- Their argument labels have been updated to read a bit nicer at call sites, and the
RedisKey
is now passed instead of aSessionID
redis(_:storeData:forID:)
->redis(_:store:with:)
redis(_:fetchDataForID:)
->redis(_:fetchDataFor:)
- They have been marked
@inlinable
in the protocol definition. To take advantage of potential inlining, make sure you also mark your conformances with@inlinable
.