spring boot整合redis ---- RedisTemplate 三分钟快速入门

huatechinfo
发布于 2020-12-30 13:40
浏览
1收藏

 

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.

 

RedisTemplate五种数据结构的操作

redisTemplate.opsForValue(); //操作字符串
redisTemplate.opsForHash(); //操作hash
redisTemplate.opsForList(); //操作list
redisTemplate.opsForSet(); //操作set
redisTemplate.opsForZSet(); //操作有序zset
RedisTemplate方法讲解
判断key是否存在
 /**
     * 判断key是否存在
     */
    @GetMapping("haskey")
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.


获取指定的key的失效时间

 /**
     * 指定key的失效时间
     */
    @GetMapping("expire")
    public void expire(String key, long time) {
        //参数一:key
        //参数二:睡眠时间
        //参数三:睡眠时间单位 TimeUnit.DAYS 天 TimeUnit.HOURS 小时 。。。
        redisTemplate.expire(key, time, TimeUnit.MINUTES);
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.


根据key获取过期时间

/**
     * 根据key获取过期时间
     */
    @GetMapping("getexpire")
    public long getExpire(String key) {
        Long expire = redisTemplate.getExpire(key);
        return expire;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.


根据key删除reids中缓存数据

/**
     * 根据key删除reids中缓存数据
     */
    @GetMapping("delredisbykey")
    public void delete(String key) {
        redisTemplate.delete(key);
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.


保存和读取String

/**
     * 保存和读取String
     */
    @GetMapping("stringredisdemo")
    public String stringredisdemo() {
        //设置过期时间为1分钟
        redisTemplate.opsForValue().set("key1", "value1", 1, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("key2", "value2");
        redisTemplate.opsForValue().set("key3", "value3");
        //读取redis数据
        String result1 = redisTemplate.opsForValue().get("key1").toString();
        String result2 = redisTemplate.opsForValue().get("key2").toString();
        String result3 = redisTemplate.opsForValue().get("key3").toString();
        System.out.println("缓存结果为:result:" + result1 + "  " + result2 + "   " + result3);
        return "缓存结果为:result:" + result1 + "  " + result2 + "   " + result3;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.


保存和读取list

/**
     * 保存和读取list
     */
    @GetMapping("listredisdemo")
    public String listredisdemo() {
        List<String> list1 = new ArrayList<>();
        list1.add("a1");
        list1.add("a2");
        list1.add("a3");
        List<String> list2 = new ArrayList<>();
        list2.add("b1");
        list2.add("b2");
        list2.add("b3");
        redisTemplate.opsForList().leftPush("listkey1", list1);
        redisTemplate.opsForList().rightPush("listkey2", list2);
        List<String> resultList1 = (List<String>) redisTemplate.opsForList().leftPop("listkey1");
        List<String> resultList2 = (List<String>) redisTemplate.opsForList().rightPop("listkey2");
        System.out.println("resultList1:" + resultList1);
        System.out.println("resultList2:" + resultList2);
        return "成功";
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.


Hash结构,保存和读取map

 /**
     * Hash结构,保存和读取map
     */
    @GetMapping("mapredisdemo")
    public String mapredisdemo() {
        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", "value4");
        map.put("key5", "value5");
        redisTemplate.opsForHash().putAll("map1", map);
        Map<String, String> resultMap = redisTemplate.opsForHash().entries("map1");
        List<String> reslutMapList = redisTemplate.opsForHash().values("map1");
        Set<String> resultMapSet = redisTemplate.opsForHash().keys("map1");
        String value = (String) redisTemplate.opsForHash().get("map1", "key1");
        System.out.println("value:" + value);
        System.out.println("resultMapSet:" + resultMapSet);
        System.out.println("resultMap:" + resultMap);
        System.out.println("resulreslutMapListtMap:" + reslutMapList);
        return "成功";
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.


保存和读取Set

/**
     * 保存和读取Set
     */
    @GetMapping("setredisdemo")
    public String getredisdemo() {
        SetOperations<String, String> set = redisTemplate.opsForSet();
        set.add("key1", "value1");
        set.add("key1", "value2");
        set.add("key1", "value3");
        Set<String> resultSet = redisTemplate.opsForSet().members("key1");
        System.out.println("resultSet:" + resultSet);
        return "resultSet:" + resultSet;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.


保存和读取zset

/**
     * 保存和读取zset
     */
    @GetMapping("zsetredisdemo")
    public String zsetredisdemo() {
        ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zset-5", 9.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("zset-6", 9.9);
        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(redisTemplate.opsForZSet().add("zset1", tuples));
        System.out.println(redisTemplate.opsForZSet().range("zset1", 0, -1));
        return "成功";
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.


完整示例代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
public class ReidsDemo {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 指定key的失效时间
     */
    @GetMapping("expire")
    public void expire(String key, long time) {
        //参数一:key
        //参数二:睡眠时间
        //参数三:睡眠时间单位 TimeUnit.DAYS 天 TimeUnit.HOURS 小时 。。。
        redisTemplate.expire(key, time, TimeUnit.MINUTES);
    }

    /**
     * 根据key获取过期时间
     */
    @GetMapping("getexpire")
    public long getExpire(String key) {
        Long expire = redisTemplate.getExpire(key);
        return expire;
    }

    /**
     * 判断key是否存在
     */
    @GetMapping("haskey")
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 根据key删除reids中缓存数据
     */
    @GetMapping("delredisbykey")
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 保存和读取String
     */
    @GetMapping("stringredisdemo")
    public String stringredisdemo() {
        //设置过期时间为1分钟
        redisTemplate.opsForValue().set("key1", "value1", 1, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("key2", "value2");
        redisTemplate.opsForValue().set("key3", "value3");
        //读取redis数据
        String result1 = redisTemplate.opsForValue().get("key1").toString();
        String result2 = redisTemplate.opsForValue().get("key2").toString();
        String result3 = redisTemplate.opsForValue().get("key3").toString();
        System.out.println("缓存结果为:result:" + result1 + "  " + result2 + "   " + result3);
        return "缓存结果为:result:" + result1 + "  " + result2 + "   " + result3;
    }

    /**
     * 保存和读取list
     */
    @GetMapping("listredisdemo")
    public String listredisdemo() {
        List<String> list1 = new ArrayList<>();
        list1.add("a1");
        list1.add("a2");
        list1.add("a3");
        List<String> list2 = new ArrayList<>();
        list2.add("b1");
        list2.add("b2");
        list2.add("b3");
        redisTemplate.opsForList().leftPush("listkey1", list1);
        redisTemplate.opsForList().rightPush("listkey2", list2);
        List<String> resultList1 = (List<String>) redisTemplate.opsForList().leftPop("listkey1");
        List<String> resultList2 = (List<String>) redisTemplate.opsForList().rightPop("listkey2");
        System.out.println("resultList1:" + resultList1);
        System.out.println("resultList2:" + resultList2);
        return "成功";
    }

    /**
     * Hash结构,保存和读取map
     */
    @GetMapping("mapredisdemo")
    public String mapredisdemo() {
        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        redisTemplate.opsForHash().putAll("map1", map);
        Map<String, String> resultMap = redisTemplate.opsForHash().entries("map1");
        List<String> reslutMapList = redisTemplate.opsForHash().values("map1");
        Set<String> resultMapSet = redisTemplate.opsForHash().keys("map1");
        String value = (String) redisTemplate.opsForHash().get("map1", "key1");
        System.out.println("value:" + value);
        System.out.println("resultMapSet:" + resultMapSet);
        System.out.println("resultMap:" + resultMap);
        System.out.println("resulreslutMapListtMap:" + reslutMapList);
        return "成功";
    }

    /**
     * 保存和读取Set
     */
    @GetMapping("setredisdemo")
    public String getredisdemo() {
        SetOperations<String, String> set = redisTemplate.opsForSet();
        set.add("key1", "value1");
        set.add("key1", "value2");
        set.add("key1", "value3");
        Set<String> resultSet = redisTemplate.opsForSet().members("key1");
        System.out.println("resultSet:" + resultSet);
        return "resultSet:" + resultSet;
    }

    /**
     * 保存和读取zset
     */
    @GetMapping("zsetredisdemo")
    public String zsetredisdemo() {
        ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zset-5", 9.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("zset-6", 9.9);
        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(redisTemplate.opsForZSet().add("zset1", tuples));
        System.out.println(redisTemplate.opsForZSet().range("zset1", 0, -1));
        return "成功";
    }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.


序列化
直接粘贴在项目中即可

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/*
*序列化
 */
@Configuration
public class MyRedisConfig {

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //参照StringRedisTemplate内部实现指定序列化器
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(keySerializer());
        redisTemplate.setHashKeySerializer(keySerializer());
        redisTemplate.setValueSerializer(valueSerializer());
        redisTemplate.setHashValueSerializer(valueSerializer());
        return redisTemplate;
    }

    private RedisSerializer<String> keySerializer(){
        return new StringRedisSerializer();
    }

    //使用Jackson序列化器
    private RedisSerializer<Object> valueSerializer(){
        return new GenericJackson2JsonRedisSerializer();
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.


    

1
收藏 1
回复
举报
1
1


回复
    相关推荐