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

springboot整合redis进行数据操作(推荐)

程序员文章站 2024-04-01 20:31:52
redis是一种常见的nosql,日常开发中,我们使用它的频率比较高,因为它的多种数据接口,很多场景中我们都可以用到,并且redis对分布式这块做的非常好。 spring...

redis是一种常见的nosql,日常开发中,我们使用它的频率比较高,因为它的多种数据接口,很多场景中我们都可以用到,并且redis对分布式这块做的非常好。

springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作。

1、添加依赖

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

2、在application.properties中加入相关配置

spring.redis.database=0 
spring.redis.host=127.0.0.1 
spring.redis.port=6379 
spring.redis.password= 
spring.redis.pool.max-idle=8 
spring.redis.pool.min-idle=0 
spring.redis.pool.max-active=8 
spring.redis.pool.max-wait=-1 
spring.redis.timeout=5000 

3、编写配置类

import org.springframework.cache.cachemanager; 
import org.springframework.cache.annotation.enablecaching; 
import org.springframework.context.annotation.bean; 
import org.springframework.context.annotation.configuration; 
import org.springframework.data.redis.cache.rediscachemanager; 
import org.springframework.data.redis.connection.redisconnectionfactory; 
import org.springframework.data.redis.core.redistemplate; 
import org.springframework.data.redis.core.stringredistemplate; 
@configuration 
@enablecaching 
public class redisconfig { 
  @bean 
  public cachemanager cachemanager(redistemplate<?,?> redistemplate) { 
   cachemanager cachemanager = new rediscachemanager(redistemplate); 
   return cachemanager; 
  } 
  @bean 
  public redistemplate<string, object> redistemplate(redisconnectionfactory factory) { 
   redistemplate<string, object> redistemplate = new redistemplate<string, object>(); 
   redistemplate.setconnectionfactory(factory); 
   return redistemplate; 
  } 
  @bean 
  public stringredistemplate stringredistemplate(redisconnectionfactory factory) { 
   stringredistemplate stringredistemplate = new stringredistemplate(); 
   stringredistemplate.setconnectionfactory(factory); 
   return stringredistemplate; 
  } 
} 

这里定义了两个bean,一个是redistemplate,另一个是stringredistemplate,它们的序列化方式不同,前者默认jdk序列方式,后者默认string的序列化方式,后者一般专门用于存储string格式,前者我们可以用来保存对象等,这里我们都配置上,根据不同业务进行不同使用。

4、编写实体类

public class user implements serializable{ 
 /** 
  * 
  */ 
 private static final long serialversionuid = 3221700752972709820l; 
 private int id; 
 private string name; 
 private int age; 
 public int getid() { 
  return id; 
 } 
 public void setid(int id) { 
  this.id = id; 
 } 
 public string getname() { 
  return name; 
 } 
 public void setname(string name) { 
  this.name = name; 
 } 
 public int getage() { 
  return age; 
 } 
 public void setage(int age) { 
  this.age = age; 
 } 
 public user(int id, string name, int age) { 
  super(); 
  this.id = id; 
  this.name = name; 
  this.age = age; 
 } 
} 

5、编写测试service

@service 
public class userservice { 
 @autowired 
 private stringredistemplate stringredistemplate; 
 @autowired 
 private redistemplate<string, object> redistemplate; 
 public void set(string key, user user) { 
  redistemplate.opsforvalue().set(key, user); 
 } 
 public user get(string key) { 
  return (user) redistemplate.boundvalueops(key).get(); 
 } 
 public void setcode(string key, string code) { 
  stringredistemplate.opsforvalue().set(key, code, 60, timeunit.seconds); 
 } 
 public string getcode(string key) { 
  return stringredistemplate.boundvalueops(key).get(); 
 } 
} 

这里我们模拟两种操作,一种是根据key存储user对象,另一种是存储key value均为string的操作,并且赋予数据过期时间,这种操作我们可以用于验证码存储,在setcode方法中,我们存储了一个有效时长为60s的数据,当60s过后,数据会自动销毁。

6、编写测试controller访问

@restcontroller 
@requestmapping("rest_redis") 
public class rediscontroller { 
 @resource 
 private userservice userservice; 
 @getmapping("set") 
 public void set() { 
  userservice.set("key1", new user(1, "meepoguan", 26)); 
 } 
 @getmapping("get") 
 public string get() { 
  return userservice.get("key1").getname(); 
 } 
 @getmapping("stringset") 
 public void stringset() { 
  userservice.setcode("stringkey", "meepoguan_coke"); 
 } 
 @getmapping("stringget") 
 public string stringget() { 
  return userservice.getcode("stringkey"); 
 } 
} 

对service中的方法进行测试。

总结

以上所述是小编给大家介绍的springboot整合redis进行数据操作,希望对大家有所帮助