One, Redis installation
Yum Install-y epel-release
Yum install-y gcc jemalloc-devel wget
Cd/usr/local/src
wget Https://codeload.github.com/antirez/redis/tar.gz/2.8.21-O redis-2.8.21.tar.gz
Tar XF redis-2.8.21.tar.gz
CD redis-2.8.21
Make
Make Prefix=/usr/local/redis Install
Mkdir/usr/local/redis/etc
wget http://www.apelearn.com/study_v2/.redis_conf-O/usr/local/redis/etc/redis.conf 2>/dev/null # download config file
wget http://www.apelearn.com/study_v2/.redis_init-O/etc/init.d/redis 2>/dev/null # Download startup script
Useradd-s/sbin/nologin Redis
Mkdir/usr/local/redis/var
chmod 777/usr/local/redis/var
chmod 755/etc/init.d/redis
Chkconfig--add Redis
Chkconfig Redis on
/etc/init.d/redis start
[Email protected] ~]#/usr/local/redis/bin/redis-cli # connection Redis127.0.0.1:6379>
Second, the basic operation of Redis
To install the Redis module:
Cd/usr/local/srcwget--no-check-certificate https://pypi.python.org/packages/source/r/redis/ Redis-2.8.0.tar.gztar XF redis-2.8.0.tar.gz mv redis-2.8.0 python-redis-2.8.0cd Python-redis-2.8.0/python setup.py Install
Python connects and operates Redis:
In [1]:ImportRedisin [2]: R = Redis. Redis (host='127.0.0.1', port=6379)#Redis. Redis () is used to connect RedisIn [3]: R.set ('name','Tom')#Set () for setting the key valueOut[3]: Truein [4]:PrintR.get ('name')#get () to get the key valueTom
Other Operation reference:
[[Email protected] ~]#/USR/LOCAL/REDIS/BIN/REDIS-CLI//Connect to Redis127.0.0.1:6379> keys *//keys are used to view all keys or to blur matches, such as keys my*127.0.0.1:6379> exists name//exists is used to determine if there is a key, return 1, no return 0127.0.0.1:6379>delName//delused to delete the specified key, successfully returned 1, failed to return 0127.0.0.1:6379> expire name 100//expire used to set the specified key for how long to expire, in seconds127.0.0.1:6379> persist name//persist used to cancel the expiration of a specified key127.0.0.1:6379> TTL name//TTL is used to see how long the specified key has expired, and returns-2 means no key, returns-1 indicates that the expiration time is not set, and how long does the other indicate that it expires127.0.0.1:6379> Select 0//Select to choose which library127.0.0.1:6379> Move Name 2//Move is used to move the specified key under which library127.0.0.1:6379> Randomkey//Randomkey is used to randomly return a key127.0.0.1:6379> Rename K1 k2//rename for renaming keys127.0.0.1:6379> type name//type to view the types of the specified key127.0.0.1:6379> dbsize//Dbsize used to return the number of keys in the current database127.0.0.1:6379> Info//Info Returns the status information for the Redis database127.0.0.1:6379> FLUSHDB//flushdb for emptying all keys in the current database127.0.0.1:6379> Flushall//Flushall for emptying all keys in all databases
Third, Redis connection pool
1, what is the connection pool
When the application to the database for the operation of the increase and deletion, you need to connect to the database, but if the application needs to frequently connect to the database for add and delete operations, it is necessary to constantly create the connection, which is time-consuming and resource-intensive operation, but also easy to create a security risk to the database. Therefore, when the application starts to establish enough database connections, and these connections into a connection pool, when the application needs to connect to the database for add and delete operations, and then use the connection pool connection, which can ensure faster database read and write speed, but also more secure and reliable.
The database connection pool operates as follows:
(1) Create connection pool when program initializes
(2) Request an available connection to the connection pool when used
(3) After use, return the connection to the connection pool
(4) When the program exits, disconnect all connections and release resources
2. Python uses a Redis connection pool
In [5]:ImportRedisin [6]: Pool = Redis. ConnectionPool (host='127.0.0.1', port=6379)#Redis. ConnectionPool () to create a connection poolIn [7]: R = Redis. Redis (Connection_pool=pool)#through Redis. Redis () Connect to Connection poolIn [8]: R.set ('name','Tom')#Performing Redis OperationsOut[8]: Truein [9]: R.get ('name') out[9]:'Tom'
Day24--nosql profile, Redis service build, Redis connection pool, Redis pipeline