欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-05 09:26:03
引入依赖 org.springframework.boot s...

引入依赖

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

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

获取指定的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入门内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!