Python Operation Redis

Source: Internet
Author: User
Tags redis server

Installing Redis-py
sudo Install Redis
Connecting to a database
= Redis. Strictredis (host='localhost', port=6379, db=0)

Note: Use R=reids. Redis (host= ' localhost ', port=6379,db=0) is also available. Difference: Redis is a subclass of Strictredis for backwards compatibility with older versions of Redis-py. The official recommendation is to use Strictredis.

Simple Redis operation
>>> R.set ('name','Jihite') True>>> R.set ('score', -) True>>>R.keys () ['score','name']>>> R.get ('name')'Jihite'>>> R.get ('score')' -'>>> R.delete ('score')1>>>R.keys () ['name']>>>r.save () True>>>R.keys () ['name']>>>r.flushdb () True>>>R.keys () []
Pipeline operation

The pipeline is a subclass of the base class where Redis caches multiple server commands in a single request, which greatly improves the performance of bulk commands by reducing the number of repeated TCP database packets between server-clients.

Example

>>> p =R.pipeline ()>>> P.hset ('MySet','name','Jihite') Pipeline<connectionpool<connection6379, db=0>>>>>> P.hset ('MySet','score', -) Pipeline<connectionpool<connection6379, db=0>>>>>> P.hget ('MySet','name') Pipeline<connectionpool<connection6379, db=0>>>>>>P.execute () [1L,1L,'Jihite']>>> R.hget ('MySet','name')'Jihite'>>> R.hget ('MySet','score')' -'

Note: Pipeline commands can be written together, such as:

>>> p =R.pipeline ()>>> P.set ('name','Jihite'). Set ('score', -). Set ('School','Bupt'). Get ('score'). Execute () [True, True, true ,' -']
Connection Pools

Redis-py Manage connections to Redis Server by connecting to the pool, by default each Redis connection instance automatically creates its own link pool, which can take advantage of an already existing link pool.

>>> pool = Redis. ConnectionPool (host='localhost', port=6379, db=0)>> > r = Redis. Redis (Connection_pool=pool)

Python Operation Redis

Related Article

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.