You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
django/servermanager/MemcacheStorage.py

37 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from werobot.session import SessionStorage
from werobot.utils import json_loads, json_dumps
from djangoblog.utils import cache
class MemcacheStorage(SessionStorage):
def __init__(self, prefix='ws_'):
self.prefix = prefix
self.cache = cache
@property
def is_available(self):
value = "1"
self.set('checkavaliable', value=value)
return value == self.get('checkavaliable')
def key_name(self, s):
return '{prefix}{s}'.format(prefix=self.prefix, s=s)
def get(self, session_id): # 修改参数名 id → session_id
# 构造完整的缓存键名
cache_key = self.key_name(session_id) # 修改变量名
# 从缓存中获取会话数据如果不存在则返回空JSON对象
session_json = self.cache.get(cache_key) or '{}'
# 将JSON字符串解析为Python对象并返回
return json_loads(session_json)
def set(self, session_id, value): # 修改参数名 id → session_id
# 构造完整的缓存键名
cache_key = self.key_name(session_id) # 修改变量名
# 将数据序列化为JSON字符串并存储到缓存中
self.cache.set(cache_key, json_dumps(value))
def delete(self, session_id): # 修改参数名 id → session_id
# 构造完整的缓存键名
cache_key = self.key_name(session_id) # 修改变量名
# 从缓存中删除对应的会话数据
self.cache.delete(cache_key)