PHPリンクredisメソッドコード

小云云
リリース: 2023-03-20 20:28:01
オリジナル
3533 人が閲覧しました

PHP プログラムで Redis を使用する前に、Redis PHP ドライバーと PHP 環境がマシンにインストールされていることを確認する必要があります。まず、コンピュータに PHP をインストールし、環境を構成します。この記事では、Redis にリンクする PHP のメソッド コードを紹介します。お役に立てれば幸いです。

インストール

それでは、Redis PHP ドライバーをセットアップする方法を見てみましょう。
github ライブラリ => http://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 のインストールが完了しました。

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   //check whether server is running or not 
   echo "Server is running: ".$redis->ping(); ?>
ログイン後にコピー

PHP

を使用してRedisサーバーに接続します プログラムが実行されると、次の結果が生成されます。

Connection to server sucessfully 
Server is running: PONG
ログイン後にコピー

Redis PHP文字列の例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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(&#39;127.0.0.1&#39;, 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(&#39;127.0.0.1&#39;, 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
ログイン後にコピー

関連する推奨事項:

Redis の PHP 操作の説明

php+redis の簡単な例 共有

PHP での Redis のいくつかの使用方法

以上がPHPリンクredisメソッドコードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!