下载redis
1、新增start.bat
编辑redis-server redis.windows.conf
2、。改动redis.windows.conf配置文件改动password:找到例如以下行:
找到# requirepass foobared 去掉前面的凝视#。并把foobared 替换为你自己的password:admin
或者命令:
(mac系统安装完Redis后,在终端中输入:
$src/redis-server
即可启动Redis服务。)
3、启动start.bat
链接命令:redis-cli -h 127.0.0.1 -p 6379 -a admin
4、
commons-pool-1.5.6.jar
commons-pool2-2.0.jar
jedis-2.7.2.jar
junit-4.9b2.jar
5、
package junit; 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 setCon(){
jedis = new Jedis("192.168.76.76", );
jedis.auth("admin");
} /**
*
*/
@Test
public void testString(){
/*jedis.set("password", "123");
System.out.println(jedis.get("password")); jedis.append("password", "456");
System.out.println(jedis.get("password")); jedis.del("password");
System.out.println(jedis.get("password"));*/ jedis.mset("name", "taop", "age", "", "qq", "");
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", "taop");
map.put("age", "");
map.put("qq", ""); jedis.hmset("userMap", map); List<String> userMap = jedis.hmget("userMap", "name", "age", "qq");
System.out.println(userMap.get()); System.out.println(jedis.hlen("userMap"));
System.out.println(jedis.exists("userMap"));
System.out.println(jedis.hkeys("userMap"));
System.out.println(jedis.hvals("userMap"));
jedis.hdel("userMap", "name"); System.out.println(jedis.hmget("userMap", "name")); Iterator<String> iterator = jedis.hkeys("userMap").iterator();
while(iterator.hasNext()){
String key = iterator.next();
System.out.println("value:"+jedis.hmget("userMap", key).get());
}
} @Test
public void testList(){
/*jedis.lpush("framework", "spring");
jedis.lpush("framework", "ibatis");
jedis.lpush("framework", "jedis");
jedis.lpush("framework", "struts");
jedis.lpush("framework", "hibernate");*/ System.out.println(jedis.lrange("framework", , -));
List<String> frameList = jedis.lrange("framework", , );
System.out.println(frameList);
} @Test
public void testRedisPool(){
JedisUtil.getJedis().set("username", "中文");
System.out.println(JedisUtil.getJedis().get("username"));
}
}
连接池封装
package junit; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class JedisUtil {
private static String ADDR = "192.168.76.76";
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; static {
try{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH);
}catch (Exception e) {
e.printStackTrace();
}
} public synchronized static Jedis getJedis(){
try{
if(jedisPool != null){
Jedis jedis = jedisPool.getResource();
return jedis;
}else{
return null;
}
}catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void returnResource(final Jedis jedis){
if(jedis != null){
jedisPool.returnResource(jedis);
}
}
}