
- Redis 基礎知識
- Redis - 主頁
- Redis - 概述
- Redis - 環境
- Redis - 配置
- Redis - 資料型別
- Redis 命令
- Redis - 命令
- Redis - 鍵
- Redis - 字串
- Redis - 雜湊
- Redis - 列表
- Redis - 集合
- Redis - 有序集合
- Redis - HyperLogLog
- Redis - 釋出訂閱
- Redis - 事務
- Redis - 指令碼
- Redis - 連線
- Redis - 伺服器
- Redis 高階
- Redis - 備份
- Redis - 安全
- Redis - 基準測試
- Redis - 客戶端連線
- Redis - 管道
- Redis - 分割槽
- Redis - Java
- Redis - Php
- Redis 有用資源
- Redis - 快速指南
- Redis - 有用資源
- Redis - 討論
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
廣告