Latest web development tutorials

PHP는 레디 스를 사용

설치

당신이 PHP에서 레디 스를 사용하기 전에, 우리는 서비스와 PHP 설치 레디 스의 레디 스 드라이브 있는지 확인해야하고 기계는 정상 PHP를 사용할 수 있습니다. 의는 PHP의 레디 스 드라이버를 설치하자 다운로드 주소 : https://github.com/phpredis/phpredis/releases을 .

레디 스를 설치하는 PHP는 확장

다음 작업은 전체 카탈로그 phpredis을 다운로드해야합니다 :

$ wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz
$ cd phpredis-2.2.7                      # 进入 phpredis 目录
$ /usr/local/php/bin/phpize              # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install

당신이 PHP7 버전의 경우, 지정된 분기를 다운로드해야합니다 :

git clone -b php7 https://github.com/phpredis/phpredis.git

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 또는 아파치를 다시 시작합니다. 은 phpinfo 정보보기, 당신은 레디 스 확장을 볼 수 있습니다.

PHP는 레디 스를 사용

레디 스 서비스에 연결

<?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

레디 스 PHP 문자열 (문자열) 예

<?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:: " . $redis->get("tutorial-name");
?>

스크립트를 실행, 출력은 :

Connection to server sucessfully
Stored string in redis:: Redis tutorial

레디 스 PHP 목록 (리스트) 예

<?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

레디 스 PHP 키 예

<?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