View Javadoc
1   package com.foxinmy.weixin4j.cache;
2   
3   import java.util.Set;
4   
5   import redis.clients.jedis.HostAndPort;
6   import redis.clients.jedis.JedisCluster;
7   import redis.clients.jedis.JedisPoolConfig;
8   
9   import com.foxinmy.weixin4j.util.Consts;
10  import com.foxinmy.weixin4j.util.SerializationUtils;
11  
12  /**
13   * 用Redis(集群)保存缓存对象(推荐使用)
14   *
15   * @className RedisClusterCacheStorager
16   * @author jinyu(foxinmy@gmail.com)
17   * @date 2016年5月28日
18   * @since JDK 1.6
19   */
20  public class RedisClusterCacheStorager<T extends Cacheable> implements
21  		CacheStorager<T> {
22  	private final static int CONNECTION_TIMEOUT = 5000;
23  	private final static int SO_TIMEOUT = 5000;
24  	private final static int MAX_REDIRECTIONS = 5;
25  	private final static int MAX_TOTAL = 50;
26  	private final static int MAX_IDLE = 5;
27  	private final static int MAX_WAIT_MILLIS = 5000;
28  	private final static boolean TEST_ON_BORROW = false;
29  	private final static boolean TEST_ON_RETURN = true;
30  	private final JedisCluster jedisCluster;
31  
32  	public RedisClusterCacheStorager(Set<HostAndPort> nodes) {
33  		JedisPoolConfig poolConfig = new JedisPoolConfig();
34  		poolConfig.setMaxTotal(MAX_TOTAL);
35  		poolConfig.setMaxIdle(MAX_IDLE);
36  		poolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
37  		poolConfig.setTestOnBorrow(TEST_ON_BORROW);
38  		poolConfig.setTestOnReturn(TEST_ON_RETURN);
39  		this.jedisCluster = new JedisCluster(nodes, CONNECTION_TIMEOUT,
40  				SO_TIMEOUT, MAX_REDIRECTIONS, poolConfig);
41  	}
42  
43  	public RedisClusterCacheStorager(Set<HostAndPort> nodes,
44  			JedisPoolConfig poolConfig) {
45  		this(nodes, CONNECTION_TIMEOUT, SO_TIMEOUT, MAX_REDIRECTIONS,
46  				poolConfig);
47  	}
48  
49  	public RedisClusterCacheStorager(Set<HostAndPort> nodes,
50  			int connectionTimeout, int soTimeout, int maxRedirections,
51  			JedisPoolConfig poolConfig) {
52  		this(new JedisCluster(nodes, connectionTimeout, soTimeout,
53  				maxRedirections, poolConfig));
54  	}
55  
56  	public RedisClusterCacheStorager(JedisCluster jedisCluster) {
57  		this.jedisCluster = jedisCluster;
58  	}
59  
60  	@SuppressWarnings("unchecked")
61  	@Override
62  	public T lookup(String key) {
63  		byte[] value = jedisCluster.get(key.getBytes(Consts.UTF_8));
64  		return value != null ? (T) SerializationUtils.deserialize(value) : null;
65  	}
66  
67  	@Override
68  	public void caching(String key, T cache) {
69  		byte[] cacheKey = key.getBytes(Consts.UTF_8);
70  		byte[] value = SerializationUtils.serialize(cache);
71  		if (cache.getExpires() > 0) {
72  			jedisCluster.setex(cacheKey,
73  					(int) (cache.getExpires() - CUTMS) / 1000, value);
74  		} else {
75  			jedisCluster.set(cacheKey, value);
76  		}
77  		jedisCluster.sadd(ALLKEY, key);
78  	}
79  
80  	@Override
81  	public T evict(String key) {
82  		T cache = lookup(key);
83  		jedisCluster.del(key);
84  		jedisCluster.srem(ALLKEY, key);
85  		return cache;
86  	}
87  
88  	@Override
89  	public void clear() {
90  		Set<String> cacheKeys = jedisCluster.smembers(ALLKEY);
91  		if (!cacheKeys.isEmpty()) {
92  			cacheKeys.add(ALLKEY);
93  			jedisCluster.del(cacheKeys.toArray(new String[cacheKeys.size()]));
94  		}
95  	}
96  }