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

启动服务

python 复制代码
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)

基于用户的缓存隔离

测试代码:

python 复制代码
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,得到的是不同的值。

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

测试代码:

python 复制代码
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())

输出结果:

相关推荐
⑩-几秒前
SpringCloud-Nacos 配置中心实战
后端·spring·spring cloud
吃喝不愁霸王餐APP开发者4 分钟前
Java后端系统对接第三方外卖API时的幂等性设计与重试策略实践
java·开发语言
TG:@yunlaoda360 云老大10 分钟前
华为云国际站代理商的CBR主要有什么作用呢?
java·网络·华为云
速易达网络1 小时前
基于Java TCP 聊天室
java·开发语言·tcp/ip
沿着路走到底1 小时前
JS事件循环
java·前端·javascript
爱笑的眼睛111 小时前
超越 `cross_val_score`:深度解析Scikit-learn交叉验证API的架构、技巧与陷阱
java·人工智能·python·ai
❀͜͡傀儡师2 小时前
SpringBoot 扫码登录全流程:UUID 生成、状态轮询、授权回调详解
java·spring boot·后端
a努力。3 小时前
国家电网Java面试被问:Spring Boot Starter 制作原理
java·spring boot·面试
一 乐3 小时前
酒店预约|基于springboot + vue酒店预约系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端