java Redis工具类
redis 就是一个 nosql 数据库,做存储做缓存的,java 代码中就是嵌入了一个客户端,读取与存储数据而已。
先来一个简单的工具类:
package com.ming.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;/**
- RedisUtil 工具类
- @author Administrator
*/
public final class RedisUtil {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Redis服务器IP</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String ADDR = "127.0.0.1"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Redis的端口号</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> PORT = 6379<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">访问密码</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String AUTH = "123456"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可用连接实例的最大数目,默认值为8; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_ACTIVE = 1024<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_IDLE = 200<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_WAIT = 10000<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> TIMEOUT = 10000<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> TEST_ON_BORROW = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisPool jedisPool = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 初始化Redis连接池 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 获取Jedis实例 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedisPool != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { Jedis resource </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> resource; } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 释放jedis资源 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> returnResource(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { jedisPool.returnResource(jedis); } }
}
一些测试代码:
package com.ming.redis;import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;import org.junit.Before;
import org.junit.Test;import redis.clients.jedis.Jedis;
/**
- Redis Test
- @author Administrator
*/
public class TestRedis {jedis.append("name", "is my lover"); //拼接 System.out.println(jedis.get("name"));</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis; @Before </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setup() { jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("127.0.0.1", 6379<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">权限认证</span> jedis.auth("123456"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * redis存储字符串 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testString() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-----添加数据---------- </span> jedis.set("name","xinxin");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向key-->name中放入了value-->xinxin </span> System.out.println(jedis.get("name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行结果:xinxin </span>
Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key));} }jedis.del(</span>"name"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除某个键</span> System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置多个键值对</span> jedis.mset("name","liuling","age","23","qq","476777XXX"<span style="color: rgba(0, 0, 0, 1)">); jedis.incr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">进行加1操作</span> System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * redis操作Map </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMap() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-----添加数据---------- </span> Map<String, String> map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String, String><span style="color: rgba(0, 0, 0, 1)">(); map.put(</span>"name", "xinxin"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"age", "22"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"qq", "123456"<span style="color: rgba(0, 0, 0, 1)">); jedis.hmset(</span>"user"<span style="color: rgba(0, 0, 0, 1)">,map); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数 </span> List<String> rsmap = jedis.hmget("user", "name", "age", "qq"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(rsmap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除map中的某个键值 </span> jedis.hdel("user","age"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.hmget(</span>"user", "age")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为删除了,所以返回的是null </span> System.out.println(jedis.hlen("user")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回key为user的键中存放的值的个数2 </span> System.out.println(jedis.exists("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否存在key为user的记录 返回true </span> System.out.println(jedis.hkeys("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有key </span> System.out.println(jedis.hvals("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有value </span>
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * jedis操作List </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始前,先移除所有的内容 </span> jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先向key java framework中存放三条数据 </span> jedis.lpush("java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">再取出所有数据jedis.lrange是按范围取出, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 </span> System.out.println(jedis.lrange("java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * jedis操作Set </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加 </span> jedis.sadd("user","liuling"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","xinxin"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","ling"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","who"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">移除noname </span> jedis.srem("user","who"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.smembers(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有加入的value </span> System.out.println(jedis.sismember("user", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断 who 是否是user集合的元素 </span> System.out.println(jedis.srandmember("user"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.scard(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回集合的元素个数 </span>
}
@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> test() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis 排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) </span> jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先清除数据,再加入数据进行测试 </span> jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","6"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","3"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","9"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"a",0,-1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [9, 3, 6, 1] </span> System.out.println(jedis.sort("a")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[1, 3, 6, 9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果 </span> System.out.println(jedis.lrange("a",0,-1<span style="color: rgba(0, 0, 0, 1)">)); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testRedisPool() { RedisUtil.getJedis().set(</span>"newname", "中文测试"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(RedisUtil.getJedis().get(</span>"newname"<span style="color: rgba(0, 0, 0, 1)">)); }
}
ok, 一般 web 项目采用此工具类使用就是了呃.....