1. 依賴包安裝
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2. Spring 項目集成進緩存支持
要啟用緩存支持,我們需要創(chuàng)建一個新的 CacheManager bean伤靠。CacheManager 接口有很多實現(xiàn),本文演示的是和 Redis 的集成,自然就是用 RedisCacheManager 了播聪。Redis 不是應用的共享內存椒振,它只是一個內存服務器昭伸,就像 MySql 似的,我們需要將應用連接到它并使用某種“語言”進行交互澎迎,因此我們還需要一個連接工廠以及一個 Spring 和 Redis 對話要用的 RedisTemplate庐杨,這些都是 Redis 緩存所必需的配置,把它們都放在自定義的 CachingConfigurerSupport 中:
package com.defonds.bdp.cache.redis;
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.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
// Defaults
redisConnectionFactory.setHostName("192.168.1.166");
redisConnectionFactory.setPort(6379);
return redisConnectionFactory;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// Number of seconds before expiration. Defaults to unlimited (0)
cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)
return cacheManager;
}
}
當然也別忘了把這些 bean 注入 Spring夹供,不然配置無效灵份。在 applicationContext.xml 中加入以下:
<context:component-scan base-package="com.defonds.bdp.cache.redis" />
3. 緩存某些方法的執(zhí)行結果
設置好緩存配置之后我們就可以使用 @Cacheable 注解來緩存方法執(zhí)行的結果了,比如根據(jù)省份名檢索城市的 provinceCities 方法和根據(jù) city_code 檢索城市的 searchCity 方法:
// R
@Cacheable("provinceCities")
public List<City> provinceCities(String province) {
logger.debug("province=" + province);
return this.cityMapper.provinceCities(province);
}
// R
@Cacheable("searchCity")
public City searchCity(String city_code){
logger.debug("city_code=" + city_code);
return this.cityMapper.searchCity(city_code);
}
4. 緩存數(shù)據(jù)一致性保證
CRUD (Create 創(chuàng)建哮洽,Retrieve 讀取填渠,Update 更新,Delete 刪除) 操作中袁铐,除了 R 具備冪等性揭蜒,其他三個發(fā)生的時候都可能會造成緩存結果和數(shù)據(jù)庫不一致。為了保證緩存數(shù)據(jù)的一致性剔桨,在進行 CUD 操作的時候我們需要對可能影響到的緩存進行更新或者清除屉更。
// C
@CacheEvict(value = { "provinceCities"}, allEntries = true)
public void insertCity(String city_code, String city_jb,
String province_code, String city_name,
String city, String province) {
City cityBean = new City();
cityBean.setCityCode(city_code);
cityBean.setCityJb(city_jb);
cityBean.setProvinceCode(province_code);
cityBean.setCityName(city_name);
cityBean.setCity(city);
cityBean.setProvince(province);
this.cityMapper.insertCity(cityBean);
}
// U
@CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true)
public int renameCity(String city_code, String city_name) {
City city = new City();
city.setCityCode(city_code);
city.setCityName(city_name);
this.cityMapper.renameCity(city);
return 1;
}
// D
@CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true)
public int deleteCity(String city_code) {
this.cityMapper.deleteCity(city_code);
return 1;
}
業(yè)務考慮,本示例用的都是 @CacheEvict 清除緩存洒缀。如果你的 CUD 能夠返回 City 實例瑰谜,也可以使用 @CachePut 更新緩存策略欺冀。筆者推薦能用 @CachePut 的地方就不要用 @CacheEvict,因為后者將所有相關方法的緩存都清理掉萨脑,比如上面三個方法中的任意一個被調用了的話隐轩,provinceCities 方法的所有緩存將被清除。
5. 自定義緩存數(shù)據(jù) key 生成策略
對于使用 @Cacheable 注解的方法渤早,每個緩存的 key 生成策略默認使用的是參數(shù)名+參數(shù)值职车,比如以下方法:
@Cacheable("users")
public User findByUsername(String username)
這個方法的緩存將保存于 key 為 users~keys 的緩存下,對于 username 取值為 "趙德芳" 的緩存鹊杖,key 為 "username-趙德芳"悴灵。一般情況下沒啥問題,二般情況如方法 key 取值相等然后參數(shù)名也一樣的時候就出問題了骂蓖,如:
@Cacheable("users")
public Integer getLoginCountByUsername(String username)
這個方法的緩存也將保存于 key 為 users~keys 的緩存下积瞒。對于 username 取值為 "趙德芳" 的緩存,key 也為 "username-趙德芳"登下,將另外一個方法的緩存覆蓋掉茫孔。
解決辦法是使用自定義緩存策略,對于同一業(yè)務(同一業(yè)務邏輯處理的方法被芳,哪怕是集群/分布式系統(tǒng))缰贝,生成的 key 始終一致,對于不同業(yè)務則不一致:
@Bean
public KeyGenerator customKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
于是上述兩個方法筐钟,對于 username 取值為 "趙德芳" 的緩存揩瞪,雖然都還是存放在 key 為 users~keys 的緩存下,但由于 key 分別為 "類名-findByUsername-username-趙德芳" 和 "類名-getLoginCountByUsername-username-趙德芳"篓冲,所以也不會有問題。
這對于集群系統(tǒng)宠哄、分布式系統(tǒng)之間共享緩存很重要壹将,真正實現(xiàn)了分布式緩存。
筆者建議:緩存方法的 @Cacheable 最好使用方法名毛嫉,避免不同的方法的 @Cacheable 值一致诽俯,然后再配以以上緩存策略。
6. 緩存的驗證
7. 注意事項
- 要緩存的 Java 對象必須實現(xiàn) Serializable 接口
- 緩存的生命周期我們可以配置承粤,然后托管 Spring CacheManager暴区,不要試圖通過 redis-cli 命令行去管理緩存。
- CacheManager 必須設置緩存過期時間辛臊,否則緩存對象將永不過期仙粱,這樣做的原因如上,避免一些野數(shù)據(jù)“永久保存”彻舰。此外伐割,設置緩存過期時間也有助于資源利用最大化候味,因為緩存里保留的永遠是熱點數(shù)據(jù)。
- 緩存適用于讀多寫少的場合隔心,查詢時緩存命中率很低白群、寫操作很頻繁等場景不適宜用緩存。