Redis(五)-- Java API

一、pox.xml

  <dependencies>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

二、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 ThreadLocal<JedisPool> threadPool = new ThreadLocal<JedisPool>();
 23     private final static ThreadLocal<Jedis> threadJedis = new ThreadLocal<Jedis>();
 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 key
103      * @return
104      */
105     public Long delValue(String key){
106         Jedis jedis = this.common();
107         return jedis.del(key);
108     }
109     
110     // ----------------------------对String类型的操作-----------------------------------------
111     /**
112      * 增加  修改
113      * @param key
114      * @param value
115      */
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 key
124      * @return
125      */
126     public String getValue(String key){
127         Jedis jedis = this.common();
128         return jedis.get(key);
129     }
130     
131     /**
132      * 追加数据
133      * @param key
134      * @param value
135      */
136     public void appendValue(String key, String value){
137         Jedis jedis = this.common();
138         jedis.append(key, value);
139     }
140     
141     /**
142      * 测试 String
143      */
144     @Test
145     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 key
167      * @param keys
168      * @return
169      */
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 key
178      * @return
179      */
180     public List<String> lrange(String key) {
181         Jedis jedis = this.common();
182         return jedis.lrange(key, 0, -1);
183     }
184     
185     /**
186      * 查询出链表中的元素个数
187      * @param key
188      * @return
189      */
190     public long llen(String key){
191         Jedis jedis = this.common();
192         return jedis.llen(key);
193     }
194     
195     /**
196      * 取出链表中的头部元素
197      * @param key
198      * @return
199      */
200     public String lpop(String key){
201         Jedis jedis = this.common();
202         return jedis.lpop(key);
203     }
204     
205     // ----------------------------对Hash类型的操作------------------------------------------
206     /**
207      * 添加
208      * @param key
209      * @param field
210      * @param value
211      * @return
212      */
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 key
221      * @param field
222      * @return
223      */
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 key
232      * @param field
233      * @return
234      */
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 key
243      * @param fields
244      * @return
245      */
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 key
255      * @param members
256      * @return
257      */
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 key
266      * @return
267      */
268     public Set<String> sMember(String key){
269         Jedis jedis = this.common();
270         return jedis.smembers(key);
271     }
272     
273     /**
274      * 查询出 set中元素的个数
275      * @param key
276      * @return
277      */
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 key
287      * @param score
288      * @param member
289      * @return
290      */
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 key
299      * @return
300      */
301     public Set<String> zrange(String key){
302         Jedis jedis = this.common();
303         return jedis.zrange(key, 0, -1);
304     }
305     
306     /**
307      * 查询zset中的元素个数
308      * @param key
309      * @return
310      */
311     public long zcard(String key){
312         Jedis jedis = this.common();
313         return jedis.zcard(key);
314     }
315     
316     /**
317      * 删除zset中的一个 或者多个元素
318      * @param key
319      * @param members
320      * @return
321      */
322     public long zrem(String key, String ...members){
323         Jedis jedis = this.common();
324         return jedis.zrem(key, members);
325     }
326 }
原文地址:https://www.cnblogs.com/xbq8080/p/6733694.html