springboot整合redis進行數(shù)據(jù)操作(推薦)
redis是一種常見的nosql,日常開發(fā)中,我們使用它的頻率比較高,因為它的多種數(shù)據(jù)接口,很多場景中我們都可以用到,并且redis對分布式這塊做的非常好。
springboot整合redis比較簡單,并且使用redistemplate可以讓我們更加方便的對數(shù)據(jù)進行操作。
1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> lt;/dependency>
2、在application.properties中加入相關(guān)配置
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格式,前者我們可以用來保存對象等,這里我們都配置上,根據(jù)不同業(yè)務(wù)進行不同使用。
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();
}
}
這里我們模擬兩種操作,一種是根據(jù)key存儲user對象,另一種是存儲key value均為string的操作,并且賦予數(shù)據(jù)過期時間,這種操作我們可以用于驗證碼存儲,在setcode方法中,我們存儲了一個有效時長為60s的數(shù)據(jù),當60s過后,數(shù)據(jù)會自動銷毀。
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中的方法進行測試。
總結(jié)
以上所述是小編給大家介紹的springboot整合redis進行數(shù)據(jù)操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限項目實踐
本文主要介紹了mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Javamelody監(jiān)控不到sql的問題(親測有效)??
JavaMelody是用來在QA和實際運行生產(chǎn)環(huán)境中監(jiān)控Java或Java?EE應(yīng)用程序服務(wù)器的一個開源框架,這篇文章主要介紹了Javamelody監(jiān)控不到sql(親測有效)??,需要的朋友可以參考下2022-10-10
關(guān)于springboot-starter-undertow和tomcat的區(qū)別說明
這篇文章主要介紹了關(guān)于springboot-starter-undertow和tomcat的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis-Plus 條件構(gòu)造器 QueryWrapper 的基本用法
這篇文章主要介紹了Mybatis-Plus - 條件構(gòu)造器 QueryWrapper 的使用,通過實例代碼給大家介紹了查詢示例代碼及實現(xiàn)需求,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法
當數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時間不使用的情況下,該連接會自動回收并失效,就導致客戶端程序報“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧2024-03-03
Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實例講解
這里我們演示了Servelet驅(qū)動Tomcat來進行HTTP下載的方法,接下來就詳細來看Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實例講解2016-05-05
springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程
這篇文章主要介紹了springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

