Redis client Python usage

mport redis

r_server = redis.Redis(‘localhost‘) #this line creates a new Redis object and
                                    #connects to our redis server
r_server.set(‘test_key‘, ‘test_value‘) #with the created redis object we can
                                        #submits redis commands as its methods

print ‘previous set key ‘ + r_server.get(‘test_key‘) # the previous set key is fetched

‘‘‘In the previous example you saw that we introduced a redis
data type: the string, now we will set an integer and try to
increase its value using redis object built-in methods‘‘‘

r_server.set(‘counter‘, 1) #set an integer to a key
r_server.incr(‘counter‘) #we increase the key value by 1, has to be int
print ‘the counter was increased! ‘+ r_server.get(‘counter‘) #notice that the key is increased now

r_server.decr(‘counter‘) #we decrease the key value by 1, has to be int
print ‘the counter was decreased! ‘+ r_server.get(‘counter‘) #the key is back to normal

‘‘‘Now we are ready to jump into another redis data type, the list, notice
that they are exactly mapped to python lists once you get them‘‘‘

r_server.rpush(‘list1‘, ‘element1‘) #we use list1 as a list and push element1 as its element

r_server.rpush(‘list1‘, ‘element2‘) #assign another element to our list
r_server.rpush(‘list2‘, ‘element3‘) #the same

print ‘our redis list len is: %s‘% r_server.llen(‘list1‘) #with llen we get our redis list size right from redis

print ‘at pos 1 of our list is: %s‘% r_server.lindex(‘list1‘, 1) #with lindex we query redis to tell us which element is at pos 1 of our list

‘‘‘sets perform identically to the built in Python set type. Simply, sets are lists but, can only have unique values.‘‘‘

r_server.sadd("set1", "el1")
r_server.sadd("set1", "el2")
r_server.sadd("set1", "el2")

print ‘the member of our set are: %s‘% r_server.smembers("set1")

‘‘‘basically our redis client can do any command supported by redis, check out redis documentation for available commands for your server‘‘‘
时间: 2025-01-02 10:12:48

Redis client Python usage的相关文章

Redis的Python实践,以及四中常用应用场景详解——学习董伟明老师的《Python Web开发实践》

首先,简单介绍:Redis是一个基于内存的键值对存储系统,常用作数据库.缓存和消息代理. 支持:字符串,字典,列表,集合,有序集合,位图(bitmaps),地理位置,HyperLogLog等多种数据结构. 支持事务.分片.主从复之.支持RDB(内存数据保存的文件)和AOF(类似于MySQL的binlog)两种持久化方式.3.0加入订阅分发.Lua脚本.集群等特性. 命令参考:http://doc.redisfans.com 中文官网:http://www.redis.net.cn 安装(都大同小

lettuce--Advanced Redis client

redis官方提供的java client: git地址:https://github.com/mp911de/lettuceAdvanced Redis client for thread-safe sync, async, and reactive usage. Supports Cluster, Sentinel, Pipelining, and codecs.http://redis.paluch.biz Introduction Lettuce is a scalable thread

Redis的Python客户端redis-py的初步使用

1. 安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Parser类,PythonParser和HiredisParser.默认,如果已经安装了hiredis模块,redis-py会使用HiredisParser,否则会使用PythonParser.HiredisParser是C编写的,由redis核心团队维护,性能要比PythonParser提高10倍以上,所以推荐使

redis client protocol 解析

在官网http://redis.io/topics/protocol有对redis通信协议有做说明. 基于下面的一些原因,我想解析redis client protocol: 1.足够了解通信协议,有助于做出更好的系统设计. 2.学习RESP的设计思想,不仅能扩展我的思维,也许将来能应用于我的代码中. 3.因为有些人想将redis client直接并入自己已有的系统中:包括我在内.这个将在我下一篇文章再做说明. 下面我翻译一下http://redis.io/topics/protocol一些我认

StackExchange.Redis Client

StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceStack.Redis这个.net客户端,但是这个的最新版本目前已经变成了商业软件.对于ServiceStack.Redis这种行为,我们没有什么好说的,留给我们的选择是使用低版本的开源版本或者转向其他的客户端. 要说到StackExchange.

redis client 2.0.0 pipeline 的list的rpop bug

描写叙述: redis client 2.0.0 pipeline 的list的rpop 存在严重bug,rpop list的时候,假设list已经为空的时候,rpop出来的Response依旧不为null,导致吊response.get()方法抛异常 代码: @Test public void testRedisPipeline(){ Jedis jedis = null; try{ jedis = new Jedis("127.0.0.1",6379); Pipeline pipe

【轮子狂魔】手把手教你自造Redis Client

为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居然开始收费了. 作为轮子狂魔,是可忍孰不可忍啊.于是我决定自己造轮子了. Redis通信协议 先给个Redis官方的通信协议地址:http://redisdoc.com/topic/protocol.html 关键是我截图的部分,我们可以得到以下几个信息: 1.tcp协议 2.默认端口6379 3.

redis client protocol 实现

在官网中http://redis.io/clients有许多已经实现好的redis client:有需要可以参考一下. 其实前一篇http://blog.csdn.net/yitouhan/article/details/46612925 redis client protocol 解析,我已经对RESP做主要的解析. 下面是解析RESP的所有函数,其中对外函数是RedisProtocol::Decode: https://github.com/XJM2013/GameEngine/blob/m

./redis-trib.rb 报错:/usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 0 is already busy (Redis::CommandError)

错误提示是 slot插槽被占用了(这是 搭建集群前时,以前redis的旧数据和配置信息没有清理干净.) 解决方案是 用redis-cli 登录到每个节点执行  flushall  和 cluster reset  就可以了. 然后重新执行群集脚本命令: ./redis-trib.rb create --replicas 1 192.168.*.*:7001 192.168.*.*:7002 192.168.*.*:7003 192.168.*.*:7004 192.168.*.*:7005  1