redis storage system
Article Directory
1. What is redis
REmote DIctionary Server (Redis) is a key-value storage systems. Use written in ANSI C, BSD comply with the agreement and support network, based on the persistence of memory can log type, Key-Value database, and provides multi-lingual API (python, php, java ...).
It is often called a data structure of the server, since the value (value) can be a string (String), a hash (the Map), list (List), set (sets), and an ordered collection (sorted sets) and other types.
Redis advantage
- High performance - Redis can read as fast as 110,000 times / s, write speed is 81000 times / s
- Rich data types - Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets the data type of the operation
- Atomic - Redis All operations are atomic, meaning that either succeed or fail completely executed execution. It is a single atomic operation. A plurality of operation also supports transaction, i.e. atomicity, and EXEC instructions by wrap MULTI
- Redis data backup data backup support, i.e., master-slave mode,
- When persistence Redis support data, data in memory can be saved to disk, restart can be loaded again using
2.redis installation
- Install python package
Method 1: pycharm in setting the pip downloaded directly
Method 2: Download command (linux of the shell)
Su #切换到超级用户
Cd /usr/local/python3/bin
./pip install redis
./pip3 install -U pip #升级pip
- Installation services redis
Download: http: //redis.io/download, download the latest stable version
tar zxf redis-5.0.3.tar.gz
cd redis-5.0.3/
yum install gcc -y
make
make install
cd utils/
./install_server.sh
netstat -antlp|grep 6379
redis-cli
/etc/init.d/redis_6379 restart
3.redis connection
Method 1: This method automatically disconnects a connection
import redis
import time
redis.Redis(host='localhost',port=6379)
conn = redis.Redis()
conn.set('name','westos',3)
print(conn.get('name'))
print('等待3秒...')
time.sleep(3)
print(conn.get('name'))
Method 2: not disconnected automatically with respect to the first method is more stable
pool = redis.ConnectionPool(host='localhost',port=6379)
conn = redis.Redis(connection_pool=pool)
# 默认返回bytes类型 如果需要转换 要解码为utf=8编码格式
conn.set('name','粉条',4)
print(conn.get('name').decode('utf-8'))
print('等待3s....')
time.sleep(3)
print(conn.get('name'))
4.redis specific use
Upload / Get / Clear
import redis
import time
pool = redis.ConnectionPool(host='localhost',port=6379)
conn = redis.Redis(connection_pool=pool)
conn.set('name','fentiao')
conn.set('age',10)
conn.set('score',100)
# 获取所有的key值
print(conn.keys())
print(len(conn.keys()))
# 当前redis数据库中的数据条数
print(conn.dbsize())
# 删除指定的key-value值
conn.delete('score')
print('正在删除key....')
print(conn.get('score'))
print('清除前:',conn.keys())
# 清除redis里面的所有k-v
conn.flushall()
print('清除后:',conn.keys())