java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot RedisTemplate入门

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

作者:程序员小哲

这篇文章主要介绍了spring boot整合redis实现RedisTemplate三分钟快速入门,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

引入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

RedisTemplate五种数据结构的操作

RedisTemplate方法讲解

判断key是否存在

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

获取指定的key的失效时间

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

根据key获取过期时间

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

根据key删除reids中缓存数据

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

保存和读取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;
  }

保存和读取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 "成功";
  }

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 "成功";
  }

保存和读取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;
  }

保存和读取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 "成功";
  }

完整示例代码

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 "成功";
  }
}

序列化

直接粘贴在项目中即可

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();
  } 
}

到此这篇关于spring boot整合redis实现RedisTemplate三分钟快速入门的文章就介绍到这了,更多相关springboot RedisTemplate入门内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文