类 UserCache

java.lang.Object
net.sohelp.boot.admin.core.cache.UserCache
所有已实现的接口:
ISohelpCache

@Component public class UserCache extends Object implements ISohelpCache
用户缓存(永不过期,按需失效)

存储策略:

  • Key "list" → 全量用户 JSON 列表(用于 list() 查询)
  • Key "u:{userId}" → 单用户 JSON Map(用于按 ID 查询)

NONE 模式每次直接查询 pb_users 表。

从以下版本开始:
2023/7/21 15:16
作者:
AaronFung
  • 构造器详细资料

    • UserCache

      public UserCache()
  • 方法详细资料

    • init

      @PostConstruct public void init()
      初始化缓存提供者。

      在 Bean 构造完成后由 Spring 自动调用,创建名为 "user"、永不过期(TTL=0)的缓存提供者。

    • cacheName

      public String cacheName()
      获取缓存名称。
      指定者:
      cacheName 在接口中 ISohelpCache
      返回:
      缓存名称,固定为 "user"
    • cacheDescription

      public String cacheDescription()
      获取缓存的中文描述。
      指定者:
      cacheDescription 在接口中 ISohelpCache
      返回:
      缓存描述,固定为 "用户缓存"
    • list

      @Comment("\u83b7\u53d6\u6240\u6709\u7528\u6237\u5217\u8868") public List<Map<String,Object>> list() throws SQLException
      获取所有用户列表。

      若缓存未启用(NONE 模式),直接查询数据库;否则优先从缓存读取,缓存未命中时触发全量加载。

      返回:
      所有用户的列表,每个用户为一个 Map
      抛出:
      SQLException - 数据库查询异常
    • getByUserName

      public Map<String,Object> getByUserName(String userName) throws SQLException
      根据用户姓名(user_name)查询用户信息。
      参数:
      userName - 用户姓名
      返回:
      匹配的用户 Map,未找到时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getByLoginName

      public Map<String,Object> getByLoginName(String loginName) throws SQLException
      根据登录名(login_name)查询用户信息。
      参数:
      loginName - 登录名
      返回:
      匹配的用户 Map,未找到时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getUser

      @Comment("\u83b7\u53d6\u7528\u6237\u4fe1\u606f") public Map<String,Object> getUser(long userId) throws SQLException
      根据用户ID获取用户摘要信息(仅包含 id、user_name、avatar)。
      参数:
      userId - 用户ID
      返回:
      包含用户摘要信息的 Map,未找到时返回空 Map
      抛出:
      SQLException - 数据库查询异常
    • getUsers

      public List<Map<String,Object>> getUsers(List<Long> users) throws SQLException
      批量获取用户摘要信息(仅包含 id、user_name、avatar)。
      参数:
      users - 用户ID列表
      返回:
      匹配的用户摘要列表,入参为空时返回空列表
      抛出:
      SQLException - 数据库查询异常
    • reload

      @Comment("\u542f\u52a8\uff0c\u521d\u59cb\u5316\u5230\u7f13\u5b58") public void reload() throws SQLException
      全量重新加载用户缓存。

      先清空所有缓存,然后从数据库查询全量用户,分别以列表和单用户两种方式写入缓存。

      指定者:
      reload 在接口中 ISohelpCache
      抛出:
      SQLException - 数据库查询异常
    • reload

      @Comment("\u6839\u636e\u7528\u6237id\u521d\u59cb\u5316\u5230\u7f13\u5b58\u4e2d") public void reload(Object userId) throws SQLException
      按用户ID重新加载单个用户的缓存。

      先移除该用户的旧缓存和列表缓存,再从数据库查询最新数据并写入缓存。

      参数:
      userId - 用户ID
      抛出:
      SQLException - 数据库查询异常
    • getNickname

      @Comment("\u83b7\u53d6\u7528\u6237\u59d3\u540d") public String getNickname(Long userId) throws SQLException
      获取用户昵称。
      参数:
      userId - 用户ID
      返回:
      用户昵称,用户不存在时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getPowerType

      @Comment("\u83b7\u53d6\u7528\u6237\u6743\u9650\u7c7b\u578b") public Integer getPowerType(Long userId) throws SQLException
      获取用户权限类型。
      参数:
      userId - 用户ID
      返回:
      权限类型值,用户不存在时返回 -1
      抛出:
      SQLException - 数据库查询异常
    • getUserName

      @Comment("\u83b7\u53d6\u7528\u6237\u59d3\u540d") public String getUserName(Long userId) throws SQLException
      获取用户姓名(user_name 字段)。
      参数:
      userId - 用户ID
      返回:
      用户姓名,用户不存在时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getUserAvatar

      @Comment("\u83b7\u53d6\u7528\u6237\u5934\u50cf") public String getUserAvatar(Long userId) throws SQLException
      获取用户头像的 Base64 编码字符串。
      参数:
      userId - 用户ID
      返回:
      头像的 Base64 编码,无头像或文件不存在时返回空字符串
      抛出:
      SQLException - 数据库查询异常
    • getUserNo

      @Comment("\u83b7\u53d6\u7528\u6237\u7f16\u53f7") public String getUserNo(Long userId) throws SQLException
      获取用户编号(user_no 字段)。
      参数:
      userId - 用户ID
      返回:
      用户编号,用户不存在时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getLoginName

      @Comment("\u6839\u636e\u7528\u6237ID\u83b7\u53d6\u767b\u5f55\u540d\u79f0") public String getLoginName(Long userId) throws SQLException
      根据用户ID获取登录名称(login_name 字段)。
      参数:
      userId - 用户ID
      返回:
      登录名称,用户不存在时返回 null
      抛出:
      SQLException - 数据库查询异常
    • getUserMap

      @Comment("\u6839\u636e\u7528\u6237ID\u83b7\u53d6\u7528\u6237\u8be6\u60c5") public Map<String,Object> getUserMap(Long userId) throws SQLException
      根据用户ID获取用户完整详情 Map。

      若缓存未启用则直接查库;否则优先读缓存,缓存未命中时按需加载。

      参数:
      userId - 用户ID,为 null 时返回空 Map
      返回:
      用户详情 Map,用户不存在时返回空 Map
      抛出:
      SQLException - 数据库查询异常
    • isDeveloper

      @Comment("\u662f\u5426\u4e3a\u5f00\u53d1\u4eba\u5458") public boolean isDeveloper(Long userId) throws SQLException
      判断指定用户是否为开发人员。
      参数:
      userId - 用户ID
      返回:
      true 表示是开发人员
      抛出:
      SQLException - 数据库查询异常
    • isAdministrator

      @Comment("\u662f\u5426\u4e3a\u7ba1\u7406\u5458") public boolean isAdministrator(Long userId) throws SQLException
      判断指定用户是否为管理员。
      参数:
      userId - 用户ID
      返回:
      true 表示是管理员
      抛出:
      SQLException - 数据库查询异常
    • isLoginEnabled

      @Comment("\u662f\u5426\u542f\u7528\u5e10\u6237") public boolean isLoginEnabled(Long userId) throws SQLException
      判断指定用户的账户是否启用(状态为 "1" 表示启用)。
      参数:
      userId - 用户ID
      返回:
      true 表示账户已启用
      抛出:
      SQLException - 数据库查询异常
    • getTenantId

      @Comment("\u6839\u636e\u7528\u6237ID\u83b7\u53d6\u7528\u6237\u5f53\u524d\u6240\u5728\u79df\u6237ID") public Long getTenantId(Long userId) throws SQLException
      根据用户ID获取用户当前所在租户ID。
      参数:
      userId - 用户ID,为 null 时返回 null
      返回:
      租户ID,用户不存在时返回 null
      抛出:
      SQLException - 数据库查询异常
    • remove

      @Comment("\u6839\u636e\u7528\u6237ID\u6e05\u7a7a\u7528\u6237\u7f13\u5b58") public void remove(Object userId)
      根据用户ID清空该用户的缓存。

      同时移除单用户缓存和全量列表缓存,确保下次访问时能从数据库获取最新数据。

      参数:
      userId - 用户ID,为 null 时不做任何操作