本文整理汇总了Python中redis.StrictRedis.hincrby方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.hincrby方法的具体用法?Python StrictRedis.hincrby怎么用?Python StrictRedis.hincrby使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.hincrby方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_group
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import hincrby [as 别名]
def new_group(request):
groupname = request.POST.get("groupname")
totwords = request.POST.get("totwords")
totmembers = request.POST.get('totmembers')
pref = settings.MY_PREFIX
prefg = pref+":"+groupname
user = str(request.user)
rd = StrictRedis()
# the above statements are self explanatory
exists = rd.exists(pref+":"+groupname)
if exists:
# return error, can't create the group with that name
# don't do that, just add this user to the already existing group
# if group size < totmembers
d = rd.hgetall(prefg+":hash")
response = {'facility':""}
if int(d['totmembers'])>int(d['curmembers']):
rd.hincrby(prefg+":hash", 'curmembers')
#d = rd.hgetall(prefg+":hash")
response['facility'] = groupname
response['new_group_created'] = False
rd.sadd(prefg, user)
#now notify the others that this user joined the group!
redis_publisher = RedisPublisher(facility = pref, broadcast = True)
mydict = {"type":"new_join", 'who':user, 'name':groupname}
msgstring = json.dumps(mydict)
print "broadcast message:"+msgstring
message = RedisMessage(msgstring)
redis_publisher.publish_message(message)
# now check if the competition is ready to start
if int(d['totmembers'])-1 == int(d['curmembers']):
start_competition(request,groupname)
return JsonResponse(response)
# so the group doesn't exist
rd.sadd(prefg, user)
# add this user to the set of all the users that are part of this group
hashdict = {'totwords':totwords, 'totmembers':totmembers, "owner":user, "name":groupname, 'curmembers':1}
# adding group name is redundant but it simplifies things a lot
rd.hmset(prefg+":hash", hashdict)
# using hash greatly simplifies the things(dict interconversion hgetall()), though i feel that the
# naming convention used is horrible
redis_publisher = RedisPublisher(facility = pref, broadcast = True)
mydict = {"type":"new_group"}
mydict.update(hashdict)
msgstring = json.dumps(mydict)
print msgstring
message = RedisMessage(msgstring)
redis_publisher.publish_message(message)
# notify the others about this new group that was created
rd.sadd(pref+":groups", groupname)
return JsonResponse({'facility':groupname, 'new_group_created':True})
示例2: RedisBroker
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import hincrby [as 别名]
class RedisBroker(object):
def __init__(self, **redis_c):
self.ip_set_name = redis_c.pop('ip_set_name')
self._r = StrictRedis(**redis_c)
def ip_notebook(self, ip):
(count, status) = self._r.hmget(ip, 'count', 'status')
print count, status
if not count:
self._r.hmset(ip, {'count': 1, 'status': 0})
self._r.sadd(self.ip_set_name, ip)
else:
self._r.hincrby(ip, 'count', amount=1)
示例3: Link
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import hincrby [as 别名]
class Link(object):
def __init__(self):
self.r = StrictRedis(
host=settings.LINKDB_REDIS_HOST,
port=settings.LINKDB_REDIS_PORT,
db=settings.LINKDB_REDIS_DB)
def shorten(self, link):
"""
create a new link.
"""
n = 3
i = 0
while True:
hash = self._hash(n)
if self.r.hsetnx(hash, 'link', link):
break
i = i + 1
# if we didn't find an available hash after this number of tries,
# increment hash length.
if i > 100:
n = n + 1
i = 0
uuid = self._uuid()
date = self._date()
self.r.hset(hash, 'uuid', uuid)
self.r.hset(hash, 'hits', 0)
self.r.hset(hash, 'mods', 0)
self.r.hset(hash, 'created_at', date)
self.r.hset(hash, 'lasthit_at', '')
self.r.hset(hash, 'changed_at', '')
return hash, uuid
def modify(self, hash, uuid, link):
"""
update hash to point to a new long url.
"""
if not self.exists(hash, uuid):
raise AuthFailure
self.r.hset(hash, 'link', link)
self.r.hincrby(hash, 'mods', 1)
self.r.hset(hash, 'changed_at', self._date())
def resolve(self, hash):
"""
resolve hash -> long url, and update counters.
"""
link = self.r.hget(hash, 'link')
if link is None:
raise NotFound
self.r.hincrby(hash, 'hits', 1)
self.r.hset(hash, 'lasthit_at', self._date())
return link
def exists(self, hash, uuid=None):
"""
check if a hash or a (hash + uuid) combination exists.
"""
if uuid:
return self.r.hget(hash, 'uuid') == uuid.lower()
return self.r.exists(hash)
def _hash(self, length=7):
"""
random base62 string
"""
return ''.join(choice(letters + digits) for _ in range(length))
def _uuid(self):
"""
random uuid string
"""
return str(uuid4())
def _date(self):
"""
`YYYY-MM-DD HH:MM:SS`
"""
return str(datetime.utcnow().replace(microsecond=0))
示例4: aggregate
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import hincrby [as 别名]
def aggregate(db_params):
cxn1 = Redis(db=db_params['events_db_id'] )
cxn1.hset('H1', 'counter', 0)
while 1:
line = (yield)
cxn1.hincrby('H1', 'counter', 1)