python-Connect to Redis and operate

Source: Internet
Author: User

First turn on Redis's external connection

[Email protected]:~$ sudo vim/etc/redis/redis.conf

Comment out the bind 127.0.0.1.

Then restart Redis

Sudo/etc/init.d/redis-server restart

This way, the Ubuntu Redis machine can be connected

Connect and manipulate
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import Redisr = Redis. Redis (host= ' 192.168.220.144 ', port=6379) r.set (' name ', ' John ') print (R.get (' name '))

Run results

Results of Redis on Ubuntu

Connection pool

Python operation Redis, the operation once requested a connection, the operation is completed disconnect, the connection pool to the Redis connection request into the pool, easy to operate, to avoid each establishment, release the cost of the connection

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import Redispool = Redis. ConnectionPool (host= ' 192.168.220.144 ', port=6379) R = Redis. Redis (Connection_pool=pool) r.set (' age ', *) print (R.get (' age '))

Run results

Results of Redis on Ubuntu

Pipeline

Execute multiple commands in a single request, you can use the pipeline implementation to execute multiple commands in a single request

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import Redispool = Redis. ConnectionPool (host= ' 192.168.220.144 ', port=6379, db=2) R = Redis. Redis (connection_pool=pool) pipe = R.pipeline (transaction=true) pipe.set (' name ', ' John ') pipe.set (' age ', 22) Pipe.execute ()

Run results

python-Connect to Redis and operate

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.