栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 操作Redis

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675000.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号