【Python之旅】第七篇(二):Redis使用基础

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

0.说明

    由于学习开发监控软件的需要,因此需要使用到Redis,这里简单介绍。

    注意,使用的环境为:Ubuntu 15.10


1.安装

    可以采用源码安装,也可以采用apt-get来安装,都比较简单。


2.启动

    由于采用的是源码安装的方式,所以直接进入src目录,启动redis-server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis- 2.8 . 9 /src$ ./redis-server 
[ 12681 16  Oct  00 : 06 : 52.964  # Warning: no config file specified, using the  default  config. In order to specify a config file  use  ./redis-server /path/to/redis.conf
[ 12681 16  Oct  00 : 06 : 52.967  # You requested maxclients of  10000  requiring at least  10032  max file descriptors.
[ 12681 16  Oct  00 : 06 : 52.968  # Redis can't  set  maximum open files to  10032  because of OS error: Operation not permitted.
[ 12681 16  Oct  00 : 06 : 52.968  # Current maximum open files  is  1024 . maxclients has been reduced to  4064  to compensate  for  low ulimit. If you need higher maxclients increase  'ulimit -n' .
                 _._                                                  
            _.-``__  '' -._                                             
       _.-``    `.  `_.   '' -._           Redis  2.8 . 9  ( 00000000 / 0 64  bit
   .-`` .-```.  ```\/    _.,_  '' -._                                   
  (    '      ,       .-`  | `,    )     Running  in  stand alone mode
  |`-._`-...-` __...-.``-._| '` _.-' |     Port:  6379
  |    `-._   `._    /     _.-'    |     PID:  12681
   `-._    `-._  `-./  _.- '    _.-'                                   
  |`-._`-._    `-.__.- '    _.-' _.-'|                                  
  |    `-._`-._        _.- '_.-'     |           http: //redis.io        
   `-._    `-._`-.__.- '_.-'     _.-'                                   
  |`-._`-._    `-.__.- '    _.-' _.-'|                                  
  |    `-._`-._        _.- '_.-'     |                                  
   `-._    `-._`-.__.- '_.-'     _.-'                                   
       `-._    `-.__.- '    _.-'                                       
           `-._        _.-'                                           
               `-.__.-'                                               
 
[ 12681 16  Oct  00 : 06 : 52.974  # Server started, Redis version  2.8 . 9
[ 12681 16  Oct  00 : 06 : 52.974  # WARNING overcommit_memory  is  set  to  0 ! Background save may fail under low memory condition. To fix  this  issue add  'vm.overcommit_memory = 1'  to /etc/sysctl.conf and then reboot or run the command  'sysctl vm.overcommit_memory=1'  for  this  to take effect.
[ 12681 16  Oct  00 : 06 : 52.976  * DB loaded from disk:  0.002  seconds
[ 12681 16  Oct  00 : 06 : 52.977  * The server  is  now ready to accept connections on port  6379

    出现上面所示的提示,说明已经正常启动了redis。


3.交互式操作

    进入src目录,运行redis-cli即可进入redis交互界面:

1
2
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis- 2.8 . 9 /src$ ./redis-cli
127.0 . 0.1 : 6379 >

    基本操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#查看帮助
127.0 . 0.1 : 6379 > help  set
 
   SET key value [EX seconds] [PX milliseconds] [NX|XX]
   summary: Set the string value of a key
   since:  1.0 . 0
   group: string
 
#创建key-value
127.0 . 0.1 : 6379 set  name xpleaf
OK
 
#获得key对应的value
127.0 . 0.1 : 6379 get  name
"xpleaf"
 
#创建有时间的key-value
127.0 . 0.1 : 6379 set  name2 CL ex  5
OK
 
#创建列表
127.0 . 0.1 : 6379 > lpush stu_list xpleaf yonghaoye CL
(integer)  3
127.0 . 0.1 : 6379 > lpush stu_list CLYYH
(integer)  4
 
#获取列表内容
127.0 . 0.1 : 6379 > lrange stu_list  1  4
1 "CL"
2 "yonghaoye"
3 "xpleaf"
127.0 . 0.1 : 6379 > lrange stu_list  0  4
1 "CLYYH"
2 "CL"
3 "yonghaoye"
4 "xpleaf"
 
#删除key-value或其它数据类型
127.0 . 0.1 : 6379 > del name
(integer)  1


3.在Python交互器中使用redis

  • 要使用Python来操作Redistribute,则需要安装Python与Redis通信的接口:

1
apt- get  install python-redis
  • 在交互器中连接Redis数据库:

1
2
>>>  import  redis
>>> r = redis.Redis( '127.0.0.1' , port= 6379 , db= 0 )
  • 基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#查看所有的key
>>> r.keys()
[ 'YourKey' 'stu_list' 'k1' 'k3' ]
 
#创建key-value
>>> r. set ( 'xpleaf' 'xpleaf' )
True
 
#获取key所对应的value
>>> r[ 'xpleaf' ]
'xpleaf'
>>> r. get ( 'xpleaf' )
'xpleaf'
 
#保存Python中的字典到Redis数据库中
>>>  import  json
>>> myDict = { 'name' 'xpleaf' 'age' 21 'loving' 'cl' }
>>> r[ 'Py_myDict' ] = json.dumps(myDict)
>>> 
>>> r[ 'Py_myDict' ]
'{"age": 21, "name": "xpleaf", "loving": "cl"}'
 
#取出保存在Redis数据库中的Python字典
>>> a = json.loads(r[ 'Py_myDict' ])
>>> a
{u 'age' 21 , u 'name' : u 'xpleaf' , u 'loving' : u 'cl' }
>>> a[ 'name' ]
u 'xpleaf'
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
5月前
|
NoSQL 网络协议 Redis
Python redis 使用(笔记)2
Python redis 使用(笔记
48 4
|
6月前
|
NoSQL Redis 索引
python调用redis 五大基本类型
python调用redis 五大基本类型
|
6月前
|
存储 NoSQL Redis
Python分享之redis(2)
Python分享之redis(2)
|
4月前
|
存储 NoSQL Redis
Redis 简介 + Python 操作发布订阅
Redis 简介 + Python 操作发布订阅
|
6天前
|
NoSQL MongoDB Redis
Python与NoSQL数据库(MongoDB、Redis等)面试问答
【4月更文挑战第16天】本文探讨了Python与NoSQL数据库(如MongoDB、Redis)在面试中的常见问题,包括连接与操作数据库、错误处理、高级特性和缓存策略。重点介绍了使用`pymongo`和`redis`库进行CRUD操作、异常捕获以及数据一致性管理。通过理解这些问题、易错点及避免策略,并结合代码示例,开发者能在面试中展现其技术实力和实践经验。
125 8
Python与NoSQL数据库(MongoDB、Redis等)面试问答
|
1月前
|
存储 JSON NoSQL
Redis与Python的完美结合:实现高效数据交互和应用场景全解析
Redis与Python的完美结合:实现高效数据交互和应用场景全解析
115 0
|
1月前
|
存储 NoSQL Redis
如何在Python中操作Redis数据库
如何在Python中操作Redis数据库
29 0
|
1月前
|
NoSQL Redis Python
python flask 使用 redis 写一个例子给我
python flask 使用 redis 写一个例子给我
38 4
|
2月前
|
缓存 NoSQL Redis
如何在Python中使用Redis或Memcached进行缓存?
如何在Python中使用Redis或Memcached进行缓存?
28 2
|
6月前
|
存储 NoSQL Redis
Python分享之redis
Python分享之redis

热门文章

最新文章