python 操作Redis

一. 发布订阅模式

1.Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能。基于事件的系统中,Pub/Sub是目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统所要求的松散耦合的交互模式:订阅者(如客户端)以事件订阅的方式表达出它有兴趣接收的一个事件或一类事件;发布者(如服务器)可将订阅者感兴趣的事件随时通知相关订阅者。

2.示例:

发布者

import redis
REDIS_HOST = "xxx"
REDIS_HOST_PORT = xx
REDIS_PASSWORD = "xx"
REDIS_DB = 0
REDIS_TOPIC_DAC_CORE = "topic"

redis_client = redis.StrictRedis(
    host=REDIS_HOST,
    port=REDIS_HOST_PORT,
    password=REDIS_PASSWORD,
    db=REDIS_DB,
    decode_responses=True
)

str_list = ['hello ', 'world', 'python ', 'java']

for i in range(len(str_list)):
    value_new = str(str_list[i])
    redis_client.publish(REDIS_TOPIC_DAC_CORE, value_new)

订阅者:

redis_client = redis.StrictRedis(
    host=REDIS_HOST,
    port=REDIS_HOST_PORT,
    password=REDIS_PASSWORD,
    db=REDIS_DB,
    decode_responses=True
)

ps = redis_client.pubsub()
ps.subscribe(REDIS_TOPIC_DAC_CORE)


for item in ps.listen():
    if item['type'] == 'message':
        print(item['channel'])
        print(item['data'])

3.结果

二. python 操作redis

1.连接

host = "xx"
port = xxx
password = xx
db = x

redistClient = redis.Redis(host=host, port=port, password=password, db=db)

2. 字符串

def string():
    redistClient.set('foo', 'bar1')
    print(redistClient.get('foo'))

3. list

def set_list():
    redistClient.lpush('foo_list', 'one')
    redistClient.lpush('foo_list', 'tow')
    redistClient.lpush('foo_list', 'three')
    redistClient.lpush('foo_list', 'four')

    result = redistClient.lrange('foo2', 0, 5)
    print(result)

list常用函数

  • lpush(): 在list头部插入元素,如果列表不存在则会创建
  • lpushx():插入已经存在的列表,如果列表不存在,则插入无效
  • rpush():在list尾部插入数据
  • rpushx():在list的尾部差人数据,如果列表不存咋,则插入无效
  • llen():计算list的长度
  • lpop():删除并弹出list的第一个元素
  • rpop():删除并弹出list的最后一个元素
  • lrem():从key对应 list 中删除 count 个和 value 相同的元素

4. hash

redistClient.hset('foo_hash:info', 'name', 'Jack')
    redistClient.hset('foo_hash:info', 'age', 20)
    redistClient.hset('foo_hash:info', 'phone', '18382469020')
    redistClient.hset('foo_hash:info', 'email', '123@qq.com')

    result = redistClient.hgetall('foo_hash:info')
    print(result)

5. set

def set_set():
    redistClient.sadd('foo_set', "one")
    redistClient.sadd('foo_set', 'tow')
    redistClient.sadd('foo_set', 'three')
    # redistClient.sadd('foo_set',)
    res = redistClient.smembers('foo_set')
    print(res)

6. sorted set


def sorted_set_set():
    redistClient.zadd('mark', {'one': 1})
    redistClient.zadd('mark', {'tow': 2})

    res = redistClient.zrange('mark', 0, 2)
    print(res)

三. django 操作redis

1.下载  django-redis==5.1.0

pip install django-redis==5.1.0 

2.settings 配置

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        # "LOCATION":"redis://:123456@47.108.194.188:6479/0",
        "LOCATION": "redis://:%s@%s:%d/%d" % (REDIS_PASSWORD, REDIS_HOST, REDIS_HOST_PORT, REDIS_DB),

        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient"
        }
    }
}

3.使用

from django_redis import get_redis_connection
con = get_redis_connection('default') 
value = con.get(key)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值