javascript
Spring添加对缓存的支持
緩存可以可以存儲經常使用到的信息,如果這些信息保存在數據庫中,經常對數據庫的讀取會嚴重影響應用的性能,所以將這些信息保存在緩存中,取出來就可以立即使用。
1、啟用spring對緩存的支持
Spring對緩存的支持有兩種方式:
1)注解驅動緩存
2)XML申明的緩存
使用Spring最通用的方法就是在方法上添加@Cacheable和@CacheEvict注解。本人更喜歡使用XML與注解混合使用的方式開發。
2、使用XML文件配置緩存
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 啟用緩存注解開關 --><cache:annotation-driven cache-manager="cacheManager"/><!-- 啟用注解 --><context:component-scan base-package="com.zjp" /><context:annotation-config/><bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"/></bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehcache"/></bean></beans>在spring配置文件中聲明了使用ehcache緩存管理器以及對應的ehcache.xml文件定義的緩存。
對應的ehcache.xml文件
其中定義了兩個緩存,分別為userCache和users。
3、為對應的方法添加注解支持緩存
為對應的方法添加上注解就可以實現對緩存的控制,常用的注解如下:
| @Cacheable | 添加緩存之前檢查緩存中是否存在,存在就不調用,否則調用 |
| @CachePut | 不管緩存是否存在都調用方法 |
| @CacheEvict | 刪除一條或者多條緩存 |
| @Caching | 分組的注解,可以同時應用多個緩存注解 |
1)添加緩存
@Cacheable和@CachePut注解都可以填充緩存,但是它們的工作方差異。
@Cacheable首先在緩存中查找條目,如果找到了匹配的條目,那么就不會對方法進行
了。如果沒有找到匹配的條目,方法會被調用并且返回值要放到緩存之中。而@Cache
不會在緩存中檢查匹配的值,目標方法總是會被調用,并將返回值添加到緩存之中。
a、自定義緩存key
@Cacheable和@CachePut都有一個名為key屬性,這個屬性能夠替換默認的key,它是通過
一個SpEL表達式計算得到的。任意的SpEL表達式都是可行的,但是更常見的場景是所定義的
表達式與存儲在緩存中的值有關,據此計算得到key。
b、條件化緩存
@Cacheable和@CachePut提供了兩個屬性用以實現條件化緩存:unless和condition,
這兩個屬性都接受一個SpEL表達式。如果unless屬性的SpEL表達式計算結果為true,那么
緩存方法返回的數據就不會放到緩存中。與之類似,如果condition屬性的SpEL表達式計算
結果為false,那么對于這個方法緩存就會被禁用掉。
2)緩存的移除
使用@CacheEvict 就可以移除緩存中對應的數據。
對于緩存操作的UserService如下:
同時添加上其他的
public interface UserDao {/*** 刪除所有用戶*/public void removeAllUser();/*** 根據Id查詢用戶* @param id* @return*/public User findUserById(long id);/*** 更新用戶* @param user*/public void updateUser(User user);/*** 根據Id刪除用戶* @param id*/public void removeUserById(long id);} package com.zjp.service.impl;import org.springframework.stereotype.Service;import com.zjp.domain.User; import com.zjp.service.UserDao;@Service public class UserDaoImpl implements UserDao{public void removeAllUser() {// TODO Auto-generated method stub}public User findUserById(long id) {User user = new User();user.setId(123424);user.setPassword("ahfakjbnfjk");user.setUsername("lisi");return user;}public void updateUser(User user) {// TODO Auto-generated method stub}public void removeUserById(long id) {// TODO Auto-generated method stub}}總結
以上是生活随笔為你收集整理的Spring添加对缓存的支持的全部內容,希望文章能夠幫你解決所遇到的問題。