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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| public final class RedisUtil {
private static String ADDR;
private static int PORT;
private static String AUTH;
private static int MAX_ACTIVE;
private static int MAX_IDLE;
private static int MAX_WAIT;
private static int TIMEOUT;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static { Resource resource = new ClassPathResource("config/redis.properties"); Properties props = null; try { props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } try { ADDR = props.getProperty("redis.ip"); PORT = Integer.valueOf(props.getProperty("redis.port")).intValue(); AUTH = props.getProperty("redis.pass"); TIMEOUT = Integer.valueOf(props.getProperty("redis.timeout")).intValue(); MAX_ACTIVE = Integer.valueOf(props.getProperty("redis.pool.maxTotal")).intValue(); MAX_IDLE = Integer.valueOf(props.getProperty("redis.pool.maxIdle")).intValue(); MAX_WAIT = Integer.valueOf(props.getProperty("redis.pool.maxWaitMillis")).intValue(); TEST_ON_BORROW = Boolean.parseBoolean(props.getProperty("redis.pool.testOnBorrow"));
JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(MAX_IDLE); config.setMaxTotal(MAX_ACTIVE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); if(StringUtil.isBlank(AUTH)){ jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT); }else{ jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } } catch (Exception e) { e.printStackTrace(); } }
public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { System.out.println("Redis异常:" + e.getMessage()); return null; } }
public static void gc(Jedis jedis){ if (jedis != null && jedisPool !=null) { jedisPool.returnResource(jedis); } }
public static Long zAdd(String key, Map<String, Double> scoreMembers){ Jedis jedis = null; try { jedis = getJedis(); if(jedis != null){ return jedis.zadd(key, scoreMembers); } }catch(Exception e) { }finally{ gc(jedis); } return 0L; }
public static Set<String> zRange(String key, long start, long end){ Jedis jedis = null; try { jedis = getJedis(); if(jedis != null){ return jedis.zrange(key, start, end); } }catch(Exception e) { }finally{ gc(jedis); } return null; }
public static Set<String> zRevrange(String key, long start, long end){ Jedis jedis = null; try { jedis = getJedis(); if(jedis != null){ return jedis.zrevrange(key, start, end); } }catch(Exception e) { }finally{ gc(jedis); } return null; }
public static Double zIncrby(String key, double score, String value){ Jedis jedis = null; try { jedis = getJedis(); if(jedis != null){ return jedis.zincrby(key, score, value); } }catch(Exception e) { }finally{ gc(jedis); } return 0D; }
private static final String LOCK_SUCCESS = "OK";
private static final String SET_IF_NOT_EXIST = "NX";
private static final String SET_WITH_EXPIRE_TIME = "PX";
public static boolean tryGetDistributedLock(String lockKey, String value, int expireTime){ Jedis jedis = null; String result = ""; try { jedis = getJedis(); if(jedis != null){ result = jedis.set(lockKey, value, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime); } }catch(Exception e) { }finally{ gc(jedis); } return LOCK_SUCCESS.equals(result); } }
public static Long del(String key){ Jedis jedis = null; try { jedis = getJedis(); if(jedis != null){ return jedis.del(key); } }catch(Exception e) {
}finally{ gc(jedis); } return 0L; }
|