【Python之旅】第七篇(二):Redis使用基础
0.说明
由于学习开发监控软件的需要,因此需要使用到Redis,这里简单介绍。
注意,使用的环境为:Ubuntu 15.10
1.安装
可以采用源码安装,也可以采用apt-get来安装,都比较简单。
2.启动
由于采用的是源码安装的方式,所以直接进入src目录,启动redis-server:
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。
#查看帮助
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[p]
>>> import redis >>> r = redis.Redis('127.0.0.1', port=6379, db=0)
- [p]基本操作
#查看所有的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'[/code]阅读更多
- 【Python之旅】第七篇(二):Redis使用基础
- 【Python之旅】第七篇(三):使用Redis订阅服务
- python-math-基础使用
- python基础:用apply使用元祖和列表调用函数
- python入门基础教程03 Python开发环境基本使用
- 使用Python连接redis(redis作MQ使用)_Tister的空间_百度空间
- python基础教程之类class定义使用方法
- .Net程序员之Python基础教程学习----字典的使用 [Third Day]
- 在Python中使用Redis数据库
- redis的pythonAPI的使用
- 在linux下使用redis的基础操作(一)
- Python 使用 Xlrd/xlwt 操作 Excel 分类: python基础学习 2014-03-17 12:06 958人阅读 评论(0) 收藏
- Python基础教程之第3章 使用字符串
- python基础知识(2)---使用字符串
- 使用Python操作Redis
- easy_install的安装和使用(Python基础)
- python使用基础(win10)
- python操作mysq-redis脚本及使用nohup或crontab任务后台执行
- Python学习入门基础教程(learning Python)--1.2.1 Python输出语句print基本使用 .
- 使用生成器返回fibs列表 分类: python Module python基础学习 2013-10-28 18:19 283人阅读 评论(0) 收藏