package com.iplatform.chat.cache; import com.iplatform.chat.ChatSession; import com.iplatform.chat.ChatSessionCache; import com.iplatform.chat.Constants; import com.iplatform.chat.service.ChatDialogServiceImpl; import com.iplatform.chat.util.ChatUtils; import com.iplatform.model.po.ChatDialog; import com.walker.cache.AbstractCacheProvider; import com.walker.cache.Cache; import com.walker.infrastructure.utils.StringUtils; import java.util.List; public class LocalChatSessionCache extends AbstractCacheProvider implements ChatSessionCache { @Override public ChatSession get(long id) { ChatSession session = this.getCacheData(String.valueOf(id)); if(session == null){ ChatDialog dialog = this.chatDialogService.get(new ChatDialog(id)); if(dialog == null){ return null; } session = ChatUtils.acquireChatSession(dialog); this.putCacheData(String.valueOf(id), session); } return session; } @Override public void save(ChatSession category) { this.putCacheData(String.valueOf(category.getId()), category); } @Override public void update(ChatSession category) { this.updateCacheData(String.valueOf(category.getId()), category); } @Override public void remove(long id) { this.removeCacheData(String.valueOf(id)); } @Override protected int loadDataToCache(Cache cache) { List hosts = this.chatDialogService.selectAll(new ChatDialog()); if(!StringUtils.isEmptyList(hosts)){ for(ChatDialog h : hosts){ cache.put(String.valueOf(h.getId()), ChatUtils.acquireChatSession(h)); } return hosts.size(); } return 0; } @Override public String getProviderName() { return Constants.CACHE_NAME_CHAT_SESSION; } @Override public Class getProviderType() { return ChatSession.class; } public void setChatDialogService(ChatDialogServiceImpl chatDialogService) { this.chatDialogService = chatDialogService; } private ChatDialogServiceImpl chatDialogService; }