8178°c
The previous article simply said the Linux installation Redis process, here is a little walkthrough with Python code, first to install Redis with PIP:
sudo pip install Redis
Then you can call it in Python and write some basics below, and write it according to the introduction of the Redis combat. The complex use of Redis is used later to learn, and the code is more intuitive:
Import Redis
Cache = Redis. Strictredis (host= ' localhost ', port=6379)
#1. Simple get and set operations
Print U ' ====set operation: '
Cache.set (' Blog:title ', U ' the5fire Technology blog ')
Print Cache.get (' Blog:title ')
#真实应用场景, batch set and get
For I in range (10):
Cache.mset ({
' Blog:post:%s:title '% i:u ' article%s title '% i,
' Blog:post:%s:content '% i:u ' article%s body '% i
})
Post_list = []
For I in range (10):
Post = Cache.mget (' blog:post:%s:title '% i, ' blog:post:%s:content '% i)
If post:
Post_list.append (POST)
For title, content in Post_list:
Print title, content
#2, hashed types of operations
Print U ' ====hashed operation: '
Cache.hset (' Blog:info ', ' title ', U ' the5fire's Technical blog ')
Cache.hset (' blog:info ', ' url ', u ' http://www.the5fire.com ')
Blog_info_title = Cache.hget (' blog:info ', ' title ')
Print Blog_info_title
Blog_info = Cache.hgetall (' Blog:info ')
Print Blog_info
#同样hashed类型的set和get也可以进行批量操作
Cache.hmset (' Blog:info ', {
' title ': ' The5fire blog ',
' URL ': ' http://www.the5fire.com ',
})
Blog_info1 = Cache.hmget (' blog:info ', ' title ', ' URL ')
Print Blog_info1
#3, lists types of operations
Print U ' ====lists operation: '
Cache.lpush (' blog:tags ', ' python ')
Cache.lpush (' blog:tags ', ' Linux ')
tags = cache.lrange (' Blog:tags ', 0, 2)
Print tags
#对应的还有rpush, Lpop,rpop, see more red pills for Redis combat
#4, sets types of operations
Print U ' ====sets operation: '
Cache.sadd (' Blog:category:python ', ' 001 ')
Cache.sadd (' Blog:category:python ', ' 002 ')
#cache. Sadd (' Blog:category:python ', ' 001 ', ' 002 ')
Print cache.smembers (' Blog:category:python ')
Cache.srem (' Blog:category:python ', ' 001 ')
Print cache.smembers (' Blog:category:python ')
Python Simple Operation Redis