NoSQLデータベースのケースコンバット--Redisクライアントの展開--php、python

序文

この環境は、Redis学習環境を構築するためのCentos 7.8システムに基づいています。
具体的な構築については、Redis-5.0.9環境の展開を参照してください


1つ、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!';
  }
?>

ここに画像の説明を挿入

セットリスト

[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

2、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