连接池工具
package jedisTest;
import redis.clients.jedis.*;
/**
* @author YDAlex
* @version 1.0
* @description Jedis连接池连接redis实例
* @data 2019/11/18.
*/
public class jedisPoolUtils {
private static JedisPool pool;
public static void main(String[] args) {
}
public static Jedis getJedisFromPool(){
if (pool == null){
synchronized (jedisPoolUtils.class){
if (pool == null){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(30);
config.setMaxTotal(100);
config.setTestOnBorrow(true);
pool = new JedisPool(config,"hadoop",16001);
}
}
}
return pool.getResource();
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author YDAelx
* @version 1.0
* @description 客户端操作redis
* @data 2019/11/18.
*/
public class JedisClientDemo {
private Jedis jedis = null;
public static void main(String[] args) {
}
@Before
public void setup(){
jedis = new Jedis("hadoop",16000,2000);//这里推荐用连接池
}
@Test
public void testCRUD(){
String Code = jedis.set("empName","独孤求败");
System.out.println("Code =" + Code);
String name = jedis.get("empName");
System.out.println("name = " + name);
System.out.println("___________________________________________");
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("name","阿卡丽");
map.put("age","18");
map.put("gender","女");
map.put("address","艾欧尼亚");
jedis.hmset("emp",map);
//takeout
Map<String,String> result = jedis.hgetAll("emp");
System.out.println("result ="+ result);
//list
jedis.lpush("诺克萨斯","斯维因","德莱厄斯","德莱文","瑞文");
//读取
List<String> nuxes = jedis.lrange("诺克萨斯",0,-1);
System.out.println(nuxes);
System.out.println("______________________________________________________");
//set
jedis.sadd("weapons","黑切","饮血","无尽");
Set<String> Weapons = jedis.smembers("weapons");
System.out.println(Weapons);
System.out.println("________________________________________________________");
//zset
Map<String,Double> map1 = new LinkedHashMap<>();
map1.put("盖伦",85D);
map1.put("赵信",69D);
map1.put("蛮王",69D);
jedis.zadd("gays",map1);
Set<String> gays = jedis.zrange("gays",0,-1);
System.out.println(gays);
}
@After
public void close() {
if (jedis != null){
jedis.close();
}
}
}