Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f200730

Browse files
_serialize method
This commit adds a utility method as a counterpart to the _unserialize utility method, allowing users to manually serialize data themselves before sending it to Redis. This could be useful for calls going through EVAL, as phpredis can't automatically serialize/unserialize in that case. Addresses phpredis#431
1 parent 1a89ec2 commit f200730

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.markdown

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,7 @@ $ret = FALSE if x has been modified between the call to WATCH and the call to EX
29902990
* [clearLastError](#) - Clear the last error message
29912991
* [_prefix](#) - A utility method to prefix the value with the prefix setting for phpredis
29922992
* [_unserialize](#) - A utility method to unserialize data with whatever serializer is set up
2993+
* [_serialize](#) - A utility method to serialize data with whatever serializer is set up
29932994

29942995
### eval
29952996
-----
@@ -3139,6 +3140,28 @@ $redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
31393140
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
31403141
~~~
31413142

3143+
### _serialize
3144+
-----
3145+
_**Description**_: A utility method to serialize values manually.
3146+
3147+
This method allows you to serialize a value with whatever serializer is configured, manually.
3148+
This can be useful for serialization/unserialization of data going in and out of EVAL commands
3149+
as phpredis can't automatically do this itself. Note that if no serializer is set, phpredis
3150+
will change Array values to 'Array', and Objects to 'Object'.
3151+
3152+
##### *Parameters*
3153+
*value*: Mixed. The value to be serialized
3154+
3155+
##### *Examples*
3156+
~~~
3157+
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
3158+
$redis->_serialize("foo"); // returns "foo"
3159+
$redis->_serialize(Array()); // Returns "Array"
3160+
$redis->_serialize(new stdClass()); // Returns "Object"
3161+
3162+
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
3163+
$redis->_serialize("foo"); // Returns 's:3:"foo";'
3164+
31423165
### _unserialize
31433166
-----
31443167
_**Description**_: A utility method to unserialize data with whatever serializer is set up.

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ PHP_METHOD(Redis, time);
142142
PHP_METHOD(Redis, getLastError);
143143
PHP_METHOD(Redis, clearLastError);
144144
PHP_METHOD(Redis, _prefix);
145+
PHP_METHOD(Redis, _serialize);
145146
PHP_METHOD(Redis, _unserialize);
146147

147148
PHP_METHOD(Redis, mset);

redis.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ static zend_function_entry redis_functions[] = {
245245
PHP_ME(Redis, clearLastError, NULL, ZEND_ACC_PUBLIC)
246246

247247
PHP_ME(Redis, _prefix, NULL, ZEND_ACC_PUBLIC)
248+
PHP_ME(Redis, _serialize, NULL, ZEND_ACC_PUBLIC)
248249
PHP_ME(Redis, _unserialize, NULL, ZEND_ACC_PUBLIC)
249250

250251
PHP_ME(Redis, client, NULL, ZEND_ACC_PUBLIC)
@@ -6458,6 +6459,35 @@ PHP_METHOD(Redis, _prefix) {
64586459
}
64596460
}
64606461

6462+
/*
6463+
* {{{ proto Redis::_serialize(value)
6464+
*/
6465+
PHP_METHOD(Redis, _serialize) {
6466+
zval *object;
6467+
RedisSock *redis_sock;
6468+
zval *z_val;
6469+
char *val;
6470+
int val_free, val_len;
6471+
6472+
// Parse arguments
6473+
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz",
6474+
&object, redis_ce, &z_val) == FAILURE)
6475+
{
6476+
RETURN_FALSE;
6477+
}
6478+
6479+
// Grab socket
6480+
if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
6481+
RETURN_FALSE;
6482+
}
6483+
6484+
// Serialize, which will return a value even if no serializer is set
6485+
val_free = redis_serialize(redis_sock, z_val, &val, &val_len TSRMLS_CC);
6486+
6487+
// Return serialized value. Tell PHP to make a copy if redis_serialize didn't.
6488+
RETURN_STRINGL(val, val_len, !val_free);
6489+
}
6490+
64616491
/*
64626492
* {{{ proto Redis::_unserialize(value)
64636493
*/

tests/TestRedis.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,6 +4428,34 @@ public function testEvalSHA() {
44284428
$this->assertTrue(1 === $this->redis->evalsha($sha));
44294429
}
44304430

4431+
public function testSerialize() {
4432+
$vals = Array(1, 1.5, 'one', Array('here','is','an','array'));
4433+
4434+
// Test with no serialization at all
4435+
$this->assertTrue($this->redis->_serialize('test') === 'test');
4436+
$this->assertTrue($this->redis->_serialize(1) === '1');
4437+
$this->assertTrue($this->redis->_serialize(Array()) === 'Array');
4438+
$this->assertTrue($this->redis->_serialize(new stdClass) === 'Object');
4439+
4440+
$arr_serializers = Array(Redis::SERIALIZER_PHP);
4441+
if(defined('Redis::SERIALIZER_IGBINARY')) {
4442+
$arr_serializers[] = Redis::SERIALIZER_IGBINARY;
4443+
}
4444+
4445+
foreach($arr_serializers as $mode) {
4446+
$arr_enc = Array();
4447+
$arr_dec = Array();
4448+
4449+
foreach($vals as $k => $v) {
4450+
$enc = $this->redis->_serialize($v);
4451+
$dec = $this->redis->_unserialize($enc);
4452+
4453+
// They should be the same
4454+
$this->assertTrue($enc == $dec);
4455+
}
4456+
}
4457+
}
4458+
44314459
public function testUnserialize() {
44324460
$vals = Array(
44334461
1,1.5,'one',Array('this','is','an','array')

0 commit comments

Comments
 (0)