phpredis56
① mac中怎樣通過brew 安裝php的redis擴展
首先需要確定你自己的php版本,
然後使用brew的search功能搜索下,是否存在:brew search redis
確定存在,就安裝(這里以php7.1為例,可根據自身版本做相應調整,如php56-redis):brew install php71-redis
但是個人發現其實可能會遇到一些問題,如果你的項目不是redis的重度用戶,可以使用composer包,如:composer require predis/predis
② php中關於redis和資料庫
select 查詢的時候始終先查 redis 有沒有,沒有去查資料庫,再把結果緩存起來;
update 修改完資料庫內容後,同時對 redis 中緩存的數據做一下 update 更新操作,這樣 select 查詢 redis 的時候就是查詢的最新數據;
同理,delete、insert 操作資料庫後也要同時對 redis 中緩存的數據做 update 更新操作,這樣 select 查詢 redis 的時候就是查詢的最新數據;
這樣,所有的查詢操作就都是對 redis 做緩存讀取,可以緩解資料庫的壓力;
③ php redis如何使用
開始在
PHP
中使用
Redis
前,要確保已經安裝了
redis
服務及
PHP
redis
驅動,且你的機器上能正常使用
PHP。
PHP安裝redis擴展
/usr/local/php/bin/phpize
#php安裝後的路徑
./configure
--with-php-config=/usr/local/php/bin/php-config
make
&&
make
install
修改php.ini文件
vi
/usr/local/php/lib/php.ini
增加如下內容:
extension_dir
=
"/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so
安裝完成後重啟php-fpm
或
apache。查看phpinfo信息,就能看到redis擴展。
連接到
redis
服務
<?php
//連接本地的
Redis
服務
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//查看服務是否運行
echo
"Server
is
running:
"
.
$redis->ping();
?>
執行腳本,輸出結果為:
Connection
to
server
sucessfully
Server
is
running:
PONG
Redis
PHP
String(字元串)
實例
<?php
//連接本地的
Redis
服務
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//設置
redis
字元串數據
$redis->set("tutorial-name",
"Redis
tutorial");
//
獲取存儲的數據並輸出
echo
"Stored
string
in
redis::
"
.
jedis.get("tutorial-name");
?>
執行腳本,輸出結果為:
Connection
to
server
sucessfully
Stored
string
in
redis::
Redis
tutorial
Redis
PHP
List(列表)
實例
<?php
//連接本地的
Redis
服務
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//存儲數據到列表中
$redis->lpush("tutorial-list",
"Redis");
$redis->lpush("tutorial-list",
"Mongodb");
$redis->lpush("tutorial-list",
"Mysql");
//
獲取存儲的數據並輸出
$arList
=
$redis->lrange("tutorial-list",
0
,5);
echo
"Stored
string
in
redis::
"
print_r($arList);
?>
執行腳本,輸出結果為:
Connection
to
server
sucessfully
Stored
string
in
redis::
Redis
Mongodb
Mysql
Redis
PHP
Keys
實例
<?php
//連接本地的
Redis
服務
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//
獲取數據並輸出
$arList
=
$redis->keys("*");
echo
"Stored
keys
in
redis::
"
print_r($arList);
?>
執行腳本,輸出結果為:
Connection
to
server
sucessfully
Stored
string
in
redis::
tutorial-name
tutorial-list
④ PHP刪除Redis所有數據
1、創建userinfo_update.php,用於查詢用戶信息,先顯示信息,在修改:先通過GET獲取用戶編號查詢用戶信息:$sql = "select * from user_info where user_id='".$_GET['userId']."'"; $result = mysql_query($sql,$con);if($row = mysql_fetch_array($result)){}。
⑤ linux 下php怎麼安裝redis
1、PHP redis下載地址:http://pecl.php.net/package/redis
[cc lang="bash" escaped="true"][root@Redis ~]# wget http://pecl.php.net/get/redis-2.2.7.tgz[/cc]
2、解壓安裝並進入Redis目錄
[cc lang="bash" escaped="true"][root@Redis ~]# tar xzf redis-2.2.5.tgz
[root@Redis ~]# cd redis-2.2.5[/cc]
3、在Redis文件夾下,生成configure配置文件
[cc lang="bash" escaped="true"][root@Redis redis-2.2.5]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Mole Api No: 20090626
Zend Extension Api No: 220090626
[root@Redis redis-2.2.5]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@Redis redis-2.2.5]# make
[root@Redis redis-2.2.5]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
cp: cannot create regular file `/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/#INST@6338#': Permission denied
make: *** [install-moles] Error 1
注意:一定要用root用戶。
[root@Redis redis-2.2.5]# su
Password:
[root@web redis-2.2.5]# make install[/cc]
4、在PHP配置文件php.ini裡面載入Redis擴展
[cc lang="vim" escaped="true"]extension=redis.so[/cc]
5、查看phpinfo,Redis擴展是否載入
redis
Redis Support enabled
Redis Version 2.2.5
至此,Redis擴展已經安裝成功並載入,可以用PHP操作Redis了。
附:
windows下安裝Redis擴展就更加簡單了,找到對應的版本,下載dll文件,放到PHP目錄下面的ext,修改PHP的配置文件php.ini,載入extension=php_redis.dll,重啟Apache,查看phpinfo是否有Redis,如果有就恭喜你,已經安裝成功了,就是這么簡單。
⑥ php實現redis資料庫指定庫號遷移的方法
這篇文章主要介紹了php實現redis資料庫指定庫號遷移的方法,涉及對於redis資料庫的操作技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php實現redis資料庫指定庫號遷移的方法,分享給大家供大家參考。具體如下:
redis普通的資料庫遷移,只能整個redis
save,或者利用主從,當然也可以安裝一個redis-mp,不過比較麻煩,這里提供一種php的腳本,實現指定庫號的遷移,其實也就是遍歷根據存儲類型,讀出來,插入新庫,效果是這樣:
代碼如下:
[root@localhost
~]#
php
1.php
1/407
101/407
201/407
301/407
401/407
PHP實例代碼如下:
代碼如下:
<?php
$from
=
'10.0.2.52:6379/7';
$to
=
'127.0.0.1:6379/7';
$from_redis
=
redis_init($from);
$to_redis
=
redis_init($to);
$keys
=
$from_redis->keys('*');
$count
=
0;
$total
=
count($keys);
foreach($keys
as
$key){
if(++$count
%
100
==
1){
echo
"$count/$totaln";
}
$type
=
$from_redis->type($key);
switch($type){
case
Redis::REDIS_STRING:
$val
=
$from_redis->get($key);
$to_redis->set($key,
$val);
break;
case
Redis::REDIS_LIST:
$list
=
$from_redis->lRange($key,
0,
-1);
foreach($list
as
$val){
$to_redis->rPush($key,
$val);
}
break;
case
Redis::REDIS_HASH:
$hash
=
$from_redis->hGetAll($key);
$to_redis->hMSet($key,
$hash);
break;
case
Redis::REDIS_ZSET:
$zset
=
$from_redis->zRange($key,
0,
-1,
true);
foreach($zset
as
$val=>$score){
$to_redis->zAdd($key,
$score,
$val);
}
break;
}
}
function
redis_init($conf){
$redis
=
new
Redis();
preg_match('/^([^:]+)(:[0-9]+)?/(.+)?/',
$conf,
$ms);
$host
=
$ms[1];
$port
=
trim($ms[2],
':');
$db
=
$ms[3];
$redis->connect($host,
$port);
$redis->select($db);
return
$redis;
}
?>
希望本文所述對大家的php程序設計有所幫助。
⑦ php怎樣使用redis緩存數據
<?php
/**
* Redis緩存操作
* @author hxm
* @version 1.0
* @since 2015.05.04
*/
class RCache extends Object implements CacheFace
{
private $redis = null; //redis對象
private $sId = 1; //servier服務ID
private $con = null;//鏈接資源
/**
* 初始化Redis
*
* @return Object
*/
public function __construct()
{
if ( !class_exists('Redis') )
{
throw new QException('PHP extension does not exist: Redis');
}
$this->redis = new Redis();
}
/**
* 鏈接memcahce服務
*
* @access private
* @param string $key 關鍵字
* @param string $value 緩存內容
* @return array
*/
private function connect( $sid )
{
$file = $this->CacheFile();
require $file;
if(! isset($cache) )
{
throw new QException('緩存配置文件不存在'.$file);
}
$server = $cache[$this->cacheId];
$sid = isset($sid) == 0 ? $this->sId : $sid;//memcache服務選擇
if ( ! $server[$sid])
{
throw new QException('當前操作的緩存伺服器配置文件不存在');
}希望能幫到你,我還在後盾網學習呢,有不會的可以問我,一會有空回答你。(^ω^)
⑧ mac中怎樣通過brew 安裝php的redis擴展
1、下載php-redis;
2、下載完後進行解壓,然後移動到系統自帶php的同級目錄;①
cp /Users/panxu/Desktop/phpredis /etc
3、進入phpredis;
cd /etc/phpredis
4、動態安裝php擴展模塊;②
/usr/bin/phpize
5、進行編譯;
./configure --with-php-config=/usr/bin/php-config
6、開始安裝;
make && make install
安裝完成,如果成功,會有如下提示:
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20160303/
7、找到php.ini文件,並進行編輯;③
vi php.ini
在配置文件最後一行加入:
extension=redis.so
8、重啟php-fpm,完成安裝。④
步驟中的涉及到的資源和詳細解釋請參考:https://panxu.net/article/8400.html