How to operate redis in python and redis in python

Source: Internet
Author: User

How to operate redis in python and redis in python

This article describes how to operate redis in python. Share it with you for your reference. The details are as follows:

#! /Usr/bin/python # coding = utf-8import redisclass CRedis: def _ init _ (self): self. host = 'localhost' self. port = 6379 self. db = 0 self. r = redis. redis (host = self. host, port = self. port, db = self. db) #1. strings type and operation # set the value of key to string type value def set (self, key, value): return self. r. set (key, value) # set the value of the key to a value of the string type. If the key already exists, 0 is returned. nx means not exist def setnx (self, key, value): return self. r. setnx (key, value) # set the value corresponding to the key to a string type value, and specify the validity period of this key value def setex (self, key, time, value ): return self. r. setex (key, time, value) # Set the sub-string of the value of the specified key # setrange name 8 gmail.com # Where 8 refers to the subscript of 8 (including 8) def setrange (self, key, num, value): return self. r. setrange (key, num, value) # obtain the sub-string def getrange (self, key, start, end) of the value of the specified key: return self. r. getrange (key, start, end) # mget (list) def get (self, key): if isinstance (key, list): return self. r. mget (key) else: return self. r. get (key) # Delete def remove (self, key): return self. r. delete (key) # auto increment def incr (self, key, default = 1): if (1 = default): return self. r. incr (key) else: return self. r. incr (key, default) # self-subtraction def decr (self, key, default = 1): if (1 = default): return self. r. decr (key) else: return self. r. decr (key, default) #2. hashes type and operation # obtain session information by email def hget (self, email): return self. r. hget ('session', email) # use email as the unique identifier, and add the user session def hset (self, email, content): return self. r. hset ('set', email, content) # obtain all data in the session hash table def hgetall (self): return self. r. hgetall ('session ') # Delete hashes def hdel (self, name, key = None): if (key): return self. r. hdel (name, key) return self. r. hdel (name) # clear the current db def clear (self): return self. r. flushdb () #3. lists type and operation # suitable for email queue # Add the string element def lpush (self, key, value) to the header of the list corresponding to the key: return self. r. lpush (key, value) # delete an element from the end of the list and return the delete element def lpop (self, key): return self. r. plush (key) if _ name _ = '_ main _': r = CRedis ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.