Dexter
Dexter

Reputation: 1796

How to save session in redis with cakephp 3.0?

Hello guy's I newbie in cakephp 3.0. I know little bit cakephp 2.0. I configured my redis with cakephp 2.0 with this help of url But I don't know how to configure in cakephp 3.0 please help me

Upvotes: 0

Views: 3299

Answers (3)

Ng Sek Long
Ng Sek Long

Reputation: 4786

Since Cakephp 4 is out, I am currently studying it (which seems exists in Cakphp 3 as well) and seems like there is a way to do it. Not tested yet.

Following this: 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'
       ]
    ],

Following this link: 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.
        ],
    ],

I am going to test this out later on and make update if needed, but it really seems promising.

Update 2020-05-20: Tested, it work fine

Upvotes: 1

Emre Karataşoğlu
Emre Karataşoğlu

Reputation: 1719

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

you can see the default type of session.

They are listed as 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'
            ]
        ]
    ]

Here is cache using default config of Cache. Maybe you want to use defualt as a File Cache.

In app.php create a new cache config

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

Then you can use your new cache config here.

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

Upvotes: 0

chrisShick
chrisShick

Reputation: 1096

You would set your session to use cache sessions inside the app.php file:

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

Then, you would set your cache to redis:

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

Upvotes: 0

Related Questions