目次
インストール
Redis サーバーに接続します
Redis PHP 文字列の例
Redis PHP リストの例
Redis PHP キーの例
ホームページ php教程 php手册 Redis PHP接続操作

Redis PHP接続操作

Jun 16, 2016 am 08:39 AM

インストール

PHP プログラムで Redis を使用するには、まず Redis 用の PHP ドライバーと PHP インストールがマシン上にセットアップされていることを確認する必要があります。 PHP チュートリアルを参照して、マシンに PHP をインストールする方法を学ぶことができます。次に、Redis 用の PHP ドライバーのセットアップを見てみましょう。

phpredis を github リポジトリ (https://github.com/nicolasff/phpredis) からダウンロードする必要があります。ダウンロードが完了したら、ファイルを phpredis ディレクトリに解凍します。この拡張機能を Ubuntu にインストールするには、次の図に示すコマンドを使用してインストールします。

<span class="pln">
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install

</span>
ログイン後にコピー

次に、「modules」フォルダーの内容をコピーして PHP 拡張機能ディレクトリに貼り付け、php.ini に次の行を追加します。

<span class="pln">
extension = redis.so

</span>
ログイン後にコピー

これで、Redis と PHP のインストールが完了しました。

Redis サーバーに接続します

<span class="pln">
<?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();
?>

</span>
ログイン後にコピー

プログラムが実行されると、次の結果が生成されます:

<span class="pln">
Connection to server sucessfully
Server is running: PONG

</span>
ログイン後にコピー

Redis PHP 文字列の例

<span class="pln">
<?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");
?>

</span>
ログイン後にコピー

プログラムが実行されると、次の結果が生成されます:

<span class="pln">
Connection to server sucessfully
Stored string in redis:: Redis tutorial

</span>
ログイン後にコピー

Redis PHP リストの例

<span class="pln">
<?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);
?>

</span>
ログイン後にコピー

プログラムが実行されると、次の結果が生成されます:

<span class="pln">
Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql

</span>
ログイン後にコピー

Redis PHP キーの例

<span class="pln">
<?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);
?>

</span>
ログイン後にコピー

プログラムが実行されると、次の結果が生成されます:

<span class="pln">
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list</span>
ログイン後にコピー
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)