Skip to content

Instantly share code, notes, and snippets.

@v0112358
Last active August 22, 2018 09:30
Show Gist options
  • Save v0112358/2d2bcbec84fe2e75ea66b0c8a65c2f6b to your computer and use it in GitHub Desktop.
Save v0112358/2d2bcbec84fe2e75ea66b0c8a65c2f6b to your computer and use it in GitHub Desktop.
How PHP store data in Redis
////// Example: connect to Redis and store an array
<?php
$redis = new Redis;
$connect = $redis->connect( "172.18.0.1", "6379" );
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$array = array(
'message' => "My message",
'to_send' => 2,
'from_send' => 1,
'create_time' => date('Y-m-d H:i:s')
);
$input = $array;
$redis->set("messages",$input);
$getMessages = $redis->get("messages");
print_r($getMessages);
?>
///// Output from the script above
[root@d4860c5cd534 src]# /mnserver/php/bin/php ./test.php
Array
(
[message] => My message
[to_send] => 2
[from_send] => 1
[create_time] => 2018-08-22 16:25:41
)
///// Get messages key from Redis-cli
[root@cp ~]# redis-cli -h 172.18.0.1 GET messages
"a:4:{s:7:\"message\";s:10:\"My message\";s:7:\"to_send\";i:2;s:9:\"from_send\";i:1;s:11:\"create_time\";s:19:\"2018-08-22 16:25:41\";}"
a:4 => This mean we have 01 array with 4 elements.
///// How to read data is written by phpredis from Python ?
https://pypi.org/project/phpserialize/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment