Exemplo n.º 1
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from com.mason.redis_client import redisClient

# redis中的字符串
print("redis中的字符串")
redis_str = "redis_str"
redisClient.set(redis_str, "male")
print(redisClient.get(redis_str))
redisClient.delete(redis_str)
print(redisClient.get(redis_str))

# redis中的列表
print("redis中的列表")
redis_list = "redis_list"
redisClient.delete(redis_list)
redisClient.lpush(redis_list, "lilei")  # 从左插入
redisClient.lpush(redis_list, "hanmeimei")
print(redisClient.lrange(redis_list, 0, -1))

redisClient.rpush(redis_list, "lulala")  # 从右插入
redisClient.rpush(redis_list, "huhaha")
print(redisClient.lrange(redis_list, 0, -1))  # 获取全部,-1表示最后一个只的index

print(redisClient.lpop(redis_list))  # 从左边弹出
print(redisClient.llen(redis_list))  # 取list长度
print(redisClient.lindex(redis_list, -1))  # 取出列表最后一个元素

# redis中的集合
print("redis中的集合")
redis_set = "redis_set"
Exemplo n.º 2
0
redisClient.sadd(redis_zset, "d")
print(redisClient.smembers(redis_zset))
print(redisClient.srandmember(redis_zset, 2))
print(redisClient.spop(redis_zset))
# print(redisClient.smove(source, dest, item))

# 命令
# 用例和描述
# ZREVRANK  ZREVRANK key-name member — 返回有序集合里成员
# member 的排名,成员按照分值
# 从大到小排列
# ZREVRANGE  ZREVRANGE key-name start stop [WITHSCORES] — 返回有序集合给定排名范围内
# 的成员,成员按照分值从大到小排列
# ZRANGEBYSCORE  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] — 返回
# 有序集合中,分值介于 min 和 max 之间的所有成员
# ZREVRANGEBYSCORE  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] —
# 获取有序集合中分值介于 min 和 max 之间的所有成员,并按照分值从大到小的顺序来返
# 回它们
# ZREMRANGEBYRANK  ZREMRANGEBYRANK key-name start stop — 移除有序集合中排名介于 start 和 stop
# 之间的所有成员
# ZREMRANGEBYSCORE  ZREMRANGEBYSCORE key-name min max — 移除有序集合中分值介于 min 和 max 之
# 间的所有成员
# ZINTERSTORE  ZINTERSTORE dest-key key-count key [key ...] [WEIGHTS weight
# [weight ...]] [AGGREGATE SUM|MIN|MAX] — 对给定的有序集合执行类似于集合的
# 交集运算
# ZUNIONSTORE  ZUNIONSTORE dest-key key-count key [key ...] [WEIGHTS weight
# [weight ...]] [AGGREGATE SUM|MIN|MAX] — 对给定的有序集合执行类似于集合的
# 并集运算

redisClient.delete(redis_zset)
Exemplo n.º 3
0
# 命令    用例和描述
# SORT
# SORT source-key [BY pattern] [LIMIT offset count] [GET pattern [GET
# pattern ...]] [ASC|DESC] [ALPHA] [STORE dest-key] — 根据给定的选项,对输入
# 列表、集合或者有序集合进行排序,然后返回或者存储排序的结果

sort_input = "sort_input"
redisClient.rpush(sort_input, 23, 13, 32, 44, 31)
print(redisClient.lrange(sort_input, 0, -1))
# 默认是升序的
print(redisClient.sort(sort_input))
# 降序
print(redisClient.sort(sort_input, desc=True))
# 字母顺序
print(redisClient.sort(sort_input, alpha=True))
# 将某个散列作为权重
redisClient.hset("d-23", "field", 5)
redisClient.hset("d-13", "field", 1)
redisClient.hset("d-32", "field", 8)
redisClient.hset("d-44", "field", 9)
redisClient.hset("d-31", "field", 3)
# 将散列的域field作为权重,对sort_input进行排序
print(redisClient.sort(sort_input, by="d-*->field"))
print(redisClient.sort(sort_input, by="d-*->field", get="d-*->field"))
redisClient.delete(sort_input)
redisClient.delete("d-23")
redisClient.delete("d-13")
redisClient.delete("d-32")
redisClient.delete("d-44")
redisClient.delete("d-31")
Exemplo n.º 4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from com.mason.redis_client import redisClient

# Redis 的字符串就是一个由字节组成的序列
# 字符串可以存储以下 3 种类型的值。
# 字节串(byte string)。
# 整数。
# 浮点数。

redis_str = "redis_str"
print(redisClient.delete(redis_str))
print(redisClient.get(redis_str))  # None
print(redisClient.incr(redis_str))  # 对不存在的键执行自增或者自减时,redis会先初始化一个键,然后认为它的值为0
print(redisClient.incr(redis_str, amount=10))  # 自增自减函数的amount参数可选,默认是1
print(redisClient.decr(redis_str, amount=5))
print(redisClient.get(redis_str))
print(redisClient.set(redis_str, "22"))
print(redisClient.get(redis_str))
print(redisClient.append(redis_str, "23"))  # 尾部追加
print(len(redisClient.get(redis_str)))

# 在使用 SETRANGE 或者 SETBIT 命令对字符串进行写入的时候,如果字符串当前的长度不
# 能满足写入的要求,那么 Redis 会自动地使用空字节(null)来将字符串扩展至所需的长度,然
# 后才执行写入或者更新操作。

print(redisClient.getrange(redis_str, 0, -1))  # 截取key对应的value,包含start和end,闭区间
print(redisClient.setrange(redis_str, 0, "heiha"))  # 将键值从start开始的内容,替换为value
print(redisClient.get(redis_str))
Exemplo n.º 5
0

def remove_contact(conn: Redis, user, contact):
    conn.lrem("recent:" + user, 1, contact)


def fetch_autocomplete_list(conn: Redis, user, prefix):
    # 获取自动补全列表
    candidates = conn.lrange("recent:" + user, 0, -1)
    matches = []
    # 遍历匹配
    for candidate in candidates:
        if str(candidate).lower().startswith(prefix):
            matches.append(candidate)
    return matches


add_update_contact(redisClient, "mason", "lilei")
add_update_contact(redisClient, "mason", "hanmeimei")
add_update_contact(redisClient, "mason", "lulala")
add_update_contact(redisClient, "mason", "lilaohu")
add_update_contact(redisClient, "mason", "yahaha")
add_update_contact(redisClient, "mason", "caonima")
add_update_contact(redisClient, "mason", "nishuosha")
add_update_contact(redisClient, "mason", "woshayemeishuo")
add_update_contact(redisClient, "mason", "huhuhu")
add_update_contact(redisClient, "mason", "???")

print(fetch_autocomplete_list(redisClient, "mason", "l"))
redisClient.delete("recent:mason")
Exemplo n.º 6
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time

from com.mason.redis.constant import const
from com.mason.redis_client import redisClient

article_name = "lulala:10086"
time_zset = "time:"
score_zset = "score:"
voted_set = "voted:"
article_id = article_name.partition(":")[-1]

redisClient.delete(article_name)
redisClient.delete(time_zset)
redisClient.delete(score_zset)
redisClient.delete(voted_set + article_id)

curr_time = time.time()
print("curr_time={curr_time}".format(curr_time=curr_time))
redisClient.zadd(time_zset, {article_name: curr_time})
redisClient.zadd(score_zset, {article_name: 0})
redisClient.hset(article_name, "votes", 0)


# 投票
def article_vote(conn, user, article, is_negative=False):
    cutoff = time.time() - const.ONE_WEEK_IN_SECONDS
    # 投票时间为文章发布一周内
    curr_score = conn.zscore(time_zset, article)
    if curr_score < cutoff:
Exemplo n.º 7
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from com.mason.redis_client import redisClient

# Redis 的散列
# HEXISTS
# HEXISTS key-name key — 检查给定键是否存在于散列中
# HKEYS
# HKEYS key-name — 获取散列包含的所有键
# HVALS
# HVALS key-name — 获取散列包含的所有值
# HGETALL
# HGETALL key-name — 获取散列包含的所有键值对
# HINCRBY
# HINCRBY key-name key increment — 将键 key 存储的值加上整数 increment
# HINCRBYFLOAT
# HINCRBYFLOAT key-name key increment — 将键 key 存储的值加上浮点数 increment

redis_hash = "redis_hash"
redisClient.hmset(redis_hash, {"key01": "value01"})
print(redisClient.hkeys(redis_hash))
redisClient.delete(redis_hash)
Exemplo n.º 8
0
# -*- coding: utf-8 -*-

from com.mason.redis_client import redisClient

# Redis 的列表

redis_list = "redis_list"
redisClient.rpush(redis_list, "a")
redisClient.rpush(redis_list, "b")
redisClient.rpush(redis_list, "c")
redisClient.rpush(redis_list, "d")
print(redisClient.lrange(redis_list, 0, -1))
redisClient.ltrim(redis_list, 0, 1)  # 截取列表中[start, end]的内容
print(redisClient.lrange(redis_list, 0, -1))

# 阻塞式的列表弹出命令以及在列表之间移动元素的命令
# 命令
# 用例和描述
# BLPOP  BLPOP key-name [key-name ...] timeout — 从第一个非空列表中弹出位于最左端的元素,
# 或者在 timeout 秒之内阻塞并等待可弹出的元素出现
# BRPOP  BRPOP key-name [key-name ...] timeout — 从第一个非空列表中弹出位于最右端的元素,
# 或者在 timeout 秒之内阻塞并等待可弹出的元素出现
# RPOPLPUSH  RPOPLPUSH source-key dest-key — 从
# source-key 列表中弹出位于最右端的元素,然后
# 将这个元素推入 dest-key 列表的最左端,并向用户返回这个元素
# BRPOPLPUSH  BRPOPLPUSH source-key dest-key timeout — 从 source-key 列表中弹出位于最右端的
# 元素,然后将这个元素推入 dest-key 列表的最左端,并向用户返回这个元素;如果 source-key
# 为空,那么在 timeout 秒之内阻塞并等待可弹出的元素出现

redisClient.delete(redis_list)
Exemplo n.º 9
0
            pipe.zrange(zset_name, start_rank, query_range)
            items = pipe.execute()[-1]
            break
        except redis.exceptions.WatchError:
            # 如果自动补全集合被其他客户端修改过
            # 则重试
            continue
    # 如果有其他自动补全操作正在执行,那么从获
    # 取到的元素里面移除起始元素和结束元素
    return [item for item in items if "{" not in item]


def join_guild(conn: Redis, guild, user):
    conn.zadd("members:" + guild, {user: 0})


def leave_guild(conn: Redis, guild, user):
    conn.zrem("members:" + guild, user)


guild = "10086"
redisClient.delete("members:" + guild)
join_guild(redisClient, guild, "mason")
join_guild(redisClient, guild, "yahaha")
join_guild(redisClient, guild, "lilei")
join_guild(redisClient, guild, "hmeimei")
join_guild(redisClient, guild, "mmmeee")
join_guild(redisClient, guild, "lulala")
print(autocomplete_on_prefix(redisClient, guild, "mma"))
redisClient.delete("members:" + guild)