亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

PHP 使用 Redis

 

安裝

開始在 php 中使用 redis 前, 我們需要確保已經(jīng)安裝了 redis 服務(wù)及 PHP redis 驅(qū)動(dòng),且你的機(jī)器上能正常使用 PHP。 接下來讓我們安裝 PHP redis 驅(qū)動(dòng):下載地址為:https://github.com/phpredis/phpredis/releases

PHP安裝redis擴(kuò)展

以下操作需要在下載的 phpredis 目錄中完成:

$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ cd phpredis-3.1.4 # 進(jìn)入 phpredis 目錄
$ /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

增加如下內(nèi)容:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so

安裝完成后重啟php-fpm 或 Apache。查看phpinfo信息,就能看到redis擴(kuò)展。

PHP 使用 Redis

 

連接到 redis 服務(wù)

<?php
 //連接本地的 Redis 服務(wù)
 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379);
 echo "Connection to server successfully";
 //查看服務(wù)是否運(yùn)行
 echo "Server is running: " . $redis->ping();
?>

執(zhí)行腳本,輸出結(jié)果為:

Connection to server sucessfully
Server is running: PONG

Redis PHP String(字符串) 實(shí)例

<?php
 //連接本地的 Redis 服務(wù)
 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379);
 echo "Connection to server successfully";
 //設(shè)置 redis 字符串?dāng)?shù)據(jù)
 $redis->set("tutorial-name", "Redis tutorial");
 // 獲取存儲(chǔ)的數(shù)據(jù)并輸出
 echo "Stored string in redis:: " . $redis->get("tutorial-name");
?>

執(zhí)行腳本,輸出結(jié)果為:

Connection to server sucessfully
Stored string in redis:: Redis tutorial

Redis PHP List(列表) 實(shí)例

<?php
 //連接本地的 Redis 服務(wù)
 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379);
 echo "Connection to server successfully";
 //存儲(chǔ)數(shù)據(jù)到列表中
 $redis->lpush("tutorial-list", "Redis");
 $redis->lpush("tutorial-list", "Mongodb");
 $redis->lpush("tutorial-list", "MySQL");
 // 獲取存儲(chǔ)的數(shù)據(jù)并輸出
 $arList = $redis->lrange("tutorial-list", 0 ,5);
 echo "Stored string in redis";
 print_r($arList);
?>

執(zhí)行腳本,輸出結(jié)果為:

Connection to server sucessfully
Stored string in redis
Mysql
Mongodb
Redis

Redis PHP Keys 實(shí)例

<?php
 //連接本地的 Redis 服務(wù)
 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379);
 echo "Connection to server successfully";
 // 獲取數(shù)據(jù)并輸出
 $arList = $redis->keys("*");
 echo "Stored keys in redis:: ";
 print_r($arList);
?>

執(zhí)行腳本,輸出結(jié)果為:

Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list

分享到:
標(biāo)簽:PHP Redis
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定