Redis缓存应用、分片应用测试

数据类型:如果只用string类型,则相当于memcached


string类型及操作


set name chenhao
get name
setnx name chenhao


设置KEY有效期
setex name  10 chenhao


为KEY加子串
setrange name 8 wzgchen@gmail.com


一次设置多个 key 的值
mset name chen wan xiao 


获取 key 对应的 string 值
 get name


获取指定 key 的 value 值的子字符串
getrange name 0 6
getrange name -7 -1
getrange name 7 100
字符串左面下标是从 0 开始的


set age 20
incr age
incrby age 5
 decr age
 decrby age 5


给指定 key 的字符串值追加 value,
 append name @126.com




 取指定 key 的 value 值的长度
  strlen name
  
hash类型及操作
在redis中,hash数据类型存储的数据与mysql数据库中存储一条记录极为相似,是一个string类型的field和value的映射表,它特别适合用于存储对象,但字段值只能是字符串,不支持其他类型




设置 hash field 为指定值,如果 key 不存在,则先创建。
hset person name jack
hset person age 20
hset person sex famale


获取指定的 hash field
hget person name
hget person age


返回hash中所有的field和value
hgetall person


返回hash所有的field
hkeys person 


返回hash中所有的value
hvals person




 查看指定field是否存在。
 hexists person name 


删除指定的hash field。
 hdel person name


返回指定hash中field的数量
hlen person 






http://www.redis.net.cn/clients/
安装redis python客户端
pip install redis
pip install hiredis


import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')




安装redis python客户端,支持cluster 
pyredis Python Client with support for Redis Cluster. Currently only Python 3 is supported.


pip install python_redis


from pyredis import Client


client = Client(host="localhost")
client.bulk_start()
client.set('key1', 'value1)
client.set('key1', 'value1)
client.set('key1', 'value1)
client.bulk_stop()
[b'OK', b'OK', b'OK']


当作缓存使用:如果redis中没有,则读取数据库,并更新到redis中


[root@virtdb54 ~]# more reorder.py 
#!/usr/bin/python


import sys


import MySQLdb


import redis




re = redis.StrictRedis(host='192.168.2.55', port=7000, db=0)


key = re.get("top2salary")


if key != None:


        print "Load data from Memcache : %s,%s" % (key[0],key[1])


else:


    print "Updating memcached data from MySQL."


    conn = MySQLdb.connect (host = "192.168.2.61",


                            user = "root",


                            passwd = "5e4aroo2t",


                            db = "employees")


    cursor = conn.cursor()


    cursor.execute('select * from salaries order by salary limit 2')


    rows = cursor.fetchall()




    re.set('top2salary', rows)
    value = re.get('top2salary')
    print value








[root@virtdb54 ~]# more recount.py 
#!/usr/bin/python


import sys


import MySQLdb


import redis




re = redis.StrictRedis(host='192.168.2.55', port=7000, db=0)


key = re.get("count")


if key != None:


        print "Load data from Memcache : %s" % key


else:


    print "Updating memcached data from MySQL."


    conn = MySQLdb.connect (host = "192.168.2.61",


                            user = "root",


                            passwd = "5e4aroo2t",


                            db = "employees")


    cursor = conn.cursor()


    cursor.execute('select count(*) from salaries')


    rows = cursor.fetchone()[0]




    re.set('count', rows)
    value = re.get('count')
    print value








http://blog.youkuaiyun.com/xu470438000/article/details/42971091


首先先说前提:twemproxy作为老牌的redis集群方案,他确实在特定历史阶段实现了他的价值,但他肯定是不如现在的codis,具体codis哪好可以看很多文章介绍。
然后是官方cluster的优点,其实真的只有一个,就是没有proxy转发之后极限性能好,但绝大多数场景真的不重要




编译安装:
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar zxvf redis-3.2.1.tar.gz
cd /soft/redis-3.2.1
make 
make install
[root@node1 src]# which redis-cli
/usr/local/bin/redis-cli




Redis 做分片存储
安装:
pip install redis-shard




redis分布式写
[root@virtdb54 py]# more rehash.py 
#!/usr/bin/env python




from redis_shard.shard import RedisShardAPI


servers = [
{'name': 'server1', 'host': '192.168.2.55', 'port': 7000, 'db': 0},
{'name': 'server2', 'host': '192.168.2.55', 'port': 7001, 'db': 0},
{'name': 'server3', 'host': '192.168.2.55', 'port': 7002, 'db': 0},
]


client = RedisShardAPI(servers, hash_method='md5')


for i in range(1,10000):
    key = str(i)
    value = 'id' + str(i)
    client.set(key,value)
    result = client.get(key)
    print result




redis分布式读
[root@virtdb54 py]# more rehashget.py       
#!/usr/bin/env python




from redis_shard.shard import RedisShardAPI


servers = [{'name': 'server1', 'host': '192.168.2.55', 'port': 7000, 'db': 0},{'name': 'server2', 'host': '192.168.2.55', 'port': 7001, 'db': 0},
{'name': 'server3', 'host': '192.168.2.55', 'port': 7002, 'db': 0},]


client = RedisShardAPI(servers, hash_method='md5')


v = 0 


for i in range(1,10000):
    key = str(i)
    value = 'id' + str(i)
    try:
        result = client.get(key)
        
    except:
        print "connect failed"
        v = v + 1 


    if result == None:
        print "Not find key,update to redis"
        client.set(key,value)
    else:
        print result


per = (v+0.0)/10000


print "key miss: " + str(v)
print "key hit per: " + "%.2f%%" % (per * 100) 






    
统计redis中所有的KEY
echo "keys * "  |redis-cli -h 192.168.2.55 -p 7000 |wc -l
echo "keys * "  |redis-cli -h 192.168.2.55 -p 7001 |wc -l
echo "keys * "  |redis-cli -h 192.168.2.55 -p 7002 |wc -l




查看命令率:
echo "info"  |redis-cli -h 192.168.2.55 -p 7000 |grep hit |awk -F ":" '{print $2}'
echo "info"  |redis-cli -h 192.168.2.55 -p 7000 |grep miss |awk -F ":" '{print $2}'






[root@virtdb55 ~]# more rehit.sh 
#!/bin/sh
echo "info"  |redis-cli -h 192.168.2.55 -p 7000 |grep hit |awk -F ":" '{print $2}'  >>/tmp/rhit.txt
echo "info"  |redis-cli -h 192.168.2.55 -p 7001 |grep hit |awk -F ":" '{print $2}'  >>/tmp/rhit.txt
echo "info"  |redis-cli -h 192.168.2.55 -p 7002 |grep hit |awk -F ":" '{print $2}'  >>/tmp/rhit.txt


echo "info"  |redis-cli -h 192.168.2.55 -p 7000 |grep miss |awk -F ":" '{print $2}'  >>/tmp/rmiss.txt
echo "info"  |redis-cli -h 192.168.2.55 -p 7001 |grep miss |awk -F ":" '{print $2}'  >>/tmp/rmiss.txt
echo "info"  |redis-cli -h 192.168.2.55 -p 7002 |grep miss |awk -F ":" '{print $2}'  >>/tmp/rmiss.txt




HIT=`cat /tmp/rhit.txt |awk '{sum+=$1} END {print sum}'`
MIS=`cat /tmp/rmiss.txt |awk '{sum+=$1} END {print sum}'`


percent=$(printf "%d%%" $((HIT*100/(HIT+MIS))))
echo $percent
echo $percent >>/tmp/rhitper.txt


>/tmp/rhit.txt
>/tmp/rmiss.txt

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/91975/viewspace-2125392/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/91975/viewspace-2125392/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值