zdppy_cache缓存框架升级,支持用户级别的缓存隔离,支持超级管理员管理普通用户的缓存

98 篇文章 1 订阅
69 篇文章 1 订阅

启动服务

import zdppy_api as api
import zdppy_cache

key1 = "admin"
key2 = "admin"

app = api.Api(routes=[
    *zdppy_cache.zdppy_api.cache(key1, key2, api)
])

if __name__ == '__main__':
    import zdppy_uvicorn

    zdppy_uvicorn.run(app, host="0.0.0.0", port=8888)

基于用户的缓存隔离

测试代码:

import req

# 用户1设置缓存
headers = {
    "key1": "zhangdapeng",
    "key2": "zhangdapeng520",
}
resp = req.post("http://127.0.0.1:8888/zdppy_cache", json={
    "key": "name",
    "value": "张三"
}, headers=headers)
print(resp.json())

resp = req.get("http://127.0.0.1:8888/zdppy_caches", json={}, headers=headers)
print(resp.json())

resp = req.get("http://127.0.0.1:8888/zdppy_cache", json={"key": "name"}, headers=headers)
print(resp.json())

# 用户2设置缓存
headers2 = {
    "key1": "zhangdapeng1",
    "key2": "zhangdapeng521",
}
resp = req.post("http://127.0.0.1:8888/zdppy_cache", json={
    "key": "name",
    "value": "张三333"
}, headers=headers2)
print(resp.json())
resp = req.get("http://127.0.0.1:8888/zdppy_cache", json={"key": "name"}, headers=headers2)
print(resp.json())
resp = req.get("http://127.0.0.1:8888/zdppy_cache", json={"key": "name"}, headers=headers)
print("用户1相同的key", resp.json())

不同的用户,相同的key,得到的是不同的值。
在这里插入图片描述

超级管理员可以管理普通用户的缓存

测试代码:

import req

# 用户1设置缓存
headers = {
    "key1": "zhangdapeng",
    "key2": "zhangdapeng520",
}
resp = req.post("http://127.0.0.1:8888/zdppy_cache", json={
    "key": "name",
    "value": "张三"
}, headers=headers)
print(resp.json())
resp = req.get("http://127.0.0.1:8888/zdppy_cache", json={"key": "name"}, headers=headers)
print(resp.json())

# 通过管理员获取用户缓存
headers2 = {
    "key1": "admin",
    "key2": "admin",
    "username": "zhangdapeng",  # 指定要操作的用户
}
resp = req.get("http://127.0.0.1:8888/zdppy_cache/manage", json={
    "key": "name",
}, headers=headers2)
print(resp.json())

# 通过管理员删除用户缓存
resp = req.delete("http://127.0.0.1:8888/zdppy_cache/manage", json={
    "key": "name",
}, headers=headers2)
print(resp.json())

# 用户自己查询缓存
resp = req.get("http://127.0.0.1:8888/zdppy_cache", json={"key": "name"}, headers=headers)
print(resp.json())

输出结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python私教

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值