springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)
redis是一種常見的nosql,日常開發(fā)中,我們使用它的頻率比較高,因為它的多種數(shù)據(jù)接口,很多場景中我們都可以用到,并且redis對分布式這塊做的非常好。
springboot整合redis比較簡單,并且使用redistemplate可以讓我們更加方便的對數(shù)據(jù)進(jìn)行操作。
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,它們的序列化方式不同,前者默認(rèn)jdk序列方式,后者默認(rèn)string的序列化方式,后者一般專門用于存儲string格式,前者我們可以用來保存對象等,這里我們都配置上,根據(jù)不同業(yè)務(wù)進(jìn)行不同使用。
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ù),當(dāng)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中的方法進(jìn)行測試。
總結(jié)
以上所述是小編給大家介紹的springboot整合redis進(jìn)行數(shù)據(jù)操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot使用JavaMailSender實現(xiàn)發(fā)送郵件+Excel附件
項目審批完畢后,需要發(fā)送郵件通知相關(guān)人員,并且要附帶數(shù)據(jù)庫表生成的Excel表格,這就要求不光是郵件發(fā)送功能,還要臨時生成Excel表格做為附件,本文詳細(xì)介紹了SpringBoot如何使用JavaMailSender實現(xiàn)發(fā)送郵件+Excel附件,需要的朋友可以參考下2023-10-10SpringBoot中打印SQL語句的幾種方法實現(xiàn)
本文主要介紹了SpringBoot中打印SQL語句的幾種方法實現(xiàn),,通過打印SQL語句可以幫助開發(fā)人員快速了解數(shù)據(jù)庫的操作情況,進(jìn)而進(jìn)行性能分析和調(diào)試,感興趣的可以了解一下2023-11-11springboot?使用websocket技術(shù)主動給前端發(fā)送消息的實現(xiàn)
這篇文章主要介紹了springboot?使用websocket技術(shù)主動給前端發(fā)送消息的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java中的HashSet集合存儲數(shù)據(jù)的結(jié)構(gòu)詳解
這篇文章主要介紹了Java中的HashSet集合存儲數(shù)據(jù)的結(jié)構(gòu)詳解,數(shù)組結(jié)構(gòu)他把元素進(jìn)行分組,相同哈希值的元素是一組,鏈表/紅黑樹結(jié)構(gòu)把相同哈希值的元素鏈接到一起,存儲數(shù)據(jù)到集合中,先計算元素的哈希值,需要的朋友可以參考下2023-09-09SpringBoot整合新版SpringSecurity完整過程
Spring Security是保障Spring應(yīng)用程序安全的強(qiáng)大框架,而新版的Spring Security引入了lambda表達(dá)式來配置,使得安全配置更加簡潔、優(yōu)雅,本文將介紹如何在Spring Boot項目中整合新版Spring Security,需要的朋友可以參考下2024-02-02