一、pox.xml
redis.clients jedis 2.9.0 org.apache.commons commons-pool2 2.4.2 junit junit 4.11 test
二、Java代码,Jedis工具类
1 package om.xbq.redis; 2 3 import java.util.List; 4 import java.util.Set; 5 import org.junit.Test; 6 import redis.clients.jedis.Jedis; 7 import redis.clients.jedis.JedisPool; 8 import redis.clients.jedis.JedisPoolConfig; 9 /** 10 * Jedis工具类 11 * @author xbq 12 * @created:2017-4-19 13 */ 14 public class JedisUtil { 15 16 private JedisPool pool; 17 private static String URL = "192.168.242.130"; 18 private static int PORT = 6379; 19 private static String PASSWORD = "xbq123"; 20 21 // ThreadLocal,给每个线程 都弄一份 自己的资源 22 private final static ThreadLocalthreadPool = new ThreadLocal (); 23 private final static ThreadLocal threadJedis = new ThreadLocal (); 24 25 private final static int MAX_TOTAL = 100; // 最大分配实例 26 private final static int MAX_IDLE = 50; // 最大空闲数 27 private final static int MAX_WAIT_MILLIS = -1; // 最大等待数 28 29 /** 30 * 获取 jedis池 31 * @return 32 */ 33 public JedisPool getPool(){ 34 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 35 // 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取,如果赋值为-1,则表示不限制; 36 // 如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) 37 jedisPoolConfig.setMaxTotal(MAX_TOTAL); 38 // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 39 jedisPoolConfig.setMaxIdle(MAX_IDLE); 40 // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException 41 jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); 42 43 final int timeout = 60 * 1000; 44 pool = new JedisPool(jedisPoolConfig, URL, PORT, timeout); 45 46 return pool; 47 } 48 49 /** 50 * 在jedis池中 获取 jedis 51 * @return 52 */ 53 private Jedis common(){ 54 // 从 threadPool中取出 jedis连接池 55 pool = threadPool.get(); 56 // 为空,则重新产生 jedis连接池 57 if(pool == null){ 58 pool = this.getPool(); 59 // 将jedis连接池维护到threadPool中 60 threadPool.set(pool); 61 } 62 // 在threadJedis中获取jedis实例 63 Jedis jedis = threadJedis.get(); 64 // 为空,则在jedis连接池中取出一个 65 if(jedis == null){ 66 jedis = pool.getResource(); 67 // 验证密码 68 jedis.auth(PASSWORD); 69 // 将jedis实例维护到threadJedis中 70 threadJedis.set(jedis); 71 } 72 return jedis; 73 } 74 75 /** 76 * 释放资源 77 */ 78 public void closeAll(){ 79 Jedis jedis = threadJedis.get(); 80 if(jedis != null){ 81 threadJedis.set(null); 82 JedisPool pool = threadPool.get(); 83 if(pool != null){ 84 // 释放连接,归还给连接池 85 pool.returnResource(jedis); 86 } 87 } 88 } 89 90 /** 91 * 判断key是否存在 92 * @param key 93 * @return 94 */ 95 public boolean existsKey(String key){ 96 Jedis jedis = this.common(); 97 return jedis.exists(key); 98 } 99 100 /**101 * 删除102 * @param key103 * @return104 */105 public Long delValue(String key){106 Jedis jedis = this.common();107 return jedis.del(key);108 }109 110 // ----------------------------对String类型的操作-----------------------------------------111 /**112 * 增加 修改113 * @param key114 * @param value115 */116 public String setValue(String key, String value) {117 Jedis jedis = this.common();118 return jedis.set(key, value);119 }120 121 /**122 * 查询123 * @param key124 * @return125 */126 public String getValue(String key){127 Jedis jedis = this.common();128 return jedis.get(key);129 }130 131 /**132 * 追加数据133 * @param key134 * @param value135 */136 public void appendValue(String key, String value){137 Jedis jedis = this.common();138 jedis.append(key, value);139 }140 141 /**142 * 测试 String143 */144 @Test145 public void testString(){146 if(this.existsKey("name")){147 System.out.println("这一个key存在了!");148 this.appendValue("name", "xbq6666");149 150 String name = this.getValue("name");151 System.out.println("name===" + name);152 153 long flag = this.delValue("name");154 System.out.println(flag);155 156 }else {157 this.setValue("name", "javaCoder");158 String name = this.getValue("name");159 System.out.println("name===" + name);160 }161 }162 163 // ----------------------------对List类型的操作------------------------------------------164 /**165 * 保存到链表166 * @param key167 * @param keys168 * @return169 */170 public long lpush(String key, String ...keys){171 Jedis jedis = this.common();172 return jedis.lpush(key, keys);173 }174 175 /**176 * 取出链表中的全部元素177 * @param key178 * @return179 */180 public List lrange(String key) {181 Jedis jedis = this.common();182 return jedis.lrange(key, 0, -1);183 }184 185 /**186 * 查询出链表中的元素个数187 * @param key188 * @return189 */190 public long llen(String key){191 Jedis jedis = this.common();192 return jedis.llen(key);193 }194 195 /**196 * 取出链表中的头部元素197 * @param key198 * @return199 */200 public String lpop(String key){201 Jedis jedis = this.common();202 return jedis.lpop(key);203 }204 205 // ----------------------------对Hash类型的操作------------------------------------------206 /**207 * 添加208 * @param key209 * @param field210 * @param value211 * @return212 */213 public long hset(String key, String field, String value) {214 Jedis jedis = this.common();215 return jedis.hset(key, field, value);216 }217 218 /**219 * 查询220 * @param key221 * @param field222 * @return223 */224 public String hget(String key, String field){225 Jedis jedis = this.common();226 return jedis.hget(key, field);227 }228 229 /**230 * 判断 key 中的field 是否存在231 * @param key232 * @param field233 * @return234 */235 public boolean hexists(String key, String field){236 Jedis jedis = this.common();237 return jedis.hexists(key, field);238 }239 240 /**241 * 删除242 * @param key243 * @param fields244 * @return245 */246 public long hdel(String key, String ...fields){247 Jedis jedis = this.common();248 return jedis.hdel(key, fields);249 }250 251 // ----------------------------对Set类型的操作--------------------------------------------252 /**253 * 添加元素254 * @param key255 * @param members256 * @return257 */258 public long sadd(String key, String ...members){259 Jedis jedis = this.common();260 return jedis.sadd(key, members);261 }262 263 /**264 * 查询出set中的所有元素265 * @param key266 * @return267 */268 public Set sMember(String key){269 Jedis jedis = this.common();270 return jedis.smembers(key);271 }272 273 /**274 * 查询出 set中元素的个数275 * @param key276 * @return277 */278 public long scard(String key){279 Jedis jedis = this.common();280 return jedis.scard(key);281 }282 283 // ----------------------------对ZSet类型的操作--------------------------------------------284 /**285 * 在zset中添加元素286 * @param key287 * @param score288 * @param member289 * @return290 */291 public long zadd(String key, double score ,String member){292 Jedis jedis = this.common();293 return jedis.zadd(key, score, member);294 }295 296 /**297 * 查询所有元素298 * @param key299 * @return300 */301 public Set zrange(String key){302 Jedis jedis = this.common();303 return jedis.zrange(key, 0, -1);304 }305 306 /**307 * 查询zset中的元素个数308 * @param key309 * @return310 */311 public long zcard(String key){312 Jedis jedis = this.common();313 return jedis.zcard(key);314 }315 316 /**317 * 删除zset中的一个 或者多个元素318 * @param key319 * @param members320 * @return321 */322 public long zrem(String key, String ...members){323 Jedis jedis = this.common();324 return jedis.zrem(key, members);325 }326 }