Personal recommendations using PHP's own serialization functions serialize and Unserialize functions
We can encapsulate a redis class of our own
<?PHPclassmyredis{Private Static $handler; Private Static functionhandler () {if(!self::$handler) { self::$handler=NewRedis (); Self::$handlerConnect (' 127.0.0.1 ', ' 6379 '); } returnSelf::$handler; } Public Static functionGet$key){ $value= Self::handler (), Get ($key); $value _serl= @unserialize($value); if(Is_object($value _serl)||Is_array($value _serl)){ return $value _serl; } return $value; } Public Static functionSet$key,$value){ if(Is_object($value)||Is_array($value)){ $value=Serialize($value); } returnSelf::handler (), Set ($key,$value); }}
Call Test
<?PHPrequire_once' Myredis.php ';classmyobj{ Public functionRtime () {return Time(); }}$arr=Array(A);$obj=NewMyObj ();$str= ' My name is Jeffrey. '; Myredis:: Set (' arr ',$arr); Myredis:: Set (' obj ',$obj); Myredis:: Set (' Str ',$str);$_arr= Myredis::get (' arr ');$_obj= Myredis::get (' obj ');$_str= Myredis::get (' str ');
Var_dump($_arr);Var_dump($_obj-rtime ());Var_dump($_str);
How Redis saves arrays and objects