您好,登錄后才能下訂單哦!
redisadd.php頁(yè)面
<?php header("content-type:text/html;charset=utf-8"); //創(chuàng)建一個(gè)redis對(duì)象 $redis=new Redis(); if(!$redis->connect('127.0.0.1',6379)){ die("連接失敗"); } //添加一個(gè)字符串 if($redis->set('name', '中國(guó)')){ echo "add name ok"; } if($redis->set('online', '100')){ echo "add online ok"; } //數(shù)字自增1 $redis->incr('online'); //添加一個(gè)數(shù)組 $cities=array( 'no1'=>'北京', 'no2'=>'上海' ); //必須把數(shù)組序列化serialize成一個(gè)字符串才行,否則get不出來(lái)。 if($redis->set('cities', serialize($cities))){ echo "add cities ok"; } //添加一個(gè)對(duì)象 class Dog{ public $name; public $age; function __construct($name,$age){ $this->name=$name; $this->age=$age; } } $dog=new Dog('xiaoqiang',10); if($redis->set('dog', serialize($dog))){ echo "add dog ok"; } $dog1=new Dog('xiaoqiang1',1); $dog2=new Dog('xiaoqiang2',2); $dog3=new Dog('xiaoqiang3',3); $dog4=new Dog('xiaoqiang4',4); $dog5=new Dog('xiaoqiang5',5); $dog6=new Dog('xiaoqiang6',6); $redis->delete('dogline'); //注意序列化后才存進(jìn)redis if($redis->rPush('dogline', serialize($dog))); if($redis->rPush('dogline', serialize($dog1))); if($redis->rPush('dogline', serialize($dog2))); if($redis->rPush('dogline', serialize($dog3))); if($redis->rPush('dogline', serialize($dog4))); if($redis->rPush('dogline', serialize($dog5))); if($redis->rPush('dogline', serialize($dog6))); ?>
redisget.php頁(yè)面
<?php header("content-type:text/html;charset=utf-8"); //創(chuàng)建一個(gè)redis對(duì)象 $redis=new Redis(); if(!$redis->connect('127.0.0.1',6379)){ die("連接失敗"); } //一定要引入Dog類的定義才行。 class Dog{ public $name; public $age; function __construct($name,$age){ $this->name=$name; $this->age=$age; } } //添加一個(gè)字符串 $val=$redis->get('name'); $val1=$redis->get('online'); $val2=unserialize($redis->get('cities')); $val3=unserialize($redis->get('dog'));//注意一定要引入類的定義 var_dump($val); echo '<br />'; var_dump($val1); echo '<br />'; var_dump($val2); echo '<br />'; //取出狗全部屬性 var_dump($val3); echo '<br />'; //取出狗名 var_dump($val3->name); echo '<br />'; $doglineLen=$redis->lSize('dogline'); for($i=0;$i<$doglineLen;$i++){ $mydog=$redis->lIndex('dogline',$i); echo '<pre>'; var_dump(unserialize($mydog));//注意反序列化后才取出 echo '</pre>'; } ?>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。