【Redis】(sorted set) 有序集合数据类型的使用,应用和问题

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_29757283/article/details/85492051

Redis 有序集合(sorted set)数据类型的使用,应用和问题

redis-cli 操作数据库 - n/a

Python 操作 Redis

redis-py 库

ZADD

issue
AttributeError: <flot/string/integer> object has no attribute 'items'1
##############################
####### case 1 ###############
##############################
#### This happened after using redis 3.0.0.post1, 
#### when I uninstalled it and installed 2.10.6, it's working.
## Reference: https://github.com/andymccurdy/redis-py/issues/1068
Traceback (most recent call last):
[...]
File "/myenv/lib/python3.5/site-packages/redis/client.py", line 2263, in zadd
for pair in iteritems(mapping):
File "/myenv/lib/python3.5/site-packages/redis/_compat.py", line 123, in iteritems
return iter(x.items())
AttributeError: 'float' object has no attribute 'items'

###########################
####### case 2 ############
##########################
In [17]: rd.zadd("RANK", _id, _rank)                                 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-75a3699246e4> in <module>
----> 1 rd.zadd("RANK", _id, _rank)

~/.virtualenvs/CSDN-Data/lib/python3.5/site-packages/redis/client.py in zadd(self, name, mapping, nx, xx, ch, incr)
   2261             pieces.append(Token.get_token('INCR'))
   2262             options['as_score'] = True
-> 2263         for pair in iteritems(mapping):
   2264             pieces.append(pair[1])
   2265             pieces.append(pair[0])

~/.virtualenvs/CSDN-Data/lib/python3.5/site-packages/redis/_compat.py in iteritems(x)
    121 
    122     def iteritems(x):
--> 123         return iter(x.items())
    124 
    125     def iterkeys(x):

AttributeError: 'str' object has no attribute 'items'

solution
#
# from "https://github.com/andymccurdy/redis-py/issues/1068":
#
import redis
r = redis.Redis(...)

if redis.VERSION[0] < 3:
    r.zadd('my-key', element1=score1)
else:
    r.zadd('my-key', {element1: score1})

#
# fix ## case 2 ## solution for example
#
rd.zadd("RANK", {_id: _rank})
## rd.zadd("<the key>", {member1: score1, member2: score2,
##                       member3: score3, }) # , member4: score4, ...
## could work also.

ZCARD - n/a

Reference


  1. (redis 3.0.0 and 3.0.0.post1) AttributeError: ‘float’ object has no attribute ‘items’ #1068 ↩︎

猜你喜欢

转载自blog.csdn.net/qq_29757283/article/details/85492051