优化用户级配置能力
This commit is contained in:
parent
ae60040120
commit
d6c6c999fc
@ -190,7 +190,7 @@ def get_config(key: str,
|
|||||||
"""
|
"""
|
||||||
查询用户配置
|
查询用户配置
|
||||||
"""
|
"""
|
||||||
value = UserConfigOper().get(current_user.id, key)
|
value = UserConfigOper().get(username=current_user.name, key=key)
|
||||||
return schemas.Response(success=True, data={
|
return schemas.Response(success=True, data={
|
||||||
"value": value
|
"value": value
|
||||||
})
|
})
|
||||||
@ -201,5 +201,5 @@ def set_config(key: str, value: Union[list, dict, bool, int, str] = None,
|
|||||||
"""
|
"""
|
||||||
更新用户配置
|
更新用户配置
|
||||||
"""
|
"""
|
||||||
UserConfigOper().set(current_user.id, key, value)
|
UserConfigOper().set(username=current_user.name, key=key, value=value)
|
||||||
return schemas.Response(success=True)
|
return schemas.Response(success=True)
|
||||||
|
@ -9,30 +9,30 @@ class UserConfig(Base):
|
|||||||
用户配置表
|
用户配置表
|
||||||
"""
|
"""
|
||||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||||
# 用户ID
|
# 用户名
|
||||||
user_id = Column(Integer, index=True)
|
username = Column(String, index=True)
|
||||||
# 配置键
|
# 配置键
|
||||||
key = Column(String)
|
key = Column(String)
|
||||||
# 值
|
# 值
|
||||||
value = Column(String, nullable=True)
|
value = Column(String, nullable=True)
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
# 用户ID和配置键联合唯一
|
# 用户名和配置键联合唯一
|
||||||
UniqueConstraint('user_id', 'key'),
|
UniqueConstraint('username', 'key'),
|
||||||
Index('ix_userconfig_userid_key', 'user_id', 'key'),
|
Index('ix_userconfig_username_key', 'username', 'key'),
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@db_query
|
@db_query
|
||||||
def get_by_key(db: Session, user_id: int, key: str):
|
def get_by_key(db: Session, username: str, key: str):
|
||||||
return db.query(UserConfig) \
|
return db.query(UserConfig) \
|
||||||
.filter(UserConfig.user_id == user_id) \
|
.filter(UserConfig.username == username) \
|
||||||
.filter(UserConfig.key == key) \
|
.filter(UserConfig.key == key) \
|
||||||
.first()
|
.first()
|
||||||
|
|
||||||
@db_update
|
@db_update
|
||||||
def delete_by_key(self, db: Session, user_id: int, key: str):
|
def delete_by_key(self, db: Session, username: str, key: str):
|
||||||
userconfig = self.get_by_key(db, user_id, key)
|
userconfig = self.get_by_key(db=db, username=username, key=key)
|
||||||
if userconfig:
|
if userconfig:
|
||||||
userconfig.delete(db, userconfig.id)
|
userconfig.delete(db=db, rid=userconfig.id)
|
||||||
return True
|
return True
|
||||||
|
@ -10,7 +10,7 @@ from app.utils.singleton import Singleton
|
|||||||
|
|
||||||
class UserConfigOper(DbOper, metaclass=Singleton):
|
class UserConfigOper(DbOper, metaclass=Singleton):
|
||||||
# 配置缓存
|
# 配置缓存
|
||||||
__USERCONF: Dict[int, Dict[str, Any]] = {}
|
__USERCONF: Dict[str, Dict[str, Any]] = {}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
@ -18,85 +18,79 @@ class UserConfigOper(DbOper, metaclass=Singleton):
|
|||||||
"""
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
for item in UserConfig.list(self._db):
|
for item in UserConfig.list(self._db):
|
||||||
if ObjectUtils.is_obj(item.value):
|
value = json.loads(item.value) if ObjectUtils.is_obj(item.value) else item.value
|
||||||
self.__set_config_cache(user_id=item.user_id, key=item.key, value=json.loads(item.value))
|
self.__set_config_cache(username=item.username, key=item.key, value=value)
|
||||||
else:
|
|
||||||
self.__set_config_cache(user_id=item.user_id, key=item.key, value=item.value)
|
|
||||||
|
|
||||||
def set(self, user_id: int, key: Union[str, UserConfigKey], value: Any):
|
def set(self, username: str, key: Union[str, UserConfigKey], value: Any):
|
||||||
"""
|
"""
|
||||||
设置用户配置
|
设置用户配置
|
||||||
"""
|
"""
|
||||||
if isinstance(key, UserConfigKey):
|
if isinstance(key, UserConfigKey):
|
||||||
key = key.value
|
key = key.value
|
||||||
# 更新内存
|
# 更新内存
|
||||||
self.__set_config_cache(user_id=user_id, key=key, value=value)
|
self.__set_config_cache(username=username, key=key, value=value)
|
||||||
# 写入数据库
|
# 写入数据库
|
||||||
if ObjectUtils.is_obj(value):
|
if ObjectUtils.is_obj(value):
|
||||||
value = json.dumps(value)
|
value = json.dumps(value)
|
||||||
elif value is None:
|
elif value is None:
|
||||||
value = ''
|
value = ''
|
||||||
conf = UserConfig.get_by_key(self._db, user_id, key)
|
conf = UserConfig.get_by_key(db=self._db, username=username, key=key)
|
||||||
if conf:
|
if conf:
|
||||||
if value:
|
if value:
|
||||||
conf.update(self._db, {"value": value})
|
conf.update(self._db, {"value": value})
|
||||||
else:
|
else:
|
||||||
conf.delete(self._db, conf.id)
|
conf.delete(self._db, conf.id)
|
||||||
else:
|
else:
|
||||||
conf = UserConfig(user_id=user_id, key=key, value=value)
|
conf = UserConfig(username=username, key=key, value=value)
|
||||||
conf.create(self._db)
|
conf.create(self._db)
|
||||||
|
|
||||||
def get(self, user_id: int, key: Union[str, UserConfigKey] = None) -> Any:
|
def get(self, username: str, key: Union[str, UserConfigKey] = None) -> Any:
|
||||||
"""
|
"""
|
||||||
获取用户配置
|
获取用户配置
|
||||||
"""
|
"""
|
||||||
if not user_id:
|
if not username:
|
||||||
return self.__USERCONF
|
return self.__USERCONF
|
||||||
if isinstance(key, UserConfigKey):
|
if isinstance(key, UserConfigKey):
|
||||||
key = key.value
|
key = key.value
|
||||||
if not key:
|
if not key:
|
||||||
return self.__get_config_caches(user_id=user_id)
|
return self.__get_config_caches(username=username)
|
||||||
return self.__get_config_cache(user_id=user_id, key=key)
|
return self.__get_config_cache(username=username, key=key)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if self._db:
|
if self._db:
|
||||||
self._db.close()
|
self._db.close()
|
||||||
|
|
||||||
def __set_config_cache(self, user_id: int, key: str, value: Any):
|
def __set_config_cache(self, username: str, key: str, value: Any):
|
||||||
"""
|
"""
|
||||||
设置配置缓存
|
设置配置缓存
|
||||||
"""
|
"""
|
||||||
if not user_id or not key:
|
if not username or not key:
|
||||||
return
|
return
|
||||||
cache = self.__USERCONF
|
cache = self.__USERCONF
|
||||||
if not cache:
|
if not cache:
|
||||||
cache = {}
|
cache = {}
|
||||||
user_cache = cache.get(user_id)
|
user_cache = cache.get(username)
|
||||||
if not user_cache:
|
if not user_cache:
|
||||||
user_cache = {}
|
user_cache = {}
|
||||||
cache[user_id] = user_cache
|
cache[username] = user_cache
|
||||||
user_cache[key] = value
|
user_cache[key] = value
|
||||||
self.__USERCONF = cache
|
self.__USERCONF = cache
|
||||||
|
|
||||||
def __get_config_caches(self, user_id: int) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
获取配置缓存
|
|
||||||
"""
|
|
||||||
if not user_id:
|
|
||||||
return None
|
|
||||||
if not self.__USERCONF:
|
|
||||||
return None
|
|
||||||
return self.__USERCONF.get(user_id)
|
|
||||||
|
|
||||||
def __get_config_cache(self, user_id: int, key: str) -> Any:
|
def __get_config_caches(self, username: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
获取配置缓存
|
获取配置缓存
|
||||||
"""
|
"""
|
||||||
if not user_id or not key:
|
if not username or not self.__USERCONF:
|
||||||
return None
|
return None
|
||||||
if not self.__USERCONF:
|
return self.__USERCONF.get(username)
|
||||||
|
|
||||||
|
def __get_config_cache(self, username: str, key: str) -> Any:
|
||||||
|
"""
|
||||||
|
获取配置缓存
|
||||||
|
"""
|
||||||
|
if not username or not key or not self.__USERCONF:
|
||||||
return None
|
return None
|
||||||
user_cache = self.__USERCONF.get(user_id)
|
user_cache = self.__get_config_caches(username)
|
||||||
if not user_cache:
|
if not user_cache:
|
||||||
return None
|
return None
|
||||||
return user_cache.get(key)
|
return user_cache.get(key)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user