如何用cakephp 3.0保存redis中的会话?

时间:2015-11-04 10:57:04

标签: php cakephp redis cakephp-3.0

你好,我是cakephp 3.0中的新手。我知道一点点cakephp 2.0。我使用cakephp 2.0配置我的redis this help of url但是我不知道如何在cakephp 3.0中配置请帮帮我

3 个答案:

答案 0 :(得分:0)

您可以将会话设置为在app.php文件中使用缓存会话:

'Session' => [
    'defaults' => 'cache'
 ]

然后,您将缓存设置为redis:

 'Cache' => [
    'default' => [
        'className' => 'Redis',
     ],
  ]

答案 1 :(得分:0)

在vendor \ cakephp \ cakephp \ src \ Network \ Session.php

您可以看到默认的会话类型。

它们被列为php,cake,cache,database。

$defaults = [
        'php' => [
            'cookie' => 'CAKEPHP',
            'ini' => [
                'session.use_trans_sid' => 0,
            ]
        ],
        'cake' => [
            'cookie' => 'CAKEPHP',
            'ini' => [
                'session.use_trans_sid' => 0,
                'session.serialize_handler' => 'php',
                'session.use_cookies' => 1,
                'session.save_path' => TMP . 'sessions',
                'session.save_handler' => 'files'
            ]
        ],
        'cache' => [
            'cookie' => 'CAKEPHP',
            'ini' => [
                'session.use_trans_sid' => 0,
                'session.use_cookies' => 1,
                'session.save_handler' => 'user',
            ],
            'handler' => [
                'engine' => 'CacheSession',
                'config' => 'default'
            ]
        ],
        'database' => [
            'cookie' => 'CAKEPHP',
            'ini' => [
                'session.use_trans_sid' => 0,
                'session.use_cookies' => 1,
                'session.save_handler' => 'user',
                'session.serialize_handler' => 'php',
            ],
            'handler' => [
                'engine' => 'DatabaseSession'
            ]
        ]
    ]

这是使用Cache的默认配置的缓存。也许你想使用defualt作为文件缓存。

在app.php中 创建一个新的缓存配置

'redis' => [
        'className' => 'Redis',
        'server'=>'127.0.0.1',
        'port'=>6379
    ],

然后你可以在这里使用你的新缓存配置。

'Session' => [
    'cookie' => 'herewego',
    'ini' => [
        'session.use_trans_sid' => 0,
        'session.use_cookies' => 1,
        'session.save_handler' => 'user',
    ],
    'handler' => [
        'engine' => 'CacheSession',
        'config' => 'redis'
    ]
],

答案 2 :(得分:0)

Cakephp 4 发布以来,我目前正在研究它(似乎也存在于Cakphp 3中),并且似乎有一种解决方法。尚未测试。

此之后:https://book.cakephp.org/4/en/development/sessions.html#cache-sessions

app.php

    'Session' => [
        'defaults' => 'cache', //Use the Cache class to save session
       'handler' => [
           'config' => 'session'
       ]
    ],

跟随该链接:https://book.cakephp.org/4/en/core-libraries/caching.html#redisengine-options app.php

    /*
     * Configure the cache adapters.
     */
    'Cache' => [
        'session' => [
            'className' => RedisEngine::class,
            //`port` The port your Redis server is running on.
            //`host` The host your Redis server is running on.
            //`database` The database number to use for connection.
            //`password` Redis server password.
            //`persistent` Should a persistent connection be made to Redis.
            //`timeout` Connection timeout for Redis.
            //`unix_socket` Path to a unix socket for Redist.
        ],
    ],

稍后我将对此进行测试,并在需要时进行更新,但这确实很有希望。

更新2020-05-20:经过测试,可以正常工作