1、使用OGNL的命名規則來定義Key的值
@Cacheable(cacheNames = {"user"},key = "#root.methodName + '[' + #id + ']'") @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); }
2、自定義Key生成器
@Configuration public class MyConfig { @Bean public KeyGenerator myKeyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... params) { String key = o.getClass().getSimpleName()+"."+method.getName()+Arrays.asList(params); return key; } }; } }
key 屬性和keyGenerator屬性只能二選一
@Cacheable(cacheNames = {"user"},keyGenerator = "myKeyGenerator") @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); }
3、加條件過濾
/**condition成立的條件緩存,unless成立的條件不緩存。以下實例含義為:id > 1 且 id != 2 的緩存
*
* 1、condition:id >1的會被緩存,只執行一次查詢SQL,id <= 1的每次查詢都會執行SQL,#a0是取第一個參數,也可以用“#參數名” 即:#id
* 2、unless: id==2的不緩存
* 3、sync: 異步方式緩存
* sync 和 unless 不能同時支持,否則會報錯
*/ @Cacheable(cacheNames = {"user"},keyGenerator = "myKeyGenerator",condition = "#a0>1", unless="#id==2", sync=true) @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); }
4、緩存更新 @CachePut
/** * @CachePut:既調用方法,又更新緩存數據;同步更新緩存 * 修改了數據庫的某個數據,同時更新緩存; * 運行時機: * 1、先調用目標方法 * 2、將目標方法的結果緩存起來 * * 測試步驟: * 1、查詢1號員工;查到的結果會放在緩存中; * key:1 value:name:張三 * 2、以后查詢還是之前的結果 * 3、更新1號員工;【name:zhangsan;age:10】 * 將方法的返回值也放進緩存了; * key:傳入的employee對象 值:返回的employee對象; * 4、查詢1號員工? * 應該是更新后的員工; * key = "#record.id":使用傳入的參數的員工id; * key = "#result.id":使用返回后的id * @Cacheable的key是不能用#result * 為什么是沒更新前的?【1號員工沒有在緩存中更新】 * */ @CachePut(cacheNames = {"user"},key = "#record.id") @Override public User updateByPrimaryKeySelective(User record) { userMapper.updateByPrimaryKeySelective(record); return userMapper.selectByPrimaryKey(record.getId()); }
4、緩存清除@CacheEvict
/** * @CacheEvict:緩存清除 * key:指定要清除的數據 * allEntries = true:指定清除這個緩存中所有的數據 * beforeInvocation = false:緩存的清除是否在方法之前執行 * 默認代表緩存清除操作是在方法執行之后執行;如果出現異常緩存就不會清除 * * beforeInvocation = true: * 代表清除緩存操作是在方法運行之前執行,無論方法是否出現異常,緩存都清除 * * */ @CacheEvict(value="emp",beforeInvocation = true/*key = "#id",*/) @Override public int deleteByPrimaryKey(Integer id) { System.out.println("delete user : " + id); return id; //return userMapper.deleteByPrimaryKey(id); }
5、復雜的
/** * 復雜的緩存規則: * 1.以name查詢還會去查詢數據庫:因為有@CachePut注解,所以這方法一定要執行的,@CachePut把方法執行的結果緩存到緩存 * 2. (1), (1) : 每次都會執行SQL,因為有@CachePut * (1),(2): (1)執行SQL,不執行 * * * */ @Caching( cacheable = { @Cacheable(cacheNames = {"user"},key="#name") //(1)根據name查詢user }, put = { @CachePut(cacheNames = {"user"},key="#result.id") //(2) 根據id查詢user 以另一種key將查詢出的結果緩存到緩存中 } ) @Override public User selectByName(String name) { return userMapper.selectByName(name); }
6. 全局參數提取 @CacheConfig
@Service @CacheConfig(cacheNames={"user"},keyGenerator = "myKeyGenerator") public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @CacheEvict(/*value="emp",*/ beforeInvocation = true/*key = "#id",*/) @Override public int deleteByPrimaryKey(Integer id) { System.out.println("delete user : " + id); return id; //return userMapper.deleteByPrimaryKey(id); } }