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())

输出结果:

相关推荐
消失的旧时光-19435 分钟前
企业认证与安全体系(四):企业登录认证流程全解析——JWT、Redis、Spring Security 如何协同工作?
redis·安全·spring·spring security·jwt
YikNjy10 分钟前
string(c++)
java·服务器·c++
小江的记录本32 分钟前
【Spring AI】Spring AI中RAG误触发与系统提示词泄露问题解决方案(完整版+代码方案)
java·人工智能·spring boot·后端·python·spring·面试
勇往直前plus40 分钟前
Python 属性访问与操作全解析:内置函数、魔法方法与描述符深度指南
java·网络·python
Arenaschi1 小时前
关于GPT的版特点
java·网络·人工智能·windows·python·gpt
人道领域1 小时前
【LeetCode刷题日记】108.将有序数组转换为二叉搜索树
java·算法·leetcode
橙淮1 小时前
并发编程(五)
java
过期动态1 小时前
【LeetCode 热题 100】无重复字符的最长子串
java·数据结构·spring boot·算法·leetcode·职场和发展
Yeats_Liao1 小时前
好复杂的 IoT 世界:工业数据采集技术栈全景解析
java·物联网·struts
MageGojo1 小时前
短链还原 API 怎么接入:展开跳转链路、查看状态码和最终落地页
数据库·redis·缓存