


Detailed explanation of how to install and use redis in Python
This article mainly introduces the method of installing and using redis in Python, analyzes the specific steps of installation and configuration, and analyzes the specific usage skills of the redis database in detail with examples. Friends in need can refer to it
The example in this article describes how to install and use redis in python. Share it with everyone for your reference, the details are as follows:
1. Installation
Okay, I admit that I only know the simplest installation:
sudo apt-get install redis-server
python support package: (Actually, it’s just a file, you can just get it and use it)
sudo apt-get install python-redis
2. Configuration
Configure it. The default configuration file is: "/etc/redis/redis.conf"
Binding ip:
"bind 127.0.0.1″ -> "bind 10.0.1.7″
Change the disk synchronization to non-synchronization or synchronization every second. If it is synchronized all the time, it will be too slow:
"appendfsync always" -> "appendfsync no"
Check the background execution Whether to open:
"daemonize yes"
Or anything else you want to set, for example:
Connection timeout: "timeout 300"
Run level : "loglevel notice" (I personally think the default one is pretty good. Unless there is a big exception, there is no need to change it to debug)
3. Use
#! /usr/bin/env python #coding=utf-8 import redis print redis.__file__ # 连接,可选不同数据库 r = redis.Redis(host='10.0.1.7', port=6379, db=1) # ------------------------------------------- # 看信息 info = r.info() for key in info: print "%s: %s" % (key, info[key]) # 查数据库大小 print '\ndbsize: %s' % r.dbsize() # 看连接 print "ping %s" % r.ping() # 选数据库 #r.select(2) # 移动数据去2数据库 #r.move('a',2) # 其他 #r.save('a') # 存数据 #r.lastsave('a') # 取最后一次save时间 #r.flush() #刷新 #r.shutdown() #关闭所有客户端,停掉所有服务,退出服务器 # #-------------------------------------------- # 它有四种类型: string(key,value)、list(序列)、set(集合)、zset(有序集合,多了一个顺序属性) # 不知道你用的哪种类型? # print r.get_type('a') #可以告诉你 # ------------------------------------------- # string操作 print '-'*20 # 塞数据 r['c1'] = 'bar' #或者 r.set('c2','bar') #这里有个 getset属性,如果为True 可以在存新数据时将上次存储内容同时搞出来 print 'getset:',r.getset('c2','jj') #如果你想设置一个递增的整数 每执行一次它自加1: print 'incr:',r.incr('a') #如果你想设置一个递减的整数 please: print 'decr:',r.decr('a') # 取数据 print 'r['']:',r['c1'] #或者 print 'get:',r.get('a') #或者 同时取一批 print 'mget:',r.mget('c1','c2') #或者 同时取一批 它们的名字(key)很像 而恰好你又不想输全部 print 'keys:',r.keys('c*') #又或者 你只想随机取一个: print 'randomkey:',r.randomkey() # 查看一个数据有没有 有 1 无0 print 'existes:',r.exists('a') # 删数据 1是删除成功 0和None是没这个东西 print 'delete:',r.delete('cc') # 哦对了 它是支持批量操作的 print 'delete:',r.delete('c1','c2') # 其他 r.rename('a','c3') #呃.改名 r.expire('c3',10) #让数据10秒后过期 说实话我不太明白么意思 r.ttl('c3') #看剩余过期时间 不存在返回-1 #-------------------------------- # 序列(list)操作 print '-'*20 # 它是两头通的 # 塞入 r.push('b','gg') r.push('b','hh') # head 属性控制是不是从另一头塞 r.push('b','ee',head=True) # 看长度 print 'list len:',r.llen('b') # 列出一批出来 print 'list lrange:',r.lrange('b',start=0,end=-1) # 取出一位 print 'list index 0:',r.lindex('b',0) # 修剪列表 #若start 大于end,则将这个list清空 print 'list ltrim :',r.ltrim('b',start=0,end=3) #只留 从0到3四位 # 排序 # 这可是个大工程 #-------------------------------- # 集合(set)操作 # 塞数据 r.sadd('s', 'a') # 判断一个set长度为多少 不存在为0 r.scard('s') # 判断set中一个对象是否存在 r.sismember('s','a') # 求交集 r.sadd('s2','a') r.sinter('s1','s2') #求交集并将结果赋值 r.sinterstore('s3','s1','s2') # 看一个set对象 r.smembers('s3') # 求并集 r.sunion('s1','s2') # 阿 我想聪明的你已经猜到了 #求并集 并将结果返回 r.sunionstore('ss','s1','s2','s3') # 求不同 # 在s1中有,但在s2和s3中都没有的数 r.sdiff('s1','s2','s3') r.sdiffstore('s4','s1','s2')# 这个你懂的 # 取个随机数 r.srandmember('s1') #------------------------------------- #zset 有序set #'zadd', 'zcard', 'zincr', 'zrange', 'zrangebyscore', 'zrem', 'zscore' # 分别对应 #添加, 数量, 自加1,取数据,按照积分(范围)取数据,删除,取积分 # 我靠 你玩死我了 redis! # 今天在实验中,我尝试插入一条zset类型数据: r1.zset(u'www.liyi99.com','liwu',3) # 插入成功 # 我继续插入 r1.zset(u'www.liyi99,com',u'\u9001\u793c',5) #报错: #UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) #这次插入的是礼物的中文词 unicode编码 #为什么会失败那,这条数据是我从redis里面取出来然后没做任何修改再插入的阿 #redis返回和接受的数据类型都是unicode编码的阿 #好吧,我们再次插入试试 #再次插入 r1.zset('www.liyi99.com',u'\u9001\u793c',5) #成功了 #插入 r1.zset('www.liyi99.com','礼物',5) #依然成功,跟入redis.py 1024行 return self.send_command('ZADD %s %s %s\r\n%s\r\n' % ( key, score, len(member), member)) # 哦 万恶的编码转换! #不过取的时候,不论第一个是何种类型的数据都无关系
For more detailed articles on how to install python and use redis, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
