Rediska

Follow me on twitter…

Zend_Session save handler

Another good thing is storing your sessions in Redis.

Rediska has Zend_Session save handler:

<?php

$options = array(
    'keyPrefix' => 'SESSIONS_',
    'lifetime'  => 60 * 15 // 15 minutes
    // rediska - Rediska instance name, Rediska object or Rediska options for new instance. Default instance if not specified.
    'rediska'   => array(
         'servers' => array(
              array('host' => '127.0.0.1')
         )
    )
);

$saveHandler = new Rediska_Zend_Session_SaveHandler_Redis($options);

Zend_Session::setSaveHandler($saveHandler);

?>

If there are no Rediska parameters in options (except keyPrefix and lifetime) save handler will use Rediska default instance.

Comments

Boris June 14, 2012 09:29

We decided to move the project to store sessions in Redis (previously used Memcachedb SaveHandler), and encountered the same problem: When added to the session data contains the Cyrillic alphabet, the next request to the Web server, these data are missing from the session (there is only the empty namespace). The same data is lost (which do not contain Cyrillic), added after the addition of data from the Cyrillic alphabet. Tests: Sorry for such a great comment, wanted a more detailed description of the problem.

Boris June 14, 2012 09:30

Tests:

/******************************************************************
* 1st request:
*/
$testNS = new Zend_Session_Namespace('TestNS');
$testNS->var = 1;

print_r($_SESSION);

// Response:
Array
(
[TestNS] => Array
(
[var] => 1
)
)

/******************************************************************
 * 2nd request:
 */
$cyrillicNS = new Zend_Session_Namespace('Cyrillic');
$cyrillicNS->var = 'кириллица';

print_r($_SESSION);

// Response:
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] => Array
(
[var] => кириллица
)
)

/******************************************************************
 * 3rd request:
 * From the data contain Cyrillic characters have only a namespace.
 */
$testData = new Zend_Session_Namespace('testData');
$testData->var = 'a';

print_r($_SESSION);

// Response:
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] =>
[testData] => Array
(
[var] => a
)
)

/*******************************************************************
 * 4th request:
 * Again, add the data from the Cyrillic alphabet.
 */
$cyrillicNS = new Zend_Session_Namespace('Cyrillic');
$cyrillicNS->var = 'кириллица';

print_r($_SESSION);

// Response
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] => Array
(
[var] => кириллица
)

[testData] => Array
(
[var] => a
)
)

/******************************************************************
 * 5th request:
 * Data from the Cyrillic alphabet is not be lost until overwritten
* data without the Cyrillic alphabet, added after the data from
* the Cyrillic alphabet.
 */
$testData = new Zend_Session_Namespace('testData');
$testData->var = 'a';

print_r($_SESSION);

// Response
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] => Array
(
[var] => кириллица
)

[testData] => Array
(
[var] => a
)
)

/******************************************************************
 * 6th request:
 * Missing the last recorded data without the Cyrillic alphabet
* (left namespace).
 */
print_r($_SESSION);

// Response
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] => Array
(
[var] => кириллица
)

[testData] =>
)

/******************************************************************
 * 7th request:
 * Missing namespace.
 */
print_r($_SESSION);

// Response
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] => Array
(
[var] => кириллица
)
)

/******************************************************************
 * 8th request:
 * Missing data from the Cyrillic alphabet (left namespace).
* All subsequent requsts return the same response.
 */
print_r($_SESSION);

// Response
Array
(
[TestNS] => Array
(
[var] => 1
)

[Cyrillic] =>
)

Boris June 14, 2012 14:35

Again, sorry for such a large amount of data. Apparently the problem with the Cyrillic alphabet is not Rediska_Zend_Session_SaveHandler_Redis, but in general, with read/write Cyrillic data via sockets.

Fork me on GitHub