NoSQL 数据库案例实战 --Redis 客户端部署--php、python

前言

本环境是基于 Centos 7.8 系统构建Redis学习环境
具体构建,请参考 Redis-5.0.9环境部署


一、php客户端

1、环境部署

安装php-redis

[root@reids_source_code ~]# yum  install php-redis -y

准备php环境

[root@reids_source_code ~]# yum install httpd php -y

测试连接

vim /var/www/html/connect.php
<?php
  //连接到本地Redis服务
  $redis = new Redis();
  $redis->connect('192.168.5.12',6379);
  if ($redis->connect('192.168.5.12',6379))
  {
    
    
  echo 'Connection to server sucessfully!';
  echo 'Server is running:' .$redis->ping();
  }
  else
  {
    
    
  echo 'Connection to server failure!';
  }
?>

http://192.168.5.12/connect.php
连接成功!!!
在这里插入图片描述

2、实战案例

设置字符串、查看字符串的值

[root@reids_source_code ~]# vim /var/www/html/connect.php
<?php
  //连接到本地Redis服务
  $redis = new Redis();
  $redis->connect('192.168.5.12',6379);
  if ($redis->connect('192.168.5.12',6379))
  {
    
    
  echo 'Connection to server sucessfully!<br>';
  //查看redis服务是否允许
  echo 'Server is running:' .$redis->ping();
  //设置字符串数据
  $redis->set('Linux','Linux Redis server test!');
  //获取字符串的值
  echo 'Store string in redis:<br>' .$redis->get('Linux');
  }

  else
  {
    
    
  echo 'Connection to server failure!';
  }
?>

在这里插入图片描述

设置list

[root@reids_source_code ~]# vim /var/www/html/list_test.php
<?php
  //连接本地的 Redis 服务
  $redis = new Redis();
  $redis->connect('192.168.5.12', 6379);
  echo "Connection to server sucessfully<br>";
  //存储数据到列表中
  $redis->lpush("test-list", "Redis");
  $redis->lpush("test-list", "Mongodb");
  $redis->lpush("test-list", "Oracle");
  $redis->lpush("test-list", "Mysql");
  // 获取存储的数据并输出
  $output_list = $redis->lrange("test-list", 0 ,5);
  echo "Stored string in redis";
  print_r($output_list);
?>

在这里插入图片描述

获取键并输出

[root@reids_source_code ~]# vim /var/www/html/get_values_output.php
<?php
  //连接本地的 Redis 服务
  $redis = new Redis();
  $redis->connect('192.168.5.12', 6379);
  echo "Connection to server sucessfully<br>";
  // 获取数据并输出
  $values_output = $redis->keys("*");
  echo "Stored keys in redis:: ";
  print_r($values_output);
?>

在这里插入图片描述
注:参考地址:https://www.runoob.com/redis/redis-php.html

二、python客户端

安装python-redis

[root@reids_source_code ~]# yum install python-redis -y

编辑python脚本

[root@reids_source_code ~]# vim /python_redis.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#载入模块
import redis
#连接redis数据库
r = redis.Redis(host='127.0.0.1', port=6379,db=0)
#往redis中写数据
r.set('nvshen', 'hehe')
r['diaosi'] = 'yy'
r.set('xueba', 'xuexi')
r['xuezha'] = 'wan'
#查看对应的值
print 'nvshen', r.get('nvshen')
#查看数据库中有多少个key,多少条数据
print r.dbsize()
#将数据保存到硬盘中(保存时阻塞)
r.save()
#查看键值是否存在
print r.exists("doubi")
#列出所有键值
print r.keys()
#删除键值对应的数据
print r.delete('diaosi')
print r.delete('xuezha')
#删除当前数据库所有数据
r.flushdb()

运行python脚本

[root@reids_source_code ~]# chmod +x /python_redis.py
[root@reids_source_code ~]# /python_redis.py 
nvshen hehe
8
False
['xueba', 'test=list', 'xuezha', 'diaosi', 'test-list', 'nvshen', 'Linux', 'foo']
1
1

猜你喜欢

转载自blog.csdn.net/XY0918ZWQ/article/details/113801408