php怎么将数据导入redis

发布时间:2024-10-31 09:53:01

开端在PHP中利用Redis前,要确保曾经安装了redis效劳及PHPredis驱动,且你的呆板上能正常利用PHP。

PHP安装redis扩大年夜

/usr/local/php/bin/phpize#php安装后的道路

./configure--with-php-config=/usr/local/php/bin/php-config

make&&makeinstall

修改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或apache。检查phpinfo信息,就能看到redis扩大年夜。

连接到redis效劳

<?php

//连接当地的Redis效劳

$redis=newRedis();

$redis->connect(\'127.0.0.1\',6379);

echo\"Connectiontoserversucessfully\";

//检查效劳能否运转

echo\"Serverisrunning:\".$redis->ping();

?>

履行剧本,输出成果为:

Connectiontoserversucessfully

Serverisrunning:PONG

RedisPHPString(字符串)实例

<?php

//连接当地的Redis效劳

$redis=newRedis();

$redis->connect(\'127.0.0.1\',6379);

echo\"Connectiontoserversucessfully\";

//设置redis字符串数据

$redis->set(\"tutorial-name\",\"Redistutorial\");

//获取存储的数据并输出

echo\"Storedstringinredis::\".jedis.get(\"tutorial-name\");

?>

履行剧本,输出成果为:

Connectiontoserversucessfully

Storedstringinredis::Redistutorial

RedisPHPList(列表)实例

<?php

//连接当地的Redis效劳

$redis=newRedis();

$redis->connect(\'127.0.0.1\',6379);

echo\"Connectiontoserversucessfully\";

//存储数据到列表中

$redis->lpush(\"tutorial-list\",\"Redis\");

$redis->lpush(\"tutorial-list\",\"Mongodb\");

$redis->lpush(\"tutorial-list\",\"Mysql\");

//获取存储的数据并输出

$arList=$redis->lrange(\"tutorial-list\",0,5);

echo\"Storedstringinredis::\"

print_r($arList);

?>

履行剧本,输出成果为:

Connectiontoserversucessfully

Storedstringinredis::

Redis

Mongodb

Mysql

RedisPHPKeys实例

<?php

//连接当地的Redis效劳

$redis=newRedis();

$redis->connect(\'127.0.0.1\',6379);

echo\"Connectiontoserversucessfully\";

//获取数据并输出

$arList=$redis->keys(\"*\");

echo\"Storedkeysinredis::\"

print_r($arList);

?>

履行剧本,输出成果为:

Connectiontoserversucessfully

Storedstringinredis::

tutorial-name

tutorial-list