当前位置: 首页 > news >正文

redis 异步读写,2.0改版后操作代码

redis2
`
from tornado import ioloop, web
import json
import aioredis

settings = {
"debug" : True,
"redis":"redis://:@127.0.0.1:6379/0",
}

class Home(web.RequestHandler):
def prepare(self):
self.json_argument = {}
# if self.request.method.lower() in ("get", "post", "put", "delete", "patch"):
# if self.request.headers.get("Content-Type").lower() == "application/json":
# self.json_argument = json.loads(self.request.body)

async def get(self):# 2.0之后,aioredis 提供了 from_url 方法,直接从 url 中创建连接# redis = await aioredis.create_connection(self.application.settings["redis"])# redis = await aioredis.create_connection(self.application.settings["redis"])# await redis.execute("setex", "name", 60, "chen",)# data = await redis.get("name")# print(data.decode())# self.write(data.decode())redis = aioredis.from_url(self.application.settings["redis"])async with redis.client() as conn:await conn.setex("name", 60, "chen")data = await conn.get("name")print(data.decode())self.write(data.decode())

async def redis_pool():
# Redis client bound to pool of connections (auto-reconnecting).
redis = aioredis.from_url(
"redis://localhost", encoding="utf-8", decode_responses=True
)
await redis.set("my-key", "value")
val = await redis.get("my-key")
print(val)

def make_app():
return web.Application(handlers=[
(r"/", Home)],
**settings,
)

if name == 'main':
app = make_app()
app.listen(8888)
ioloop.IOLoop.current().start()

`

http://www.rkmt.cn/news/24269.html

相关文章:

  • 2025年棒球帽,卫衣,羽绒服厂家推荐排行榜,潮流设计与舒适体验的时尚之选!
  • 22-windows11-wsl-deepin-envoy-proxy-安装
  • uml九图
  • 2025年安恒信息公司深度解析:AI与数据安全双轮驱动的技术护城河
  • 统计单词(p1308)
  • 2025年安恒信息深度解析:AI与数据安全双轮驱动的技术跃迁
  • SpringCloud系列十三:Spring Cloud和Spring Cloud Alibaba有什么关系
  • 2025年10月美白精华推荐榜:OLAY水光小白瓶领衔对比评测
  • 2025年10月北京口腔医院推荐:对比评测榜助您高效择医
  • 2025年10月抗老精华产品推荐榜:十款热门单品对比评测与排名解析
  • 2025年10月留香沐浴露推荐:五强对比评测榜助你锁定24小时体香方案
  • 奶奶都能看懂的 C++ —— const 限定符与指针
  • 2025年10月深圳酒店综合对比与排行评测指南
  • 2025年10月连锁酒店品牌综合实力对比与投资价值排行分析
  • 2025年10月上海老房翻新公司客观对比与排名分析
  • 2025年10月北京治疗肺癌医生排行榜与专业能力深度评测
  • 2025年10月深圳婚姻纠纷律师综合对比与专业排行分析
  • 2025年10月上海离婚律师客观对比与实用排行分析
  • 2025年定型机厂家推荐排行榜,拉幅定型机/门富士定型机/节能定型机/余热回收/废气回收/烟气回收/智能排风/双层定型机公司精选
  • 2025年印染水洗机厂家推荐排行榜,高效节能与智能控制的行业首选!
  • 2025年海信洗碗机深度解析与推荐:全球技术整合与市场战略分析
  • 专题:2025年游戏科技的AI革新研究报告:全球市场趋势研究报告|附130+份报告PDF、数据仪表盘汇总下载
  • 2025年海信油烟机权威分析与推荐:全球化布局与技术创新的深度解析
  • 2025年10月洗碗机品牌对比与排行榜分析
  • 2025年10月中国人力资源管理咨询公司对比与排行榜深度解析
  • bzip2-1.0.6-13.el7.x86_64.rpm 怎么安装?CentOS 7 手动安装 rpm 包详细步骤
  • 2025年10月北京劳动争议律所综合实力排行与深度评测分析
  • 2025年10月自行车品牌综合对比与选购排行指南
  • 2025年10月自行车品牌排行榜与深度评测对比分析
  • 2025年10月槲皮素产品综合排名与详细评测分析