Skip to content

Commit ff67a00

Browse files
Merge pull request #3579 from SwiftPackageIndex/add-redis-cache
Add redis cache
2 parents 8797eb4 + e4ae483 commit ff67a00

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

Package.resolved

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let package = Package(
4848
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),
4949
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
5050
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.13.0"),
51+
.package(url: "https://github.com/vapor/redis.git", from: "4.0.0"),
5152
.package(url: "https://github.com/vapor/vapor.git", from: "4.102.0"),
5253
],
5354
targets: [
@@ -71,6 +72,7 @@ let package = Package(
7172
.product(name: "Fluent", package: "fluent"),
7273
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
7374
.product(name: "Parsing", package: "swift-parsing"),
75+
.product(name: "Redis", package: "redis"),
7476
.product(name: "ShellOut", package: "ShellOut"),
7577
.product(name: "SwiftPMDataModel-auto", package: "swift-package-manager"),
7678
.product(name: "SwiftPMPackageCollections", package: "swift-package-manager"),

Sources/App/configure.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
import Fluent
1616
import FluentPostgresDriver
17+
import Redis
1718
import Vapor
1819

20+
1921
@discardableResult
2022
public func configure(_ app: Application) async throws -> String {
2123
#if DEBUG && os(macOS)
@@ -49,6 +51,7 @@ public func configure(_ app: Application) async throws -> String {
4951
// This parameter could also be made configurable via an env variable.
5052
let maxConnectionsPerEventLoop = 3
5153

54+
// Setup database connection
5255
guard
5356
let host = Environment.get("DATABASE_HOST"),
5457
let port = Environment.get("DATABASE_PORT").flatMap(Int.init),
@@ -75,6 +78,13 @@ public func configure(_ app: Application) async throws -> String {
7578
sqlLogLevel: .debug),
7679
as: .psql)
7780

81+
// Setup Redis connection
82+
do {
83+
app.redis.configuration = try RedisConfiguration(hostname: "redis")
84+
} catch {
85+
app.logger.warning("Failed to configure Redis, caching disabled. Error: \(error)")
86+
}
87+
7888
do { // Migration 001 - schema 1.0
7989
app.migrations.add(CreatePackage())
8090
app.migrations.add(CreateRepository())

app.yml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,38 @@ services:
161161
- backend
162162

163163
alerting:
164-
image: registry.gitlab.com/finestructure/swiftpackageindex:${VERSION}
165-
<<: *shared
166-
depends_on:
167-
- migrate
168-
entrypoint: ["/bin/bash"]
169-
command: ["-c", "--",
164+
image: registry.gitlab.com/finestructure/swiftpackageindex:${VERSION}
165+
<<: *shared
166+
depends_on:
167+
- migrate
168+
entrypoint: ["/bin/bash"]
169+
command: ["-c", "--",
170170
"trap : TERM INT; while true; do ./Run alerting --env ${ENV} --time-period ${ALERTING_TIME_PERIOD:-4} --limit ${ALERTING_LIMIT:-2000}; sleep ${ALERTING_SLEEP:-300}; done"
171-
]
172-
deploy:
173-
resources:
174-
limits:
175-
memory: 0.5GB
176-
restart_policy:
171+
]
172+
deploy:
173+
resources:
174+
limits:
175+
memory: 0.5GB
176+
restart_policy:
177177
max_attempts: 5
178-
networks:
179-
- backend
178+
networks:
179+
- backend
180180

181+
redis:
182+
# Eventually we'll probably want to use redis/redis-stack-server:7.4.0-v1 here,
183+
# which excludes the RedisInsight GUI front-end.
184+
image: redis/redis-stack:7.4.0-v1
185+
environment:
186+
REDIS_ARGS: '--maxmemory 4GB --maxmemory-policy allkeys-lru'
187+
ports:
188+
- '6379:6379'
189+
- '8001:8001'
190+
deploy:
191+
resources:
192+
limits:
193+
memory: 4.2GB
194+
networks:
195+
- backend
181196

182197
migrate:
183198
image: registry.gitlab.com/finestructure/swiftpackageindex:${VERSION}

0 commit comments

Comments
 (0)