派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.qxueyou.scc.base.handler;
 
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.springframework.stereotype.Repository;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.base.dao.CommonRedisTemplate;
 
import redis.clients.jedis.ShardedJedis;
 
@Repository(value="qCacheRedis")
public class QCacheRedis extends CommonRedisTemplate{
    
    private static String BUCKET_HEADER_KEY="bucketHeaderKey";
    
    @SuppressWarnings("unchecked")
    public <T> T annocacheHget(String bucket,String cachekey, Class<T> cls) {
        ShardedJedis client = null;
        T result = null;
        try {
            client = this.getJedisClient();
            byte [] arrBytes =  client.hget(bucket.getBytes(), cachekey.getBytes());
            String strJson =null;
            Object obj = null;
            if(arrBytes!=null){
                strJson = new String(arrBytes);
                obj = JSON.parse(strJson);
            }
//            Object obj = ReisUtils.unserialize(client.hget(bucket.getBytes(), cachekey.getBytes()));
            if(obj!=null){
                Map<String, Object> valueMap =(Map<String, Object>)obj;
                //判断是否过期
                if(checkIsExpire((Long)valueMap.get("expireTime"))){
                    //删除键
                    client.hdel(bucket.getBytes(), cachekey.getBytes());
                    result = null;
                }else{
                    result = JSONObject.toJavaObject((JSON)valueMap.get("target"), cls);
                }
            }
        } finally {
            this.closeJedisClient(client);
        }
        return result;
    }
    
    public Long annocacheHset(String bucket, String cachekey, Object value,int timeout) {
        ShardedJedis client = null;
        try {
            client = this.getJedisClient();
            if(!client.exists(bucket.getBytes())){
                client.hsetnx(bucket.getBytes(), BUCKET_HEADER_KEY.getBytes(), "".getBytes());
            }
            Map<String, Object> valueMap = new HashMap<String,Object>();
            
            if(timeout>=0){
                valueMap.put("expireTime", Calendar.getInstance().getTime().getTime() + timeout * 1000);
            }else{
                valueMap.put("expireTime", -1L);
            }
            
            valueMap.put("target",value);
            
            String strJsonString = JSON.toJSONString(valueMap);
            
            return client.hset(bucket.getBytes(), cachekey.getBytes(), strJsonString.getBytes());
        }finally {
            this.closeJedisClient(client);
        }
    }
    
    /** 校验是否过期 */
    private boolean checkIsExpire(long expireTime){
        //负数表示永不过期
        if(expireTime>0){
            return new Date(expireTime).compareTo(Calendar.getInstance().getTime())==-1;    
        }else{
            return false;
        }
    }
}