穀粒 | 專案整合redis
阿新 • • 發佈:2021-08-30
新增依賴
由於redis快取是公共應用,所以我們把依賴與配置新增到了common模組下面,在common模組pom.xml下新增以下依賴
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X整合redis所需common-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
配置
配置類
在service-base模組新增redis配置類,固定寫法,貼上即可
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; 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 org.springframework.data.redis.cache.RedisCacheConfiguration; 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.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; @EnableCaching //開啟快取 @Configuration //配置類 public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 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.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 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); // 配置序列化(解決亂碼的問題),過期時間600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }
使用redis模組配置
在使用到redis的模組的配置檔案application.properties
中新增redis配置
spring.redis.host=192.168.44.132 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待時間(負數表示沒限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0
linux中redis配置
-
關閉防火牆
#1:檢視防火狀態 systemctl status firewalld service iptables status #2:暫時關閉防火牆 systemctl stop firewalld service iptables stop #3:永久關閉防火牆 systemctl disable firewalld chkconfig iptables off #4:重啟防火牆 systemctl enable firewalld service iptables restart
-
redis配置檔案
1.註釋掉bind 127.0.0.1 2.修改protected-mode yes 為 protected-mode no
使用
由於首頁資料變化不是很頻繁,而且訪問量相對較大,所以我們有必要把首頁介面資料快取到redis快取中,減少資料庫壓力和提高訪問速度改造service-cms模組首頁banner介面,首頁課程與講師介面類似
springboot註解
快取@Cacheable
根據方法對其返回結果進行快取,下次再請求時,如果有快取,則直接讀取快取資料返回;如果快取不存在,則執行方法,並把返回的結果存入快取中。一般用在查詢方法上。
屬性值如下:
屬性/方法名 | 解釋 |
---|---|
value | 快取名,必填,它指定了你的快取存放在哪塊名稱空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標籤自定義快取的key |
快取@CachePut
使用該註解標誌的方法,每次都會執行,並將結果存入指定的快取中。其他方法可以直接從響應的快取中讀取快取資料,而不需要再去查詢資料庫。一般用在新增方法上。
屬性值如下:
屬性/方法名 | 解釋 |
---|---|
value | 快取名,必填,它指定了你的快取存放在哪塊名稱空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標籤自定義快取的key |
快取@CacheEvict
使用該註解標誌的方法,會清空指定的快取。一般用在更新或者刪除方法上
屬性/方法名 | 解釋 |
---|---|
value | 快取名,必填,它指定了你的快取存放在哪塊名稱空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標籤自定義快取的key |
allEntries | 是否清空所有快取,預設為 false。如果指定為 true,則方法呼叫後將立即清空所有的快取 |
beforeInvocation | 是否在方法執行前就清空,預設為 false。如果指定為 true,則在方法執行前就會清空快取 |
在service方法上使用註解
@Cacheable(value = "banner", key = "'selectIndexList'")
@Override
public List<CrmBanner> selectAllBanner() {
QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
wrapper.last("limit 2");
List<CrmBanner> list = baseMapper.selectList(wrapper);
return list;
}
keys *
檢視redis是否存入
可以看到值已存入redis