“djh”
2024-12-04 d1549b8445c65a6775cf1ba093f5040ace0f87e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.gkhy.huataiFourierSpecialGasMonitor.infra.cache.service.impl;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.SystemCacheKeyEnum;
import com.gkhy.huataiFourierSpecialGasMonitor.infra.cache.domain.CacheUserInfo;
import com.gkhy.huataiFourierSpecialGasMonitor.infra.cache.service.UserCacheInfraService;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.concurrent.TimeUnit;
 
@Service
public class UserCacheInfraServiceImpl implements UserCacheInfraService {
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Override
    public CacheUserInfo getCacheUser(String userId) {
        if(userId == null || userId.isEmpty())
            return null;
        Object cacheUserObj = redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).get(""+userId);
        if(cacheUserObj != null){
            String json = (String)cacheUserObj;
            try {
                CacheUserInfo cacheUserInfo = objectMapper.readValue(json,CacheUserInfo.class);
                return cacheUserInfo;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    @Override
    public boolean putCacheUser(CacheUserInfo cacheUserInfo, String userId) {
        if(cacheUserInfo == null || userId == null || userId.isEmpty())
            return false;
        try {
            String json = objectMapper.writeValueAsString(cacheUserInfo);
            if(redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).put(userId,json,120,
                    TimeUnit.MINUTES) != null)
                return true;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public boolean removeCacheUser(String userId) {
        if(redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).remove(userId) != null)
            return true;
        else
            return false;
    }
}