Python Redis安装及基本操作

详细Linux中安装redis安装参考博客:https://blog.csdn.net/Dream_ya/article/details/81837298

一、Redis安装


操作系统:rhel7.3
python版本:python3.6

1、Linux中安装redis服务

redis是一个高性能的key-value数据库,我们需要先安装好其服务,用于python通过redis模块进行连接操作!!!

[root@python ~]# wget http://download.redis.io/releases/redis-4.0.8.tar.gz
[root@python ~]# yum install -y gcc
[root@python ~]# tar xf redis-4.0.8.tar.gz
[root@python redis-4.0.8]# cd redis-4.0.8
[root@python redis-4.0.8]# make && make install
[root@python redis-4.0.8]# cd utils/
[root@python utils]# ./install_server.sh          ###一直按回车即可

### 查看是否有端口
[root@python ~]# netstat -lntup|grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      979/redis-server 12 

2、安装python的redis模块

官网下载地址:https://pypi.org/project/redis/#files

(1)机器联网
[root@python ~]# pip3 install redis
(2)离线安装
[root@python ~]# wget https://files.pythonhosted.org/packages/4a/1b/9b40393630954b54a4182ca65a9cf80b41803108fcae435ffd6af57af5ae/redis-3.0.1.tar.gz
[root@python ~]# tar xf redis-3.0.1.tar.gz
[root@python ~]# cd redis-3.0.1/
[root@python redis-3.0.1]# python setup.py install

3、测试

[root@python redis-3.0.1]# python
Python 3.6.0 (default, Dec  3 2018, 00:59:25) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis                           ###导入没报错即安装成功

二、连接redis


1、直接连接

端口默认:6379,如果改变了,可以在redis.Redis中设置port来指定端口!!!

import redis
conn = redis.Redis(host='127.0.0.1')
conn.set("user","dream")
print (conn.get("user"))

结果:
b'dream'

这样的方式虽然可以进行连接,但是每次都需要重新连接,建立socket,我们可以通过建立连接池来解决!!!

2、连接池

import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set("user","dreamya")
print (r.get("user"))

结果:
b'dreamya'

三、redis操作(string)


官网命令链接:https://redis.io/commands/set

1、SET

(1)python环境
set( name, value, ex=None, px=None, nx=False, xx=False)
	EX seconds -- Set the specified expire time, in seconds.(过期时间s)
	PX milliseconds -- Set the specified expire time, in milliseconds.(过期时间ms)
	NX -- Only set the key if it does not already exist.(key不存在就设置,否则不修改)
	XX -- Only set the key if it already exist.(key存在就进行修改,否则不操作)
(2)Linux环境

SET key value [EX seconds] [PX milliseconds] [NX|XX]

[root@python ~]# redis-cli
127.0.0.1:6379> set name dream                 ###设置name(key)为dream(value)
OK
127.0.0.1:6379> get name
"dream"

127.0.0.1:6379> set name dream ex 2            ###设置存在时间为2S
OK
127.0.0.1:6379> get name
"dream"
127.0.0.1:6379> get name
(nil)

127.0.0.1:6379> set user dream nx               ###不存在就进行设置,存在就不进行修改
(nil)
127.0.0.1:6379> get user
"dream"
127.0.0.1:6379> set user dreamya nx
(nil)
127.0.0.1:6379> get user
"dream"
        
127.0.0.1:6379> set user dreamya xx            ###存在不设置
OK
127.0.0.1:6379> get user
"dreamya"
127.0.0.1:6379> set user dream xx
OK
127.0.0.1:6379> get user
"dream"

2、SETEX(string)

(1)python环境
setex( name, value, time)                   ###过期时间(s)
import redis,time
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set("user",'dream',ex=2)
print (r.get("user"))
time.sleep(2)
print (r.get("user"))

结果:
b'dream'
None
(2)Linux环境

SETEX key seconds value

127.0.0.1:6379> SETEX user 10 "dreamya"
OK
127.0.0.1:6379> TTL user
(integer) 10
127.0.0.1:6379> GET user

3、SETNX

(1)python环境
setnx( name, value)                        ###不存在就进行设置,存在就不进行修改
import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set("user",'dream')
r.setnx("user",'dreamya')
r.setnx("test",'dreamya')
print (r.get("user"))
print (r.get("test"))

结果:
b'dream'
b'dreamya'
(2)Linux环境

SETNX key value

127.0.0.1:6379> FLUSHALL                  ###删除所有key
OK
127.0.0.1:6379> KEYS *                    ###列出所有key
(empty list or set)
127.0.0.1:6379> set user dream 
OK
127.0.0.1:6379> SETNX user dreamya
(integer) 0
127.0.0.1:6379> SETNX test dream
(integer) 1
127.0.0.1:6379> get user
"dream"
127.0.0.1:6379> get test
"dream"

4、SETRANGE(string)

(1)python环境
### offset:抵消(写数字指定位置),根据后面value进行替换前面指定的位置
setrange(name, offset, value)
import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set("num",'123456789')
print (r.get("num"))
r.setrange("num",3,'dream')
print (r.get("num"))

结果:
b'123456789'
b'123dream9'
(2)Linux环境

SETRANGE key offset value

127.0.0.1:6379> SET num "123456789"
OK
127.0.0.1:6379> SETRANGE num 3 "dream"             ###把从第4位开始替换为后面的字符
(integer) 9
127.0.0.1:6379> get num                           
"123dream9"

5、STRLEN(string)

(1)python环境
strlen(name)        ###查询字符长度,0即为key不存在
import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set("num","123456789")
print (r.get("num"))
print (r.get("not exist"))
print (r.strlen('num'))
print (r.strlen('not exist'))

结果:
b'123456789'
None
9
0
(2)Linux环境

STRLEN key

127.0.0.1:6379> SET num "123"
OK
127.0.0.1:6379> STRLEN num
(integer) 3
127.0.0.1:6379> STRLEN noexist
(integer) 0

6、PSETEX(string)

(1)python环境
psetex( name, time_ms, value)               ###time_ms:存在时间,ms为单位
(2)Linux环境

PSETEX key milliseconds value

127.0.0.1:6379> PSETEX user 2000 dreamya           ###设置时间为2000ms(2s)
OK
127.0.0.1:6379> get user
"dreamya"
127.0.0.1:6379> get user
(nil)

7、GET(string)

(1)python环境
get( name)                        ###获取name(即key)的value(值)
(2)Linux环境

GET key

127.0.0.1:6379> set user dream
OK
127.0.0.1:6379> get user
"dream"

8、MGET(string)

(1)python环境
mget( keys, *args)              ###同时获取多个key
import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
r.set('user','dream')
r.set('a','1')
print (r.mget("user","a"))

结果:
[b'dream', b'1']
(2)Linux环境

MGET key [key …]

127.0.0.1:6379> set user dream
OK
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> MGET user a
1) "dream"
2) "1"

9、MSET(string)

mset(*args, **kwargs)              ###批量设置,传入字典即可
import redis
pool = redis.ConnectionPool(host='127.0.0.1')
r = redis.Redis(connection_pool=pool)
d = {"a": 1, "b": 2}
d1 = dict(c=3, d=4)
r.mset(d)
r.mset(d1)
print (r.mget(['a', 'b', 'c', 'd']))

结果:
[b'1', b'2', b'3', b'4']
(2)Linux环境

MSET key value [key value …]

127.0.0.1:6379> MSET user dream a 1
OK
127.0.0.1:6379> MGET user a
1) "dream"
2) "1"

你可能感兴趣的:(Python)