package com.yami.trading.service; import com.yami.trading.common.constants.UserRedisKeys; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Service public class OnlineUserService { @Autowired private RedisTemplate redisTemplate; public Date get(String partyId) { return (Date) redisTemplate.opsForValue().get(UserRedisKeys.ONLINEUSER_PARTYID + partyId); } public List getAll() { Map map = (Map) redisTemplate.opsForValue().get(UserRedisKeys.ONLINEUSER); if (map != null && !map.isEmpty()){ return new ArrayList(map.keySet()); } return new ArrayList(); } public void put(String partyId, Date date) { redisTemplate.opsForValue().set(UserRedisKeys.ONLINEUSER_PARTYID + partyId, date); Map map = (Map) redisTemplate.opsForValue().get(UserRedisKeys.ONLINEUSER); if (map == null) { map = new ConcurrentHashMap(); } map.put(partyId, date); redisTemplate.opsForValue().set(UserRedisKeys.ONLINEUSER, map); } public void del(String partyId) { redisTemplate.delete(UserRedisKeys.ONLINEUSER_PARTYID + partyId); Map map = (Map) redisTemplate.opsForValue().get(UserRedisKeys.ONLINEUSER); if (map != null && !map.isEmpty()) { map.remove(partyId); redisTemplate.opsForValue().set(UserRedisKeys.ONLINEUSER, map); } } }