http://blog.csdn.net/jiangtao_st/article/details/37699473(redis缓存)
http://blog.joshuawhite.com/java/caching-with-spring-data-redis/ (redis缓存)
http://www.cnblogs.com/edwinchen/p/3816938.html
https://github.com/xetorthio/jedis/releases
https://github.com/xetorthio/jedis
http://rayoo.iteye.com/blog/1963812
http://blog.97md.net/Doc/redis/
https://redis.readthedocs.org/en/latest/
http://zhaoqilong3031.iteye.com/blog/1483294
http://www.oschina.net/question/102776_13807
http://www.cnblogs.com/tankaixiong/p/3660075.html
http://www.redis.io/
http://www.cnblogs.com/tankaixiong/p/3660075.html
http://www.cnblogs.com/tankaixiong/p/3660075.html
http://blog.mkfree.com/posts/5147c478975a24f6a830e53d
http://my.oschina.net/u/1383439/blog/220871
http://bijian1013.iteye.com/blog/2099736
http://shift-alt-ctrl.iteye.com/blog/1885910
http://snowolf.iteye.com/blog/1633196
http://www.cnblogs.com/google4y/p/3360498.html
http://www.cnblogs.com/warden/p/onlinelist_base_on_redis.html
Java中使用Jedis操作Redis
使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip
如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip
1 package com.test; 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 TestRedis { 14 private Jedis jedis; 15 16 @Before 17 public void setup() { 18 //连接redis服务器,192.168.0.100:6379 19 jedis = new Jedis("192.168.0.100", 6379); 20 //权限认证 21 jedis.auth("admin"); 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","476777389"); 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 //移除noname 111 jedis.srem("user","who"); 112 System.out.println(jedis.smembers("user"));//获取所有加入的value 113 System.out.println(jedis.sismember("user", "who"));//判断 who 是否是user集合的元素 114 System.out.println(jedis.srandmember("user")); 115 System.out.println(jedis.scard("user"));//返回集合的元素个数 116 } 117 118 @Test 119 public void test() throws InterruptedException { 120 //jedis 排序 121 //注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) 122 jedis.del("a");//先清除数据,再加入数据进行测试 123 jedis.rpush("a", "1"); 124 jedis.lpush("a","6"); 125 jedis.lpush("a","3"); 126 jedis.lpush("a","9"); 127 System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] 128 System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果 129 System.out.println(jedis.lrange("a",0,-1)); 130 } 131 132 @Test 133 public void testRedisPool() { 134 RedisUtil.getJedis().set("newname", "中文测试"); 135 System.out.println(RedisUtil.getJedis().get("newname")); 136 } 137 }
Redis连接池:
1 package com.test; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 public final class RedisUtil { 8 9 //Redis服务器IP 10 private static String ADDR = "192.168.0.100"; 11 12 //Redis的端口号 13 private static int PORT = 6379; 14 15 //访问密码 16 private static String AUTH = "admin"; 17 18 //可用连接实例的最大数目,默认值为8; 19 //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 20 private static int MAX_ACTIVE = 1024; 21 22 //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 23 private static int MAX_IDLE = 200; 24 25 //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; 26 private static int MAX_WAIT = 10000; 27 28 private static int TIMEOUT = 10000; 29 30 //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; 31 private static boolean TEST_ON_BORROW = true; 32 33 private static JedisPool jedisPool = null; 34 35 /** 36 * 初始化Redis连接池 37 */ 38 static { 39 try { 40 JedisPoolConfig config = new JedisPoolConfig(); 41 config.setMaxActive(MAX_ACTIVE); 42 config.setMaxIdle(MAX_IDLE); 43 config.setMaxWait(MAX_WAIT); 44 config.setTestOnBorrow(TEST_ON_BORROW); 45 jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 51 /** 52 * 获取Jedis实例 53 * @return 54 */ 55 public synchronized static Jedis getJedis() { 56 try { 57 if (jedisPool != null) { 58 Jedis resource = jedisPool.getResource(); 59 return resource; 60 } else { 61 return null; 62 } 63 } catch (Exception e) { 64 e.printStackTrace(); 65 return null; 66 } 67 } 68 69 /** 70 * 释放jedis资源 71 * @param jedis 72 */ 73 public static void returnResource(final Jedis jedis) { 74 if (jedis != null) { 75 jedisPool.returnResource(jedis); 76 } 77 } 78 }
我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!
欢迎分享与转载