一 介绍
1、memcached & redis是什么?
NoSQl数据库,数据存到内存,读取速度快 应用场景:页面缓存,好处如下 1、减少数据库压力,提升访问速度 2、在数据挂掉的情况下,仍能保证业务正常运行一段时间,提升安全性
2、memcached与redis区别
1、类型:
2、持久化:
3、memcached并未过时
www.oschina.net/news/26691/memcached-timeout
二 安装与基本操作
1、Redis安装
`#### 1、官网:https://redis.io/
2、redis默认不正式支持windows,到这里下载windows版本
https://github.com/MicrosoftArchive/redis/releases`
2、命令行基本操作
1、命令行链接
`redis-cli
redis-cli -h host -p port -a password`
2、基本操作
`默认有16个数据库,编号从0-15
select 1 #切换到1号库
keys #查看所有的key
keys n #查看所有n开头的key
flushdb #清空redis
set key value #添加key=value
randomkey #随机取出一个key
type key #查看key的类型`
3、在Python中的两种链接方式
方式一
`#redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。
import redis
client=redis.Redis(host=’127.0.0.1′,port=6379)
client.set(‘name’,’egon’)
v= client.get(‘name’)
print(v,type(v)) #b’egon’ <class ‘bytes’>`
方式二
`#redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池。
import redis
pool=redis.ConnectionPool(host=’127.0.0.1′,port=6379,max_connections=100)
client=redis.Redis(connection_pool=pool)
client.set(‘name’,’egon’)
v= client.get(‘name’)
print(v,type(v)) #b’egon’ <class ‘bytes’>`
4、选择数据库
View Code
三 常用操作
1、String 操作
View Code
2、Hash 操作
View Code
3、List 操作
View Code
4、Set 操作
View Code
5、Sort Set 操作
View Code
6、其他常用操作
View Code
7、管道
View Code
8、发布订阅
monitor.py
订阅者们
发布者们
四 Redis可视化工具
https://redisdesktop.com/download