Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods for python to operate redis?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the python operation of redis method of what related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you will have some gains after reading this python method of operating redis, let's take a look at it.

Python operates redis, using connection pooling:

Redis-py uses connection pool to manage all connections to a redis server, avoiding the overhead of establishing and releasing connections each time. By default, each Redis instance maintains its own connection pool. You can directly establish a connection pool, and then as a parameter Redis, so that multiple Redis instances can share a connection pool.

Def getcoon (): pool= redis.ConnectionPool (host='192.168.25.126', port=6379, password='123456', db=0) coon = redis.Redis (connection_pool=pool) coon.set ('key',' value') res = coon.get ('key') return rescoon.set (' sea', 'hah', ex=30) # Expiration time unit sset (name, value, ex=None, px=None, nx=False, xx=False) is set in Redis. Default If it does not exist, it will be created; if it exists, it will be modified. Parameter: ex-Expiration time (seconds) px-Expiration time (milliseconds) nx-if set to True, the current set operation executes xx only if name does not exist-if set to True, the current set operation is performed only if name exists

Redis uses connection pooling operation

Class OPRedis (object): def _ init__ (self): if not hasattr (OPRedis, 'pool'): OPRedis.getRedisCoon () # create redis connection self.coon = redis.Redis (connection_pool=OPRedis.pool) @ staticmethod def getRedisCoon (): OPRedis.pool = redis.ConnectionPool (host=redisInfo [' host'], password=redisInfo ['password'], port=redisInfo [' port'] Db=redisInfo ['db']) "" string type {' key':'value'} redis operation "" def setredis (self, key, value, time=None): # if time: res = self.coon.setex (key, value, time) else: res = self.coon.set (key, value) return res def getRedis (self) Key): res = self.coon.get (key). Decode () return res def delRedis (self, key): res = self.coon.delete (key) return res "hash type {'name': {' key':'value'}} redis operation "" def setHashRedis (self, name, key, value): res = self.coon.hset (name, key, value) return res def getHashRedis (self, name, key=None): # determine whether key is empty, not empty, and get value of a key within the specified name If it is empty, get all the value if key corresponding to name: res = self.coon.hget (name, key) else: res = self.coon.hgetall (name) return res def delHashRedis (self, name, key=None): if key: res = self.coon.hdel (name, key) else: res = self.coon.delete (name) return res

RedisInfo configuration

RedisInfo = {"host": '192.168.1.112, "password:' 123456," port ": 6379," db ": 0} this is the end of the article on" what are the ways for python to operate redis? "Thank you for reading! I believe you all have a certain understanding of "what are the methods of python operating redis". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report