Redis在java中的使用
1、首先安装 Redis 环境。可以在 Windows、linux、别的服务器上搭建 Redis 环境
2、在 java 工程中导入必须的 jar 包
如 maven 上导入两个 jar】
<!-- Redis 必须包 --> <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>
3、在项目中连接测试
1 package com.iov.redis; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import redis.clients.jedis.Jedis; 12 13 public class Redis { 14 private Jedis jedis; 15 16 @Before 17 public void setup() { 18 //连接 redis 服务器,192.168.0.100:6379 19 jedis = new Jedis("localhost", 6379); 20 //权限认证 21 jedis.auth("123"); 22 } 23 24 /** 25 * redis 存储字符串 26 */ 27 @Test 28 public void testString() { 29 //----- 添加数据 ---------- 30 jedis.set("name","xinxin");//向 key-->name 中放入了 value-->xinxin 31 System.out.println(jedis.get("name"));//执行结果:xinxin 32 33 jedis.append("name", "is my lover"); //拼接 34 System.out.println(jedis.get("name")); 35 36 jedis.del("name"); //删除某个键 37 System.out.println(jedis.get("name")); 38 //设置多个键值对 39 jedis.mset("name","liuling","age","23","qq","476777XXX"); 40 jedis.incr("age"); //进行加 1 操作 41 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); 42 43 } 44 45 /** 46 * redis 操作 Map 47 */ 48 @Test 49 public void testMap() { 50 //----- 添加数据 ---------- 51 Map<String, String> map = new HashMap<String, String>(); 52 map.put("name", "xinxin"); 53 map.put("age", "22"); 54 map.put("qq", "123456"); 55 jedis.hmset("user",map); 56 //取出 user 中的 name,执行结果:[minxr]--> 注意结果是一个泛型的 List 57 //第一个参数是存入 redis 中 map 对象的 key,后面跟的是放入 map 中的对象的 key,后面的 key 可以跟多个,是可变参数 58 List<String> rsmap = jedis.hmget("user", "name", "age", "qq"); 59 System.out.println(rsmap); 60 61 //删除 map 中的某个键值 62 jedis.hdel("user","age"); 63 System.out.println(jedis.hmget("user", "age")); //因为删除了,所以返回的是 null 64 System.out.println(jedis.hlen("user")); //返回 key 为 user 的键中存放的值的个数 2 65 System.out.println(jedis.exists("user"));//是否存在 key 为 user 的记录 返回 true 66 System.out.println(jedis.hkeys("user"));//返回 map 对象中的所有 key 67 System.out.println(jedis.hvals("user"));//返回 map 对象中的所有 value 68 69 Iterator<String> iter=jedis.hkeys("user").iterator(); 70 while (iter.hasNext()){ 71 String key = iter.next(); 72 System.out.println(key+":"+jedis.hmget("user",key)); 73 } 74 } 75 76 /** 77 * jedis 操作 List 78 */ 79 @Test 80 public void testList(){ 81 //开始前,先移除所有的内容 82 jedis.del("java framework"); 83 System.out.println(jedis.lrange("java framework",0,-1)); 84 //先向 key java framework 中存放三条数据 85 jedis.lpush("java framework","spring"); 86 jedis.lpush("java framework","struts"); 87 jedis.lpush("java framework","hibernate"); 88 //再取出所有数据 jedis.lrange 是按范围取出, 89 // 第一个是 key,第二个是起始位置,第三个是结束位置,jedis.llen 获取长度 -1 表示取得所有 90 System.out.println(jedis.lrange("java framework",0,-1)); 91 92 jedis.del("java framework"); 93 jedis.rpush("java framework","spring"); 94 jedis.rpush("java framework","struts"); 95 jedis.rpush("java framework","hibernate"); 96 System.out.println(jedis.lrange("java framework",0,-1)); 97 } 98 99 /** 100 * jedis 操作 Set 101 */ 102 @Test 103 public void testSet(){ 104 //添加 105 jedis.sadd("user","liuling"); 106 jedis.sadd("user","xinxin"); 107 jedis.sadd("user","ling"); 108 jedis.sadd("user","zhangxinxin"); 109 jedis.sadd("user","who"); 110 111 //移除 noname 112 jedis.srem("user","who"); 113 System.out.println(jedis.smembers("user"));//获取所有加入的 value 114 System.out.println(jedis.sismember("user", "who"));//判断 who 是否是 user 集合的元素 115 System.out.println(jedis.srandmember("user")); 116 System.out.println(jedis.scard("user"));//返回集合的元素个数 117 } 118 119 @Test 120 public void test() throws InterruptedException { 121 //jedis 排序 122 //注意,此处的 rpush 和 lpush 是 List 的操作。是一个双向链表(但从表现来看的) 123 jedis.del("a");//先清除数据,再加入数据进行测试 124 jedis.rpush("a", "1"); 125 jedis.lpush("a","6"); 126 jedis.lpush("a","3"); 127 jedis.lpush("a","9"); 128 System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] 129 System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果 130 System.out.println(jedis.lrange("a",0,-1)); 131 } 132 133 @Test 134 public void testRedisPool() { 135 RedisPool.getJedis().set("newname", "中文测试"); 136 137 Jedis jedis= RedisPool.getJedis(); 138 139 jedis.set("key","方良棉"); 140 jedis.keys("*"); 141 //jedis.flushAll(); 142 System.out.println(RedisPool.getJedis().get("newname")); 143 System.out.println(jedis.keys("*")); 144 } 145 }
数据连接池用法
package com.iov.redis;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public final class RedisPool {
</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 = "localhost"<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 = "123"<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.setMaxTotal(MAX_ACTIVE); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">config.setMaxActive(MAX_ACTIVE);</span>
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
//config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
} catch (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); } }
}