当前位置:首页>开发>正文

php怎么将数据导入redis

2024-06-29 18:38:09 互联网 未知 开发

php怎么将数据导入redis?

php怎么将数据导入redis

开始在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服务

//连接本地的Redis服务

$redis=newRedis()

$redis->connect(127.0.0.1,6379)

echo"Connectiontoserversucessfully"

//查看服务是否运行

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

?>

执行脚本,输出结果为:

Connectiontoserversucessfully

Serverisrunning:PONG

RedisPHPString(字符串)实例

//连接本地的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(列表)实例

//连接本地的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实例

//连接本地的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