View Javadoc
1   package com.foxinmy.weixin4j.cache;
2   
3   import java.util.Set;
4   
5   import com.foxinmy.weixin4j.util.Consts;
6   import com.foxinmy.weixin4j.util.SerializationUtils;
7   
8   import redis.clients.jedis.Jedis;
9   import redis.clients.jedis.JedisPool;
10  import redis.clients.jedis.JedisPoolConfig;
11  import redis.clients.jedis.util.Pool;
12  
13  /**
14   * 用Redis保存缓存对象(推荐使用)
15   *
16   * @className RedisCacheStorager
17   * @author jinyu(foxinmy@gmail.com)
18   * @date 2015年1月9日
19   * @since JDK 1.6
20   */
21  public class RedisCacheStorager<T extends Cacheable> implements CacheStorager<T> {
22  
23      private Pool<Jedis> jedisPool;
24  
25      private final static String HOST = "127.0.0.1";
26      private final static int PORT = 6379;
27      private final static int TIMEOUT = 5000;
28      private final static int MAX_TOTAL = 50;
29      private final static int MAX_IDLE = 5;
30      private final static int MAX_WAIT_MILLIS = 5000;
31      private final static boolean TEST_ON_BORROW = false;
32      private final static boolean TEST_ON_RETURN = true;
33      private final static JedisPoolConfig POOLCONFIG;
34      static {
35          POOLCONFIG = new JedisPoolConfig();
36          POOLCONFIG.setMaxTotal(MAX_TOTAL);
37          POOLCONFIG.setMaxIdle(MAX_IDLE);
38          POOLCONFIG.setMaxWaitMillis(MAX_WAIT_MILLIS);
39          POOLCONFIG.setTestOnBorrow(TEST_ON_BORROW);
40          POOLCONFIG.setTestOnReturn(TEST_ON_RETURN);
41      }
42  
43      public RedisCacheStorager() {
44          this(HOST, PORT, TIMEOUT);
45      }
46  
47      public RedisCacheStorager(String host, int port, int timeout) {
48          this(host, port, timeout, null, POOLCONFIG);
49      }
50  
51      public RedisCacheStorager(String host, int port, int timeout, String password) {
52          this(host, port, timeout, password, POOLCONFIG);
53      }
54  
55      public RedisCacheStorager(JedisPoolConfig poolConfig) {
56          this(new JedisPool(poolConfig, HOST, PORT, TIMEOUT));
57      }
58  
59      public RedisCacheStorager(String host, int port, int timeout, String password, JedisPoolConfig poolConfig) {
60          this(new JedisPool(poolConfig, host, port, timeout, password));
61      }
62  
63      public RedisCacheStorager(Pool<Jedis> jedisPool) {
64          this.jedisPool = jedisPool;
65      }
66  
67      @SuppressWarnings("unchecked")
68      @Override
69      public T lookup(String key) {
70          Jedis jedis = null;
71          try {
72              jedis = jedisPool.getResource();
73              byte[] value = jedis.get(key.getBytes(Consts.UTF_8));
74              return value != null ? (T) SerializationUtils.deserialize(value) : null;
75          } finally {
76              if (jedis != null) {
77                  jedis.close();
78              }
79          }
80      }
81  
82      @Override
83      public void caching(String key, T cache) {
84          Jedis jedis = null;
85          try {
86              jedis = jedisPool.getResource();
87              byte[] cacheKey = key.getBytes(Consts.UTF_8);
88              byte[] value = SerializationUtils.serialize(cache);
89              if (cache.getExpires() > 0) {
90                  jedis.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000, value);
91              } else {
92                  jedis.set(cacheKey, value);
93              }
94              jedis.sadd(ALLKEY, key);
95          } finally {
96              if (jedis != null) {
97                  jedis.close();
98              }
99          }
100     }
101 
102     @Override
103     public T evict(String key) {
104         T cache = lookup(key);
105         Jedis jedis = null;
106         try {
107             jedis = jedisPool.getResource();
108             jedis.del(key);
109             jedis.srem(ALLKEY, key);
110         } finally {
111             if (jedis != null) {
112                 jedis.close();
113             }
114         }
115         return cache;
116     }
117 
118     @Override
119     public void clear() {
120         Jedis jedis = null;
121         try {
122             jedis = jedisPool.getResource();
123             Set<String> cacheKeys = jedis.smembers(ALLKEY);
124             if (!cacheKeys.isEmpty()) {
125                 cacheKeys.add(ALLKEY);
126                 jedis.del(cacheKeys.toArray(new String[cacheKeys.size()]));
127             }
128         } finally {
129             if (jedis != null) {
130                 jedis.close();
131             }
132         }
133     }
134 }