1. 程式人生 > >springboot整合redis進行資料操作

springboot整合redis進行資料操作

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