Skip to content

Utilizzare Redis per memorizzare le sessioni

A seconda del traffico sul sito web e/o la sua infrastruttura, potreste volere utilizzare Redis per gestire le sessioni utente al posto di PostgreSQL.

Quando abbiamo parlato di utilizzare branch per il codice di progetto, al fine di spostare le sessioni dal filesystem al database, abbiamo elencato tutti gli step necessari per aggiungere un nuovo servizio.

Qui è dove possiamo aggiungere Redis al progetto in un sol colpo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
--- i/.upsun/config.yaml
+++ w/.upsun/config.yaml
@@ -37,6 +37,7 @@ applications:
                 - iconv
                 - mbstring
                 - pdo_pgsql
+                - redis
                 - sodium
                 - xsl

@@ -62,6 +63,7 @@ applications:

         relationships:
             database: "database:postgresql"
+            redis: "rediscache:redis"

         hooks:
             build: |
--- i/.upsun/config.yaml
+++ w/.upsun/config.yaml
@@ -21,3 +21,6 @@ services:
             type: network-storage:2.0

+    rediscache:
+        type: redis:8.0
+
 applications:
--- i/compose.yaml
+++ w/compose.yaml
@@ -14,6 +14,10 @@ services:
       # - ./docker/db/data:/var/lib/postgresql/data:rw
 ###< doctrine/doctrine-bundle ###

+  redis:
+    image: redis:8.0-alpine
+    ports: [6379]
+
 volumes:
 ###> doctrine/doctrine-bundle ###
   database_data:
--- i/config/packages/framework.yaml
+++ w/config/packages/framework.yaml
@@ -4,3 +4,3 @@ framework:
     # Note that the session will be started ONLY if you read or write from it.
     session:
-        handler_id: '%env(resolve:DATABASE_URL)%'
+        handler_id: '%env(REDIS_URL)%'

Non è bellissimo?

Riavviare Docker per far partire il servizio Redis:

1
2
$ docker compose stop
$ docker compose up -d --remove-orphans

Testare localmente visitando il sito web; tutto dovrebbe funzionare come prima.

Facciamo un commit e rilasciamo il codice come sempre:

1
$ symfony cloud:push
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.
TOC
    Version