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

输出结果:

相关推荐
spencer_tseng20 小时前
Redis + Nacos.bat
java·windows·dos
落落Plus20 小时前
浏览器缓存机制详解
javascript·缓存·浏览器缓存
troyzhxu20 小时前
列表查询的 GraphQL —— 一行代码终结你的 if-else 地狱!
java·springboot·graphql
孫治AllenSun21 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm
好好沉淀1 天前
@NotBlank(message = “{xxx}“) 注解中花括号的含义
java
fīɡЙtīиɡ ℡1 天前
内存泄漏产生的原因
java·spring·servlet
心机之蛙qee1 天前
Redis的主从、哨兵及集群
数据库·redis·缓存
码智社1 天前
Java实现RSA密钥生成、加密解密、加签验签
java
xixingzhe21 天前
spring ai简单使用skills
数据库·人工智能·spring
wear工程师1 天前
ThreadLocal 在线程池里为什么会串数据:别只会答内存泄漏
java·后端