package com.test
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
public class TestRedis {
private Jedis jedis
@Before
public void setup() {
//连接redis服务器,192.168.0.100:6379
jedis = new Jedis("192.168.0.100", 6379)
//权限认证
jedis.auth("admin")
}
@Test
public void testString() {
//-----添加数据----------
jedis.set("name","xinxin")
System.out.println(jedis.get("name"))
jedis.append("name", " is my lover")
System.out.println(jedis.get("name"))
jedis.del("name")
System.out.println(jedis.get("name"))
//设置多个键值对
jedis.mset("name","liuling","age","23","qq","476777XXX")
jedis.incr("age")
System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"))
}
@Test
public void testMap() {
//-----添加数据----------
Map<String, String> map = new HashMap<String, String>()
map.put("name", "xinxin")
map.put("age", "22")
map.put("qq", "123456")
jedis.hmset("user",map)
//取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List
//第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数
List<String> rsmap = jedis.hmget("user", "name", "age", "qq")
System.out.println(rsmap)
//删除map中的某个键值
jedis.hdel("user","age")
System.out.println(jedis.hmget("user", "age"))
System.out.println(jedis.hlen("user"))
System.out.println(jedis.exists("user"))
System.out.println(jedis.hkeys("user"))
System.out.println(jedis.hvals("user"))
Iterator<String> iter=jedis.hkeys("user").iterator()
while (iter.hasNext()){
String key = iter.next()
System.out.println(key+":"+jedis.hmget("user",key))
}
}
@Test
public void testList(){
//开始前,先移除所有的内容
jedis.del("java framework")
System.out.println(jedis.lrange("java framework",0,-1))
//先向key java framework中存放三条数据
jedis.lpush("java framework","spring")
jedis.lpush("java framework","struts")
jedis.lpush("java framework","hibernate")
//再取出所有数据jedis.lrange是按范围取出,
// 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有
System.out.println(jedis.lrange("java framework",0,-1))
jedis.del("java framework")
jedis.rpush("java framework","spring")
jedis.rpush("java framework","struts")
jedis.rpush("java framework","hibernate")
System.out.println(jedis.lrange("java framework",0,-1))
}
@Test
public void testSet(){
//添加
jedis.sadd("user","liuling")
jedis.sadd("user","xinxin")
jedis.sadd("user","ling")
jedis.sadd("user","zhangxinxin")
jedis.sadd("user","who")
//移除noname
jedis.srem("user","who")
System.out.println(jedis.smembers("user"))
System.out.println(jedis.sismember("user", "who"))
System.out.println(jedis.srandmember("user"))
System.out.println(jedis.scard("user"))
}
@Test
public void test() throws InterruptedException {
//jedis 排序
//注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)
jedis.del("a")
jedis.rpush("a", "1")
jedis.lpush("a","6")
jedis.lpush("a","3")
jedis.lpush("a","9")
System.out.println(jedis.lrange("a",0,-1))
System.out.println(jedis.sort("a"))
System.out.println(jedis.lrange("a",0,-1))
}
@Test
public void testRedisPool() {
RedisUtil.getJedis().set("newname", "中文测试")
System.out.println(RedisUtil.getJedis().get("newname"))
}
}
package com.test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class RedisUtil {
private static String ADDR = "192.168.0.100";
private static int PORT = 6379;
private static String AUTH = "admin";
private static int MAX_ACTIVE = 1024;
private static int MAX_IDLE = 200;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
原文:http://www.cnblogs.com/liuling/p/2014-4-19-04.html