1 Memcache是什麼
Memcache是danga.com的一個項目,最早是爲 LiveJournal 服務的,目前全世界不少人使用這個緩存項目來構建自己大負載的網站,來分擔數據庫的壓力。
它可以應對任意多個連接,使用非阻塞的網絡IO。由於它的工作機制是在內存中開闢一塊空間,然後建立一個HashTable,Memcached自管理這些HashTable。
爲什麼會有Memcache和memcached兩種名稱?
其實Memcache是這個項目的名稱,而memcached是它服務器端的主程序文件名,
Memcache官方網站:http://www.danga.com/memcached,
2 Memcache工作原理
首先 memcached 是以守護程序方式運行於一個或多個服務器中,隨時接受客戶端的連接操作,客戶端可以由各種語言編寫,目前已知的客戶端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。客戶端在與 memcached 服務建立連接之後,接下來的事情就是存取對象了,每個被存取的對象都有一個唯一的標識符 key,存取操作均通過這個 key 進行,保存到 memcached 中的對象實際上是放置內存中的,並不是保存在 cache 文件中的,這也是爲什麼 memcached 能夠如此高效快速的原因。注意,這些對象並不是持久的,服務停止之後,裏邊的數據就會丟失。
與許多 cache 工具類似,Memcached 的原理並不複雜。它採用了C/S的模式,在 server 端啓動服務進程,在啓動時可以指定監聽的 ip,自己的端口號,所使用的內存大小等幾個關鍵參數。一旦啓動,服務就一直處於可用狀態。Memcached 的目前版本是通過C實現,採用了單進程,單線程,異步I/O,基於事件 (event_based) 的服務方式.使用 libevent 作爲事件通知實現。多個 Server 可以協同工作,但這些 Server 之間是沒有任何通訊聯繫的,每個 Server 只是對自己的數據進行管理。Client 端通過指定 Server 端的 ip 地址(通過域名應該也可以)。需要緩存的對象或數據是以 key->value 對的形式保存在Server端。key 的值通過 hash 進行轉換,根據 hash 值把 value 傳遞到對應的具體的某個 Server 上。當需要獲取對象數據時,也根據 key 進行。首先對 key 進行 hash,通過獲得的值可以確定它被保存在了哪臺 Server 上,然後再向該 Server 發出請求。Client 端只需要知道保存 hash(key) 的值在哪臺服務器上就可以了。
其實說到底,memcache 的工作就是在專門的機器的內存裏維護一張巨大的 hash 表,來存儲經常被讀寫的一些數組與文件,從而極大的提高網站的運行效率。
今天先研究研究緩存工具類的改造,在舊框架中部分函數用了ehcache對執行結果進行了緩存處理,現在目標是提供一個緩存工具類,在配置文件中配置使用哪種緩存(memcached或ehcached),使其它程序對具體的緩存不依賴,同時使用AOP方式來對方法執行結果進行緩存。
首先是工具類的實現:
在Spring中配置
- <!-- EhCache Manager -->
- <bean id="cacheManager"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property name="configLocation">
- <value>classpath:ehcache.xml</value>
- </property>
- </bean>
- <bean id="localCache"
- class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property name="cacheManager" ref="cacheManager" />
- <property name="cacheName"
- value="×××.cache.LOCAL_CACHE" />
- </bean>
- <bean id="cacheService"
- class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">
- <property name="cacheServerList" value="${cache.servers}"/>
- <property name="cacheServerWeights" value="${cache.cacheServerWeights}"/>
- <property name="cacheCluster" value="${cache.cluster}"/>
- <property name="localCache" ref="localCache"/>
- </bean>
在properties文件中配置${cache.servers} ${cache.cacheServerWeights} ${cache.cluster}
具體工具類的代碼
- /**
- * @author Marc
- *
- */
- public class CacheService {
- private Log logger = LogFactory.getLog(getClass());
- private Cache localCache;
- String cacheServerList;
- String cacheServerWeights;
- boolean cacheCluster = false;
- int initialConnections = 10;
- int minSpareConnections = 5;
- int maxSpareConnections = 50;
- long maxIdleTime = 1000 * 60 * 30; // 30 minutes
- long maxBusyTime = 1000 * 60 * 5; // 5 minutes
- long maintThreadSleep = 1000 * 5; // 5 seconds
- int socketTimeOut = 1000 * 3; // 3 seconds to block on reads
- int socketConnectTO = 1000 * 3; // 3 seconds to block on initial
- // connections. If 0, then will use blocking
- // connect (default)
- boolean failover = false; // turn off auto-failover in event of server
- // down
- boolean nagleAlg = false; // turn off Nagle's algorithm on all sockets in
- // pool
- MemCachedClient mc;
- public CacheService(){
- mc = new MemCachedClient();
- mc.setCompressEnable(false);
- }
- /**
- * 放入
- *
- */
- public void put(String key, Object obj) {
- Assert.hasText(key);
- Assert.notNull(obj);
- Assert.notNull(localCache);
- if (this.cacheCluster) {
- mc.set(key, obj);
- } else {
- Element element = new Element(key, (Serializable) obj);
- localCache.put(element);
- }
- }
- /**
- * 刪除
- */
- public void remove(String key){
- Assert.hasText(key);
- Assert.notNull(localCache);
- if (this.cacheCluster) {
- mc.delete(key);
- }else{
- localCache.remove(key);
- }
- }
- /**
- * 得到
- */
- public Object get(String key) {
- Assert.hasText(key);
- Assert.notNull(localCache);
- Object rt = null;
- if (this.cacheCluster) {
- rt = mc.get(key);
- } else {
- Element element = null;
- try {
- element = localCache.get(key);
- } catch (CacheException cacheException) {
- throw new DataRetrievalFailureException("Cache failure: "
- + cacheException.getMessage());
- }
- if(element != null)
- rt = element.getValue();
- }
- return rt;
- }
- /**
- * 判斷是否存在
- *
- */
- public boolean exist(String key){
- Assert.hasText(key);
- Assert.notNull(localCache);
- if (this.cacheCluster) {
- return mc.keyExists(key);
- }else{
- return this.localCache.isKeyInCache(key);
- }
- }
- private void init() {
- if (this.cacheCluster) {
- String[] serverlist = cacheServerList.split(",");
- Integer[] weights = this.split(cacheServerWeights);
- // initialize the pool for memcache servers
- SockIOPool pool = SockIOPool.getInstance();
- pool.setServers(serverlist);
- pool.setWeights(weights);
- pool.setInitConn(initialConnections);
- pool.setMinConn(minSpareConnections);
- pool.setMaxConn(maxSpareConnections);
- pool.setMaxIdle(maxIdleTime);
- pool.setMaxBusyTime(maxBusyTime);
- pool.setMaintSleep(maintThreadSleep);
- pool.setSocketTO(socketTimeOut);
- pool.setSocketConnectTO(socketConnectTO);
- pool.setNagle(nagleAlg);
- pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
- pool.initialize();
- logger.info("初始化memcached pool!");
- }
- }
- private void destory() {
- if (this.cacheCluster) {
- SockIOPool.getInstance().shutDown();
- }
- }
- }
然後實現函數的AOP攔截類,用來在函數執行前返回緩存內容
- public class CachingInterceptor implements MethodInterceptor {
- private CacheService cacheService;
- private String cacheKey;
- public void setCacheKey(String cacheKey) {
- this.cacheKey = cacheKey;
- }
- public void setCacheService(CacheService cacheService) {
- this.cacheService = cacheService;
- }
- public Object invoke(MethodInvocation invocation) throws Throwable {
- Object result = cacheService.get(cacheKey);
- //如果函數返回結果不在Cache中,執行函數並將結果放入Cache
- if (result == null) {
- result = invocation.proceed();
- cacheService.put(cacheKey,result);
- }
- return result;
- }
- }
Spring的AOP配置如下:
- <aop:config proxy-target-class="true">
- <aop:advisor
- pointcut="execution(* ×××.PoiService.getOne(..))"
- advice-ref="PoiServiceCachingAdvice" />
- </aop:config>
- <bean id="BasPoiServiceCachingAdvice"
- class="×××.core.cache.CachingInterceptor">
- <property name="cacheKey" value="PoiService" />
- <property name="cacheService" ref="cacheService" />
- </bean>