/**
* 通过网络,访问redis网络
* 修改redis.conf
*/
//String host int port
String host ="192.168.96.139";
int port = 6379;
Jedis jedis = new Jedis(host,port);
jedis.set("break","rice");//String
System.out.println(jedis.get("break"));
jedis.mset("lunch","mmm","dinner","nuddles");
System.out.println(jedis.mget("lunch", "dinner"));
jedis.hset("wah","age","18");
HashMap<String,String> map = new HashMap<String, String>();
map.put("age","18");
map.put("sex","girl");
map.put("name","wah");
jedis.hmset("people",map);//hset 集合
System.out.println("---------------------------------");
System.out.println(jedis.hmget("people", "age"));
System.out.println("--------------------------------------");
System.out.println(jedis.hgetAll("people"));
jedis.sadd("city","a","b","c");//set 不能重复
jedis.zadd("score",77.3,"math");
Map<String,Double> map1 = new HashMap<String, Double>();
map1.put("linux",75.0);
map1.put("数据结构",80.1);
jedis.zadd("score",map1);//zset按照分数排序
jedis.lpush("friends","gqq","lyr","djr","gqn");//"gqn""gqn"lyr"gqq",
jedis.rpush("friends2","gqq","lyr","djr","gqn");//"gqq","lyr",""gqn","gqn"
}
}
---------------------------------------------------------------------------------------------------------------------------------
public class RedisUtil {
private static JedisPool jedisPool;
public static JedisPool open(String ip,int port){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);//设置最大线程数,一个线程就是一个jedis
config.setMaxIdle(2);//设置最大空闲数
config.setTestOnBorrow(true);
jedisPool = new JedisPool(ip,port);
return jedisPool;
}
public static void close(){
if (jedisPool!=null){
jedisPool.close();
}
}
}
public class RedisPoll {
public static void main(String[] args) {
String host ="192.168.96.139";
int port = 6379;
JedisPool pool = null;
Jedis jedis = null;
pool = RedisUtil.open(host,port);
jedis = pool.getResource();
System.out.println(jedis.zrange("score", 0, -1));
System.out.println(jedis.smembers("city"));
System.out.println(jedis.lrange("friends", 0, -1));
pool.close();
}
}