Class ActiveThreadCacheService

java.lang.Object
com.bytedesk.core.thread.ActiveThreadCacheService

@Service public class ActiveThreadCacheService extends Object
活跃会话缓存服务 使用 Redis Hash 存储活跃服务会话的关键信息,避免频繁查询数据库 缓存策略: 1. 会话创建(状态为 CHATTING/ROBOTING/QUEUING)时加入缓存 2. 会话关闭(状态为 CLOSED)时从缓存移除 3. 会话状态变更时更新缓存 4. 消息发送时更新 updatedAtMillis
  • Field Details

    • stringRedisTemplate

      private final org.springframework.data.redis.core.StringRedisTemplate stringRedisTemplate
  • Constructor Details

    • ActiveThreadCacheService

      public ActiveThreadCacheService()
  • Method Details

    • addOrUpdateActiveThread

      public void addOrUpdateActiveThread(ThreadEntity thread)
      添加或更新活跃会话到缓存
    • removeActiveThread

      public void removeActiveThread(String threadUid)
      从缓存移除活跃会话
    • getActiveThread

      public Optional<ActiveThreadCache> getActiveThread(String threadUid)
      获取单个活跃会话缓存
    • getAllActiveServiceThreads

      public List<ActiveThreadCache> getAllActiveServiceThreads()
      获取所有活跃服务会话缓存 用于替代数据库查询 findServiceThreadStateStarted
    • updateThreadTimestamp

      public void updateThreadTimestamp(String threadUid)
      更新会话的最后更新时间 用于消息发送时更新
    • updateThreadStatus

      public void updateThreadStatus(String threadUid, String newStatus)
      更新会话状态
    • getActiveThreadCount

      public long getActiveThreadCount()
      获取活跃会话数量
    • isServiceThread

      private boolean isServiceThread(ThreadEntity thread)
      判断是否为服务类型会话
    • isServiceType

      public boolean isServiceType(String type)
      判断是否为服务类型(基于缓存数据)
    • clearAllActiveThreads

      public void clearAllActiveThreads()
      清空所有活跃会话缓存 慎用!仅用于系统维护
    • rebuildCacheFromDatabase

      public void rebuildCacheFromDatabase(List<ThreadEntity> threads)
      从数据库重建缓存 用于系统启动或缓存失效时的恢复