一 Redis_Jedis_测试
1 Jedis所需要的jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
2 连接Redis注意事项
禁用Linux的防火墙:Linux(CentOS7)里执行命令
systemctl stop/disable firewalld.service
redis.conf中注释掉
bind 127.0.0.1
然后将安全模式关闭
protected-mode no
或者不关闭安全模式,设置密码在配置文件中的requirepass中修改
然后执行命令前需要先输入密码
AUTH 密码 之后再执行命令
新建工具类 user_profile_manager_0224\src\main\java\com\atguigu\userprofile\utils\RedisUtil.java
3 测试相关数据类型
(0)测试连接
public class RedisUtil {
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
}
}
(1)Key
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.set("k1000","v1000");
jedis.set("k2000","v2000");
jedis.set("k3000","v3000");
Set<String> keys = jedis.keys("*");
System.out.println(keys.size());
for(String key:keys){
System.out.println(key);
}
System.out.println(jedis.exists("k3000"));
System.out.println(jedis.ttl("k2000"));
System.out.println(jedis.get("k1000"));
}
(2)String
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.mset("str1", "v1", "str2", "v2", "str3", "v3");
System.out.println(jedis.mget("str1", "str2", "str3"));
}
(3)List
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.lpush("mylist","v1","v2","v3");
List<String> list = jedis.lrange("mylist", 0, -1);
for(String element : list){
System.out.println(element);
}
}
(4)set
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.sadd("sets1","set01","set02","set03","set04");
jedis.sadd("sets2","set02","set03","set04","set05");
Set<String> smembers = jedis.smembers("sets1");
for (String set : smembers) {
System.out.println(set);
}
System.out.println("===================");
jedis.srem("sets1","set02");
System.out.println(jedis.scard("sets1"));
Set<String> sinter = jedis.sinter("sets1", "sets2");
for (String s : sinter) {
System.out.println(s);
}
System.out.println("===================");
Set<String> sunion = jedis.sunion("sets1", "sets2");
for (String s : sunion) {
System.out.println(s);
}
System.out.println("===================");
Set<String> sdiff = jedis.sdiff("sets1", "sets2");
for (String s : sdiff) {
System.out.println(s);
}
}
(5)hash
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.hset("hash1","userName","zhangsan");
System.out.println(jedis.hget("hash1", "userName"));
HashMap<String, String> map = new HashMap<>();
map.put("userName","lisi");
map.put("age","20");
map.put("gender","nv");
jedis.hmset("hash2",map);
List<String> res = jedis.hmget("hash2", "userName", "age", "gender");
for (String re : res) {
System.out.println(re);
}
}
(6)zset
public static void main(String[] args) {
Jedis jedis = new Jedis("hadoop101", 6379);
System.out.println(jedis.ping()); // 输出PONG
jedis.zadd("zset01", 100d, "z3");
jedis.zadd("zset01", 90d, "l4");
jedis.zadd("zset01", 80d, "w5");
jedis.zadd("zset01", 70d, "z6");
Set<Tuple> zrange = jedis.zrangeWithScores("zset01", 0, -1);
for (Tuple tuple : zrange) {
System.out.println(tuple);
}
}
(7)连接池
为了节省每次连接redis服务带来的消耗,把连接好的实例反复利用。
通过参数管理连接的行为
代码如下
public class RedisUtil