當前位置:首頁 » 編程語言 » php訪問redis

php訪問redis

發布時間: 2022-05-26 10:31:22

Ⅰ 如何利用php訪問帶有密碼的Redis

1. 設置Redis密碼,以提供遠程登陸
打開redis.conf配置文件,找到requirepass,然後修改如下:
requirepass yourpassword

yourpassword就是redis驗證密碼,設置密碼以後發現可以登陸,但是無法執行命令了。
命令如下:
redis-cli -h 127.0.0.1 -p 6379//啟動redis客戶端,並連接伺服器
keys * //輸出伺服器中的所有key

報錯如下
(error) ERR operation not permitted
這時候你可以用授權命令進行授權,就不報錯了
命令如下:
auth youpassword

2. PHP訪問Redis

$redis = new Redis();
$conn = $redis->connect('localhost', 6379);
$auth = $redis->auth('20160601'); //設置密碼
var_mp($auth);
$redis->set('access_token', "123213213213213213");
$redis->set('expired_time', 1464344863);

var_mp($redis->get("access_token"));
var_mp($redis->get("expired_time"));

Ⅱ php 使用redis鎖限制並發訪問類示例

本文介紹了php
使用redis鎖限制並發訪問類,並詳細的介紹了並發訪問限制方法。
1.並發訪問限制問題
對於一些需要限制同一個用戶並發訪問的場景,如果用戶並發請求多次,而伺服器處理沒有加鎖限制,用戶則可以多次請求成功。
例如換領優惠券,如果用戶同一時間並發提交換領碼,在沒有加鎖限制的情況下,用戶則可以使用同一個換領碼同時兌換到多張優惠券。
偽代碼如下:
if
A(可以換領)

B(執行換領)

C(更新為已換領)
D(結束)
如果用戶並發提交換領碼,都能通過可以換領(A)的判斷,因為必須有一個執行換領(B)後,才會更新為已換領(C)。因此如果用戶在有一個更新為已換領之前,有多少次請求,這些請求都可以執行成功。
2.並發訪問限制方法
使用文件鎖可以實現並發訪問限制,但對於分布式架構的環境,使用文件鎖不能保證多台伺服器的並發訪問限制。
Redis是一個開源的使用ANSI
C語言編寫、支持網路、可基於內存亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API。
本文將使用其setnx方法實現分布式鎖功能。setnx即Set
it
N**ot
eX**ists。
當鍵值不存在時,插入成功(獲取鎖成功),如果鍵值已經存在,則插入失敗(獲取鎖失敗)
RedisLock.class.PHP
<?php
/**
*
Redis鎖操作類
*
Date:
2016-06-30
*
Author:
fdipzone
*
Ver:
1.0
*
*
Func:
*
public
lock
獲取鎖
*
public
unlock
釋放鎖
*
private
connect
連接
*/
class
RedisLock
{
//
class
start
private
$_config;
private
$_redis;
/**
*
初始化
*
@param
Array
$config
redis連接設定
*/
public
function
__construct($config=array()){
$this->_config
=
$config;
$this->_redis
=
$this->connect();
}
/**
*
獲取鎖
*
@param
String
$key
鎖標識
*
@param
Int
$expire
鎖過期時間
*
@return
Boolean
*/
public
function
lock($key,
$expire=5){
$is_lock
=
$this->_redis->setnx($key,
time()+$expire);
//
不能獲取鎖
if(!$is_lock){
//
判斷鎖是否過期
$lock_time
=
$this->_redis->get($key);
//
鎖已過期,刪除鎖,重新獲取
if(time()>$lock_time){
$this->unlock($key);
$is_lock
=
$this->_redis->setnx($key,
time()+$expire);
}
}
return
$is_lock?
true
:
false;
}
/**
*
釋放鎖
*
@param
String
$key
鎖標識
*
@return
Boolean
*/
public
function
unlock($key){
return
$this->_redis->del($key);
}
/**
*
創建redis連接
*
@return
Link
*/
private
function
connect(){
try{
$redis
=
new
Redis();
$redis->connect($this->_config['host'],$this->_config['port'],$this->_config['timeout'],$this->_config['reserved'],$this->_config['retry_interval']);
if(empty($this->_config['auth'])){
$redis->auth($this->_config['auth']);
}
$redis->select($this->_config['index']);
}catch(RedisException
$e){
throw
new
Exception($e->getMessage());
return
false;
}
return
$redis;
}
}
//
class
end
?>
demo.php
<?php
require
'RedisLock.class.php';
$config
=
array(
'host'
=>
'localhost',
'port'
=>
6379,
'index'
=>
0,
'auth'
=>
'',
'timeout'
=>
1,
'reserved'
=>
NULL,
'retry_interval'
=>
100,
);
//
創建redislock對象
$oRedisLock
=
new
RedisLock($config);
//
定義鎖標識
$key
=
'mylock';
//
獲取鎖
$is_lock
=
$oRedisLock->lock($key,
10);
if($is_lock){
echo
'get
lock
success<br>';
echo
'do
sth..<br>';
sleep(5);
echo
'success<br>';
$oRedisLock->unlock($key);
//
獲取鎖失敗
}else{
echo
'request
too
frequently<br>';
}
?>
測試方法:
打開兩個不同的瀏覽器,同時在A,B中訪問demo.php
如果先訪問的會獲取到鎖
輸出
get
lock
success
do
sth..
success
另一個獲取鎖失敗則會輸出request
too
frequently
保證同一時間只有一個訪問有效,有效限制並發訪問。
為了避免系統突然出錯導致死鎖,所以在獲取鎖的時候增加一個過期時間,如果已超過過期時間,即使是鎖定狀態都會釋放鎖,避免死鎖導致的問題。
源碼下載地址:點擊查看

