引入jar包
redis.clients
jedis
3.6.0
创建资源目录,创建jedis.properties文件,配置jedis参数;
resources/jedis.properties
host=192.168.xx.xxx
port=6379
max=50
maxidle=10
创建连接池类
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties prop = new Properties();
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(prop.getProperty("maxidle")));//最大空闲数
config.setMaxTotal(Integer.parseInt(prop.getProperty("max")));//最大连接数
//创建连接池对象
/* jedisPool = new JedisPool(config, prop.getProperty("host"),
Integer.parseInt(prop.getProperty("port")),
prop.getProperty("user"),
prop.getProperty("pwd"));*/
//创建连接池对象
jedisPool = new JedisPool(config, prop.getProperty("host"),
Integer.parseInt(prop.getProperty("port")));
}
public static Jedis getJedis(){
return jedisPool.getResource();
}
public static void main(String[] args) {
//测试连接池
Jedis jedis = JedisPoolUtils.getJedis();
System.out.println(jedis);
jedis.close();
Set keys = jedis.keys("stu*");
for (String key : keys) {
System.out.println(key);
}
}
}
Redis数据类型:String Hash List Set SortedSet
下面是通过JedisPool对redis的五种类型数据进行 *** 作
public class JedisDemo {
public static void main(String[] args) {
Jedis jedis = JedisPoolUtils.getJedis();
//类型:String Hash List Set SortedSet
JedisDemo jedisDemo = new JedisDemo();
// jedisDemo.StringOperation(jedis);
// jedisDemo.HashOperation(jedis);
// jedisDemo.SetOperation(jedis);
// jedisDemo.ListOperation(jedis);
jedisDemo.SortedSet(jedis);
}
public void HashOperation(Jedis jedis){
// jedis.flushDB();//清空数据库
//hset
// jedis.hset("msg", "name", "kb16");
//hmset
/* HashMap hinfo = new HashMap<>();
hinfo.put("name","zhangsan");
hinfo.put("age","18");
hinfo.put("address","andmen");
hinfo.put("tel","15812365632");
jedis.hmset("info1",hinfo);*/
//hdel:
// jedis.hdel("info1","address","tel");
// jedis.hkeys("msg");
/*List msg = jedis.hvals("msg");
System.out.println(msg);*/
/*Boolean hexists = jedis.hexists("msg", "age");
System.out.println(hexists);*/
//hgetAll
Map result = jedis.hgetAll("msg");
System.out.println(result);
}
public void ListOperation(Jedis jedis){
// jedis.flushDB();
/*Long lpushNum = jedis.lpush("newlist", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
System.out.println(lpushNum);*/
/*List newlist = jedis.lrange("newlist", 0, -1);
System.out.println(newlist);*/
/*Long rpushNum = jedis.rpush("newlist", "aa", "bb", "cc", "dd");
System.out.println(rpushNum);*/
/*String newlist1 = jedis.lpop("newlist");
System.out.println(newlist1);*/
/*Long llenNum = jedis.llen("newlist");
System.out.println(llenNum);*/
/* String element = jedis.lindex("newlist", 2);
System.out.println(element);*/
/*String lset = jedis.lset("newlist", 3, "welcome");
System.out.println(lset);*/
/*List newlist = jedis.lrange("newlist", 0, -1);
System.out.println(newlist);*/
}
public void StringOperation(Jedis jedis){
System.out.println("------------------------String--------------------");
// jedis.select(0);
//清空数据库
// jedis.flushDB();
//插值
// jedis.set("kb11","good");
// jedis.set("kb15","nice");
// jedis.set("kb16","very nice");
/*//取值
String value = jedis.get("kb16");
System.out.println(value);*/
//修改
/*System.out.println(jedis.get("kb16"));
jedis.set("kb16","very very nice");
System.out.println(jedis.get("kb16"));*/
//修改 value值,在原值基础上追加内容
/* System.out.println(jedis.get("kb16"));
jedis.append("kb16","tomorrow");
System.out.println(jedis.get("kb16"));*/
//删除
/* Long num = jedis.del("kb11");
System.out.println(num);*/
//批量新增
// jedis.mset("kt107","s1j","kd40","xxq","kd58","sdd");
//批量查询
/* List results = jedis.mget("kt107", "kb16");
for (String result : results) {
System.out.println(result);
}*/
//批量删除
// jedis.del("kt107","kd58");
//设置当key存在时,value值不会修改;当key不存在,则创建
// Long setnx = jedis.setnx("kb11", "xxq");
// System.out.println(setnx);
//设置key,并指定生存周期
// jedis.setex("name", 50L, "gree");
//ttl 剩余生存时间
// Long time = jedis.ttl("name");
// System.out.println(time);
//getSet :设置新的value,返回原来的value
/*String oldValue = jedis.getSet("kb16", "Very very nice");
System.out.println(oldValue);*/
/*String rangeValue = jedis.getrange("kb16", 0, -1);
System.out.println(rangeValue);*/
}
public void SetOperation(Jedis jedis){
// jedis.flushDB();
//设置set类型
/* jedis.sadd("kb11","zs");
jedis.sadd("kb11","ls");
jedis.sadd("kb11","ww");
jedis.sadd("kb16","ls","ww","zl");*/
//smembers:查询set中所有的值
/* Set kb11 = jedis.smembers("kb11");
for (String value : kb11) {
System.out.println(value);
}*/
//srem:删除指定字段
/* Long num = jedis.srem("kb11", "ww");
System.out.println(num);*/
//spop
/*String value = jedis.spop("kb11");
System.out.println(value);*/
// Set results = jedis.sdiff("kb11", "kb16");
// Set results = jedis.sinter("kb16", "kb11");
/*Set results = jedis.sunion("kb16", "kb11");
for (String result : results ) {
System.out.println(result);
}*/
/*Long num = jedis.scard("kb16");
System.out.println(num);*/
}
public void SortedSet(Jedis jedis){
/*jedis.zadd("myzset",1,"one");
jedis.zadd("myzset",2,"two");
jedis.zadd("myzset",3,"three");*/
Map< String,Double> content = new HashMap();
content.put("four",4.0);
content.put("five",5.0);
content.put("six",6.0);
jedis.zadd("myzset",content);
//删除集合中的元素
// jedis.zrem("myzset","one");
//查看元素数量
Long num = jedis.zcard("myzset");
System.out.println(num);
//查看集合
Set myzset = jedis.zrange("myzset", 0, -1);
for (String s : myzset) {
System.out.println(s);
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)