【脚本语言系列】关于PythonNoSQL数据库处理redis,你需要知道的事

如何使用redis

# -*- coding:utf-8 -*-
import redis
# string
conn = redis.Redis()
conn.keys("*")
conn.set("Allen","Moore")
conn.set("Age", 24)
conn.get("Allen")
conn.get("Age")
conn.set("Stature",160)
conn.get("Stature")
conn.set("Weight","160")
conn.setnx("Weight","160")
conn.get("Weight")
conn.getset("Weight","80kg")
conn.getrange("Weight", -1,-2)
conn.setrange("Weight",0, "160Jin")
conn.mset({"Hobby":"Program","Major":"Automation"})
conn.delete("Weight")
conn.incr("Age")
conn.incr("Age", 10)
conn.decr("Age")
conn.decr("Age",15)
conn.set("Weight","80")
conn.incrbyfloat("Weight")
conn.incrbyfloat("Weight",0.5)
# -*- coding:utf-8 -*-
import redis
# list only with string
conn = redis.Redis()
print conn.lpush("bigmarket","pear")
print conn.lpush("bigmarket","apple","banana")
print conn.linsert("bigmarket","before","cantaloupe","peach")
print conn.linsert("bigmarket","after","ctarine","honeydew")
print conn.lset("bigmarket",2,"plum")
print conn.rpush("bigmarket","grape")
print conn.lindex("bigmarket",3)
print conn.lrange("bigmarket",0,2)
print conn.ltrim("bigmarket",1,4)             
print conn.lrange("bigmarket",0,-1)
5
7
-1
-1
True
8
apple
['banana', 'apple', 'plum']
True
['apple', 'plum', 'apple', 'plum']
# -*- coding:utf-8 -*-
import redis
# hash likes dict
conn = redis.Redis()
print conn.hmset("song",{"do":"a beer","re":"about a beer"})
print conn.hset("song","fa","a heart to a thing")
print conn.hget("song","fa")
print conn.hmget("song","do","re")
print conn.hkeys("song")
print conn.hvals("song")
print conn.hlen("song")
print conn.hgetall("song")
print conn.hsetnx("song","so","a hobby to a dinner")
True
1
a heart to a thing
['a beer', 'about a beer']
['do', 're', 'fa']
['a beer', 'about a beer', 'a heart to a thing']
3
{'do': 'a beer', 'fa': 'a heart to a thing', 're': 'about a beer'}
1
# -*- coding:utf-8 -*-
import redis
# set same with set
conn = redis.Redis()
print conn.sadd("store","apple","banana","lemon","melon")
print conn.scard("store")
print conn.smembers("store")
print conn.srem("store","banana")
print conn.smembers("store")
print conn.sadd("new_store","banana","lemon","melon","orange")
print conn.sinter("store","new_store")
print conn.sinterstore("supermarket","store","new_store")
print conn.smembers("supermarket")
print conn.sunion("store","new_store")
print conn.sunionstore("littlemarket","store","new_store")
print conn.smembers("littlemarket")
print conn.sdiff("store","new_store")
print conn.sdiffstore("diffmarket","store","new_store")
print conn.smembers("diffmarket")
1
4
set(['melon', 'lemon', 'apple', 'banana'])
1
set(['melon', 'lemon', 'apple'])
0
set(['melon', 'lemon'])
2
set(['melon', 'lemon'])
set(['orange', 'melon', 'lemon', 'apple', 'banana'])
5
set(['orange', 'melon', 'lemon', 'apple', 'banana'])
set(['apple'])
1
set(['apple'])
# -*- coding:utf-8 -*-
import time
# zset same with zset
# rank

# second index

# time series
now = time.time()
print conn.zadd("logins","Allen",now)
print conn.zadd("logins","Moore",now+(5*60))
print conn.zadd("logins","Aaron",now+(2*60*60))
print conn.zadd("logins","Mose",now+(24*60*60))
print conn.zrank("logins","Aaron")
print conn.zscore("logins","Aaron")
print conn.zrange("logins",0,-1)
print conn.zrange("logins",0,-1, withscores = True)
1
1
1
1
5
1497590410.06
['smeagol', 'Allen', 'sauron', 'Moore', 'bilbo', 'Aaron', 'treebeard', 'Mose']
[('smeagol', 1497583134.492), ('Allen', 1497583210.058), ('sauron', 1497583434.492), ('Moore', 1497583510.058), ('bilbo', 1497590334.492), ('Aaron', 1497590410.058), ('treebeard', 1497669534.492), ('Mose', 1497669610.058)]
# -*- coding:utf-8
import time
# bit same with bitset
days = ["2017-02-25","2017-02-26","2017-02-27"]
big_spender = 1089
tire_kicker = 40459
late_joiner = 550212
conn.setbit(days[0],big_spender, 1)
conn.setbit(days[0],tire_kicker, 1)
conn.setbit(days[1],big_spender, 1)
conn.setbit(days[2],big_spender, 1)
conn.setbit(days[2],late_joiner, 1)
for day in days:
    print conn.bitcount(day)
print conn.getbit(days[1], tire_kicker)
print conn.bitop("and", "everyday", *days)
print conn.bitcount("everyday")
print conn.getbit("everyday",big_spender)
print conn.bitop("or","alldays",*days)
print conn.bitcount("alldays")
2
1
2
0
68777
1
1
68777
3
import time
# expiration date
key = "now see it"
print conn.set(key, "but not for long")
print conn.expire(key, 5)
print conn.ttl(key)
conn.get(key)
time.sleep(6)
print conn.get(key)
True
True
5
None

什么是redis

redis是基于内存的数据结构服务器。
redis存储数据到磁盘,可以存储不限于字符串的多种数据结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值