Redis-php
提供:Dev Guides
Redis-PHP
PHPプログラムでRedisの使用を開始する前に、Redis PHPドライバーとPHPがマシンにセットアップされていることを確認する必要があります。 マシンにPHPをインストールするためのPHPチュートリアルを確認できます。
インストール
ここで、Redis PHPドライバーのセットアップ方法を確認しましょう。
githubリポジトリhttps://github.com/nicolasff/phpredisからphpredisをダウンロードする必要があります。 ダウンロードしたら、ファイルをphpredisディレクトリに抽出します。 Ubuntuで、次の拡張機能をインストールします。
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install
次に、「modules」フォルダーの内容をコピーしてPHP拡張ディレクトリに貼り付け、 php.ini に次の行を追加します。
extension = redis.so
これで、Redis PHPのインストールが完了しました
Redisサーバーに接続する
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//check whether server is running or not
echo "Server is running: ".$redis->ping();
?>
プログラムが実行されると、次の結果が生成されます。
Connection to server sucessfully
Server is running: PONG
Redis PHP文字列の例
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//set the data in redis string
$redis->set("tutorial-name", "Redis tutorial");
//Get the stored data and print it
echo "Stored string in redis:: " .$redis→get("tutorial-name");
?>
上記のプログラムを実行すると、次の結果が生成されます。
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis phpリストの例
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//store data in redis list
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
//Get the stored data and print it
$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キーの例
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//Get the stored keys and print it
$arList = $redis->keys("*");
echo "Stored keys in redis:: "
print_r($arList);
?>
プログラムが実行されると、次の結果が生成されます。
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list