liudehaos

还历史以真诚,还生命以过程。 ——余秋雨
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Redis相关练习操作,redis连接池

Posted on 2022-05-15 22:32  liudehaos  阅读(82)  评论(0编辑  收藏  举报

package com.shujia.controller;

import com.shujia.entity.MyTypedTuple;
import com.shujia.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.util.*;

/*
StringRedisTemplate:操作的是 string Value 也是string 的数据;
Redis 中的 value的类型是:string,list,set,zset,hash
RedisTemplate:key -- Object value --Object

            key    valule
        原本 string   string
        name ---->namexxx
        现在读的时候,会将键进行序列化,讲序列化后的值再取值redis 去找这个键,由于原本的name的值是字符串没有进行序列化,所以找不到,
        redisTemplate 默认是做JdkSerializationRedisSerializer
        如果查找的是一个字符串,将序列化方式变成自负床方式序列化;

*/
@RestController
public class TestController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
protected RedisTemplate<Object, Object> redisTemplate;

@RequestMapping("testKey")
public String testKey() {

// Boolean name = stringRedisTemplate.delete("name");
// System.out.println(name);
Set keys = stringRedisTemplate.keys("*");
System.out.println(keys);

    Boolean address = stringRedisTemplate.hasKey("address");
    System.out.println(address);
    DataType age = stringRedisTemplate.type("age");
    System.out.println(age);


    return "测试完毕" + keys;
}

@RequestMapping("testString")
public String testString() {
    stringRedisTemplate.opsForValue().set("name2", "xiaoxiao");
    stringRedisTemplate.opsForValue().set("name3", "xiaoma", Duration.ofSeconds(10l));
    String name = stringRedisTemplate.opsForValue().get("name");
    System.out.println(name);
    Integer name1 = stringRedisTemplate.opsForValue().append("name", "---chinese");
    System.out.println(name1);
    ArrayList<String> strings = new ArrayList<>();
    strings.add("name");
    strings.add("age");
    strings.add("address");
    strings.add("like");
    strings.add("sex");
    for (String string : strings) {
        System.out.println(string);
    }
    List<String> list = stringRedisTemplate.opsForValue().multiGet(strings);
    System.out.println(list);
    return "测试String完毕!!!" + name + "\t" + name1 + "\t" + "\t" + strings + "第二个" + "\t" + list;
}

@RequestMapping("testList")
public String testList() {
    stringRedisTemplate.opsForList().leftPush("list", "aaa");
    stringRedisTemplate.opsForList().leftPush("list", "bbb");
    List<String> list = stringRedisTemplate.opsForList().range("list", 0, -1);

    stringRedisTemplate.opsForList().trim("list", 0, 2);

    System.out.println(list);
    return "测试完毕testList!!!" + list;
}

@RequestMapping("testZset")
public String testZet() {
    //一次添加一个

    stringRedisTemplate.opsForZSet().add("zset1", "xiaoming", 100);

    //一次性添加多个元素


    HashSet<ZSetOperations.TypedTuple<String>> set = new HashSet<>();
    MyTypedTuple xiaoming = new MyTypedTuple("xiaoming", 80.0);
    MyTypedTuple xiaohu = new MyTypedTuple("xiaohu", 50.0);
    MyTypedTuple xiaoma = new MyTypedTuple("xiaoma", 10.0);
    set.add(xiaoming);
    set.add(xiaohu);
    set.add(xiaoma);
    stringRedisTemplate.opsForZSet().add("set", set);
    Set<String> zset = stringRedisTemplate.opsForZSet().rangeByScore("set", 10, 100);
    System.out.println("set" + zset);

    Set<ZSetOperations.TypedTuple<String>> set1 = stringRedisTemplate.opsForZSet().rangeByScoreWithScores("set", 10, 100);
    System.out.println("set1" + set1);

    Set<String> set2 = stringRedisTemplate.opsForZSet().reverseRange("set", 0, 100);
    System.out.println("set2" + set2);
    //查询并查看分数;
    Set<String> set3 = stringRedisTemplate.opsForZSet().reverseRangeByScore("set", 0, -1);
    System.out.println("set3" + set3);
    System.out.println("-----------------------");
    //根据分数降序查看,并且打印出来;
    Set<ZSetOperations.TypedTuple<String>> set4 = stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores("set", 0, 100);
    System.out.println("set4" + set4);
    for (ZSetOperations.TypedTuple<String> stringTypedTuple : set4) {
        String value = stringTypedTuple.getValue();
        Double score = stringTypedTuple.getScore();
        System.out.println(value + "--" + score);

    }


    return "测试完毕!!!";

}


@RequestMapping("testHash")
public String testHashash() {
    //一次添加一个数据
    stringRedisTemplate.opsForHash().put("1001", "name", "xiaoming");
    //一次添加多个数据
    HashMap<Object, Object> map = new HashMap<>();
    map.put("name ", "xiaoma");
    map.put("age ", "18");
    map.put("like ", "bannal");
    map.put("address ", "hefei");
    map.put("爱好 ", "看书写字");
    //将对应的取值对应的小key 组成一个Collection集合
    ArrayList<Object> arrayList = new ArrayList<>();
    arrayList.add("name");
    arrayList.add("age");
    arrayList.add("like");
    arrayList.add("address");
    arrayList.add("爱好");


    stringRedisTemplate.opsForHash().putAll("1002", map);
    List<Object> list = stringRedisTemplate.opsForHash().multiGet("1002", arrayList);
    System.out.println(list);


    return "测试完毕!!!";

}

@RequestMapping("testObject")
public String testObject() {
    //构建redis 资源连接池配置
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    //给定个数
    poolConfig.setMaxTotal(100);
    //根据配置来创建redis连接
    JedisPool jedisPool = new JedisPool(poolConfig, "192.168.170.110", 7000);
    //在连接池中构建连接redis
    Jedis jedis = jedisPool.getResource();

    //拿着这个连接操作redis
    String name1 = jedis.get("name");
    System.out.println(name1);

    //释放资源
    jedis.close();

    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.opsForValue().set("name", "xiaowang");

    Object name = redisTemplate.opsForValue().get("name");
    System.out.println(name);
    RedisSerializer<?> keySerializer = redisTemplate.getKeySerializer();
    System.out.println(keySerializer);
    //存储一个对象到redis
    User user = new User("1001", "xiangyu", 18);
    redisTemplate.opsForValue().set("user1", "user");
    //针对hash小key进行序列化
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.opsForHash().put("user2", "name", user);
    return "测试完毕!!!" + name;
}

}