Blame view

java110-utils/src/main/java/com/java110/utils/cache/BaseCache.java 1.22 KB
88e030b7   王彪总   init project
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
  package com.java110.utils.cache;
  
  
  import com.java110.utils.factory.ApplicationContextFactory;
  
  import java.util.Set;
  
  /**
   * 缓存基类
   * Created by wuxw on 2018/4/14.
   */
  public class BaseCache {
  
      public final static String JEDIS_DEFAULT_POOL = "jedisClientTemplate"; // 单节点模式 jedisClientPool  集群模式 jedisClientCluster
  
  
  
      protected static Jedis getJedis(){
  //        JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
  //        return jedisPool.getResource();
          //Jedis jedis = (Jedis) ApplicationContextFactory.getBean("jedisClientCluster");
          Jedis jedis = (Jedis) ApplicationContextFactory.getBean(JEDIS_DEFAULT_POOL);
  
          return jedis;
      }
  
      /**
       * 删除数据
       * @param pattern
       */
      public static void removeData(String pattern){
          Jedis redis = null;
          try {
              redis = getJedis();
              Set<String> keys = redis.keys("*"+pattern);
              if(keys == null || keys.size() == 0){
                  return ;
              }
              for(String key : keys){
                  redis.del(key);
              }
          }finally {
              if(redis != null){
                  redis.close();
              }
          }
      }
  
  }