這篇文章主要講解了“springboot整合redis的方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springboot整合redis的方法是什么”吧!
成都創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,先為樺川等服務(wù)建站,樺川等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為樺川企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
概述
springboot通常整合redis,采用的是RedisTemplate的形式,除了這種形式以外,還有另外一種形式去整合,即采用spring支持的注解進(jìn)行訪問緩存 。
》準(zhǔn)備工作
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ù)庫索引(默認(rèn)為0)
spring.redis.database=0
# Redis服務(wù)器地址
spring.redis.host=127.0.0.1
# Redis服務(wù)器連接端口
spring.redis.port=6379
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=8
# 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
spring.redis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=0
Redis配置類
/**
* @author hulonghai
* redis配置類
*/
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport{
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// 多個緩存的名稱,目前只定義了一個
rcm.setCacheNames(Arrays.asList("thisredis"));
//設(shè)置緩存過期時間(秒)
rcm.setDefaultExpiration(600);
return rcm;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
可以看出,我們這里主要配置了兩個東西,cacheManager方法配置了一個緩存名稱,它的名字叫做thisredis,當(dāng)我們要在方法注解里面使用到它的時候,就要根據(jù)名稱進(jìn)行區(qū)分不同緩存。同時設(shè)置了緩
存的過期時間。redisTemplate則是比較常見的,我們設(shè)置了RedisTemplate,因此在代碼里面,我們也可以通過@Autowired注入RedisTemplate來操作redis.
使用
接下來就是如何使用注解啦,這一步反而是最簡單的。其實只用到了兩個注解,@Cacheable和@CacheEvict。第一個注解代表從緩存中查詢指定的key,如果有,從緩存中取,不再執(zhí)行方法。如果沒有則執(zhí)
行方法,并且將方法的返回值和指定的key關(guān)聯(lián)起來,放入到緩存中。而@CacheEvict則是從緩存中清除指定的key對應(yīng)的數(shù)據(jù)。使用的代碼如下:
@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刪除");
}
可以看出,我們用@Cacheable的value屬性指定具體緩存,并通過key將其放入緩存中。這里key非常靈活,支持spring的el表達(dá)式,可以通過方法參數(shù)產(chǎn)生可變的key(見findUser方法),也可以通過其指定在
什么情況下,使用/不使用緩存(見delUser方法)。
感謝各位的閱讀,以上就是“springboot整合redis的方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對springboot整合redis的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
名稱欄目:springboot整合redis的方法是什么
網(wǎng)頁URL:http://aaarwkj.com/article44/gghcee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站營銷、移動網(wǎng)站建設(shè)、品牌網(wǎng)站制作、域名注冊、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)