Ⅲ 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總是提示超時

伺服器上的連接把IP改成本地IP:127.0.0.1試試看。

Ⅳ 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('當前操作的緩存伺服器配置文件不存在');
}希望能幫到你,我還在後盾網學習呢,有不會的可以問我,一會有空回答你。(^ω^)

Ⅵ php訪問redis擴展應該使用哪個版本

5.3以上的版本,現在基本都是5.6以上版本了吧?更多的是應該考慮以後升級迭代的成本和系統的穩定性成本吧?
你可以去後盾人平台看看,裡面的東西不錯

Ⅶ php連接redis是什麼服務類型

要在PHP程序中使用Redis,首先需要確保Redis的PHP驅動程序和PHP安裝設置在機器上。可以查看PHP教程教你如何在機器上安裝PHP。現在,讓我們來看看一下如何設置Redis的PHP驅動程序。
需要從github上資料庫:https://github.com/nicolasff/phpredis下載phpredis。下載完成以後,將文件解壓縮到phpredis目錄。在Ubuntu上安裝這個擴展,可使用如下圖所示的命令來安裝。
cdphpredis
sudophpize
sudo./configure
sudomake
sudomakeinstall
現在,復制和粘貼「moles」文件夾的內容復制到PHP擴展目錄中,並在php.ini中添加以下幾行。
extension=redis.so
現在Redis和PHP安裝完成。
連接到Redis伺服器
<?php
//
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//
echo"Serverisrunning:".$redis->ping();
?>
當執行程序時,會產生下面的結果:
Connectiontoserversucessfully
Serverisrunning:PONG
Redis的PHP字元串實例
<?php
//
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//setthedatainredisstring
$redis->set("tutorial-name","Redistutorial");
//Getthestoreddataandprintit
echo"Storedstringinredis::".$redis.get("tutorial-name");
?>
當執行程序時,會產生下面的結果:
Connectiontoserversucessfully
Storedstringinredis::Redistutorial
Redis的PHP列表示例
<?php
//
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//storedatainredislist
$redis->lpush("tutorial-list","Redis");
$redis->lpush("tutorial-list","Mongodb");
$redis->lpush("tutorial-list","Mysql");
//Getthestoreddataandprintit
$arList=$redis->lrange("tutorial-list",0,5);
echo"Storedstringinredis::"
print_r($arList);
?>
當執行程序時,會產生下面的結果:
Connectiontoserversucessfully
Storedstringinredis::
Redis
Mongodb
Mysql
Redis的PHP鍵例
<?php
//
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//Getthestoredkeysandprintit
$arList=$redis->keys("*");
echo"Storedkeysinredis::"
print_r($arList);
?>
當執行程序時,會產生下面的結果:
Connectiontoserversucessfully
Storedstringinredis::
tutorial-name
tutorial-list

Ⅷ php 連接redis,怎麼判斷Redis是否掛掉

一般鏈接redis,如果鏈接不上,或者redis掛掉,都會發生超時,你可以設置超時時間短一點,比如5秒。如果5秒鏈接不上則不連接了,繼續往下,不影響整體代碼運行。
<?php
$redis = new \Redis();
$redis->connect($config['host'],$config['port'], $config['timeout']);一般來說,你可以多去後盾人學習,這樣相關的問題都能夠得到解決

Ⅸ php 連接redis,怎麼判斷Redis是否掛掉

一般鏈接redis,如果鏈接不上,或者redis掛掉,都會發生超時,你可以設置超時時間短一點,比如5秒。如果5秒鏈接不上則不連接了,繼續往下,不影響整體代碼運行。

<?php
$redis=newRedis();
$redis->connect($config['host'],$config['port'],$config['timeout']);
$redis->ping();//檢測當前鏈接狀態,返回PONG或者拋出異常。

Ⅹ 為什麼php調用redis 返回+ok

Redis::__construct構造函數
$redis = new Redis();
connect, open 鏈接redis服務
參數
host: string,服務地址
port: int,埠號
timeout: float,鏈接時長 (可選, 默認為 0 ,不限鏈接時間)
注: 在redis.conf中也有時間,默認為300
pconnect, popen 不會主動關閉的鏈接
參考上面
setOption 設置redis模式
getOption 查看redis設置的模式
ping 查看連接狀態
KEY相關操作
DEL
移除給定的一個或多個key。
如果key不存在,則忽略該命令。
時間復雜度:
O(N),N為要移除的key的數量。
移除單個字元串類型的key,時間復雜度為O(1)。
移除單個列表、集合、有序集合或哈希表類型的key,時間復雜度為O(M),M為以上數據結構內的元素數量。
返回值:
被移除key的數量。這樣呢我自己學習在後盾人看見的,老師講的很詳細,希望對你有用😊(ง •̀_•́)ง努力

熱點內容
c與java編譯過程 發布:2025-02-12 21:47:47 瀏覽:373
python的面向對象 發布:2025-02-12 21:46:10 瀏覽:613
醫學影像存儲解決方案 發布:2025-02-12 21:45:58 瀏覽:976
股票走勢預測演算法 發布:2025-02-12 21:45:06 瀏覽:769
游戲lua腳本 發布:2025-02-12 21:45:01 瀏覽:918
怎麼下載安卓版的光子助手 發布:2025-02-12 21:43:45 瀏覽:454
oppor7s怎麼取消鎖屏密碼 發布:2025-02-12 21:43:31 瀏覽:595
我的世界伺服器甜蜜小鎮 發布:2025-02-12 21:41:08 瀏覽:75
ftp影響數據交換 發布:2025-02-12 21:27:18 瀏覽:387
編譯原理與實現pdf 發布:2025-02-12 21:27:14 瀏覽:42