Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Symfony: The Fast Track
  4. Japanese
  5. Redisにセッションを保存する

Redisにセッションを保存する

Webサイトのトラフィックやインフラストラクチャによって、PostgreSQLではなくRedisを使ってセッションを管理したいかもしれません。

プロジェクトのコードを分岐させて、セッションをファイルシステムからデータベースにセッションを移行したとき、新しいサービスを追加するすべての必要なステップを並べました。

こちらが、1つのパッチでプロジェクトにRedisを追加する方法です:

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
48
49
50
51
--- a/.platform.app.yaml
+++ b/.platform.app.yaml
@@ -14,6 +14,7 @@ runtime:
         - iconv
         - mbstring
         - pdo_pgsql
+        - redis
         - sodium
         - xsl
         
@@ -39,6 +40,7 @@ mounts:

 relationships:
     database: "database:postgresql"
+    redis: "rediscache:redis"
     
 hooks:
     build: |
--- a/.platform/services.yaml
+++ b/.platform/services.yaml
@@ -15,3 +15,6 @@ varnish:
 files:
     type: network-storage:1.0
     disk: 256
+
+rediscache:
+    type: redis:5.0
--- a/config/packages/framework.yaml
+++ b/config/packages/framework.yaml
@@ -7,7 +7,7 @@ framework:
     # Enables session support. Note that the session will ONLY be started if you read or write from it.
     # Remove or comment this section to explicitly disable session support.
     session:
-        handler_id: '%env(DATABASE_URL)%'
+        handler_id: '%env(REDIS_URL)%'
         cookie_secure: auto
         cookie_samesite: lax
         storage_factory_id: session.storage.factory.native
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -15,6 +15,10 @@ services:
       # - ./docker/db/data:/var/lib/postgresql/data:rw
 ###< doctrine/doctrine-bundle ###

+  redis:
+    image: redis:5-alpine
+    ports: [6379]
+
 volumes:
 ###> doctrine/doctrine-bundle ###
   db-data:

これは 美しく ないでしょうか?

Redisサービスを追加するために、Dockerを"再起動"します:

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

ブラウザでWebサイトを表示して、ローカルでテストします。以前と同じくすべての機能が使えます。

通常通りコミットしてデプロイします:

1
$ symfony cloud:deploy

より深く学ぶために

  • Redis ドキュメント.
Previous page Symfony の内部を知る
Next page RabbitMQをメッセージのブローカーとして使う
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.
TOC
    Version

    Symfony 5.4 is backed by

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Make sure your project is risk free

    Make sure your project is risk free

    Version:
    Locale:
    ebook

    This book is backed by:

    see all backers

    Symfony footer

    Avatar of Matthias Schmidt, a Symfony contributor

    Thanks Matthias Schmidt for being a Symfony contributor

    2 commits • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony