springboot使用redis實現(xiàn)從配置到實戰(zhàn)
概述
springboot通常整合redis,采用的是RedisTemplate的形式,除了這種形式以外,還有另外一種形式去整合,即采用spring支持的注解進行訪問緩存.
準備工作
pom.xml
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>RELEASE</version> </dependency>
application.properties
# REDIS (RedisProperties) # Redis數(shù)據(jù)庫索引(默認為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=127.0.0.1 # Redis服務(wù)器連接端口 spring.redis.port=6379 # 連接池最大連接數(shù)(使用負值表示沒有限制) spring.redis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時時間(毫秒) spring.redis.timeout=0
Redis配置類
package cn.chenlove.config; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{ @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.max-wait}") private long maxWaitMillis; @Bean public JedisPool redisPoolFactory() { Logger.getLogger(getClass()).info("JedisPool注入成功??!"); Logger.getLogger(getClass()).info("redis地址:" + host + ":" + port); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); return jedisPool; } }
可以看出,我們這里主要配置了兩個東西,cacheManager方法配置了一個緩存名稱,它的名字叫做thisredis,當我們要在方法注解里面使用到它的時候,就要根據(jù)名稱進行區(qū)分不同緩存.同時設(shè)置了緩
存的過期時間.redisTemplate則是比較常見的,我們設(shè)置了RedisTemplate,因此在代碼里面,我們也可以通過@Autowired注入RedisTemplate來操作redis.
使用
接下來就是如何使用注解啦,這一步反而是最簡單的.其實只用到了兩個注解,@Cacheable和@CacheEvict.第一個注解代表從緩存中查詢指定的key,如果有,從緩存中取,不再執(zhí)行方法.如果沒有則執(zhí)
行方法,并且將方法的返回值和指定的key關(guān)聯(lián)起來,放入到緩存中.而@CacheEvict則是從緩存中清除指定的key對應(yīng)的數(shù)據(jù).使用的代碼如下:
//有參數(shù) @Cacheable(value="thisredis", key="'users_'+#id") public User findUser(Integer id) { User user = new User(); user.setUsername("hlhdidi"); user.setPassword("123"); user.setUid(id.longValue()); System.out.println("log4j2壞啦?"); logger.info("輸入user,用戶名:{},密碼:{}",user.getUsername(),user.getPassword()); return user; } @CacheEvict(value="thisredis", key="'users_'+#id",condition="#id!=1") public void delUser(Integer id) { // 刪除user System.out.println("user刪除"); } //無參數(shù) @RequestMapping("/get") @Cacheable(value="thisredis") @ResponseBody public List<User> xx(){ return userMapper.selectAll(); } @RequestMapping("/get3") @CacheEvict(value="thisredis") @ResponseBody public String xx3(){ return "ok"; }
可以看出,我們用@Cacheable的value屬性指定具體緩存,并通過key將其放入緩存中.這里key非常靈活,支持spring的el表達式,可以通過方法參數(shù)產(chǎn)生可變的key(見findUser方法),也可以通過其指定在
什么情況下,使用/不使用緩存(見delUser方法).
Gitee碼云:https://gitee.com/lyc96/projects
到此這篇關(guān)于springboot使用redis實現(xiàn)從配置到實戰(zhàn)的文章就介紹到這了,更多相關(guān)springboot使用redis 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot如何使用@Cacheable進行緩存與取值
這篇文章主要介紹了SpringBoot如何使用@Cacheable進行緩存與取值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08spring boot整合redis實現(xiàn)RedisTemplate三分鐘快速入門
這篇文章主要介紹了spring boot整合redis實現(xiàn)RedisTemplate三分鐘快速入門,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12java集合——Java中的equals和hashCode方法詳解
本篇文章詳細介紹了Java中的equals和hashCode方法詳解,Object 類是所有類的父類,非常具有實用價值,需要的朋友可以參考下。2016-10-10深入理解Netty?FastThreadLocal優(yōu)缺點及實現(xiàn)邏輯
本文以線上詭異問題為切入點,通過對比JDK ThreadLocal和Netty FastThreadLocal實現(xiàn)邏輯以及優(yōu)缺點,并深入解讀源碼,由淺入深理解Netty FastThreadLocal2023-10-10Spring?session?redis?修改默認的序列化方法(案例)
這篇文章主要介紹了Spring?session?redis?修改默認的序列化方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Java數(shù)據(jù)結(jié)構(gòu)及算法實例:漢諾塔問題 Hanoi
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:漢諾塔問題 Hanoi,本文直接給出實現(xiàn)代碼,代碼中包含大量注釋,需要的朋友可以參考下2015-06-06