python爬虫(二十六)------消息队列之kv数据库Redis(window版本)介绍

介绍:
与RabbitMQ对比
Redis: 轻量级,高并发,延迟敏感。适用于即时数据分析、秒杀计数器、缓存等
RabbitMQ: 比Redis更专业的重量级,高并发,异步消息队列。适用于批量数据异步处理、并行任务串行化,高负载任务的负载均衡等
常用与银行交易系统

选择:
只要求速度,不要求100%的准确,就可以用成本较低的Redis作为消息队列
安装:
https://blog.csdn.net/qq_32444825/article/details/80718650
解压后:在cmd开启服务端:

redis>redis-server.exe
[8496] 04 Apr 19:13:50.264 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server.exe /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 8496
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[8496] 04 Apr 19:13:50.269 # Server started, Redis version 3.2.100
[8496] 04 Apr 19:13:50.270 * DB loaded from disk: 0.000 seconds
[8496] 04 Apr 19:13:50.270 * The server is now ready to accept connections on port 6379

然后在打开另一个cmd窗口开启客户端:

redis>redis-cli.exe
127.0.0.1:6379>

基本操作:

非常适合随机访问,如果想要随机访问,没有select * from tablename的机制,只能

127.0.0.1:6379> keys *
1) "test_key"
2) "MDM001302"
3) "LoginInfoToken"
4) "LoginInfo"


127.0.0.1:6379> select10
(error) ERR unknown command 'select10'
127.0.0.1:6379> select 10
OK
127.0.0.1:6379[10]> select 0
OK
127.0.0.1:6379> keys *
1) "test_key"
2) "MDM001302"
3) "LoginInfoToken"
4) "LoginInfo"
127.0.0.1:6379> get "test_key"
"test_value"
127.0.0.1:6379> set 'tom' 'jery'
OK
127.0.0.1:6379> get 'tom'
"jery"
127.0.0.1:6379>

在python中操作redis客户端前你的服务端需要处于运行状态
redis在命令行提供的命令在python中基本都有

import redis
#redis默认端口:127.0.0.1:6379
r=redis.Redis(host='localhost',port=6379,db=1)
r.set('k1','v1')
r.set('k2','v2')
print(r.get('v1'))
print(r.keys())
print(r.dbsize())
  
#但这样一次一次的访问命令开销太大所以要用pipeline一次
p=r.pipeline()
p.set('k3','v3')
p.set('k4','v4')
p.incr('www')incr一次添加一个k,v=1,第二次变成2
p.incr('www')
p.execute()
print(r.get('www'))

猜你喜欢

转载自blog.csdn.net/qq_41228218/article/details/89036193