成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java環境下Memcached應用詳解

開發 后端
這里將介紹Java環境下Memcached應用,Memcached主要是集群環境下的緩存解決方案,希望本文對大家有所幫助。

本文將對在Java環境下Memcached應用進行詳細介紹。Memcached主要是集群環境下的緩存解決方案,可以運行在Java或者.NET平臺上,這里我們主要講的是Windows下的Memcached應用。

這些天在設計SNA的架構,接觸了一些遠程緩存、集群、session復制等的東西,以前做企業應用的時候感覺作用不大,現在設計面對internet的系統架構時就非常有用了,而且在調試后看到壓力測試的情況還是比較好的。

在緩存的選擇上有過很多的思考,雖然說memcached結合java在序列化上性能不怎么樣,不過也沒有更好的集群環境下的緩存解決方案了,就選擇了memcached。本來計劃等公司買的服務器到位裝個linux再來研究memcached,但這兩天在找到了一個windows下的Memcached版本,就動手開始調整現有的框架了。

Windows下的Server端很簡單,不用安裝,雙擊運行后默認服務端口是11211,沒有試著去更改端口,因為反正以后會用Unix版本,到時再記錄安裝步驟。下載客戶端的JavaAPI包,接口非常簡單,參考API手冊上就有現成的例子。

目標,對舊框架緩存部分進行改造:

1、緩存工具類

2、hibernate的provider

3、用緩存實現session機制

今天先研究研究緩存工具類的改造,在舊框架中部分函數用了ehcache對執行結果進行了緩存處理,現在目標是提供一個緩存工具類,在配置文件中配置使用哪種緩存(memcached或ehcached),使其它程序對具體的緩存不依賴,同時使用AOP方式來對方法執行結果進行緩存。

首先是工具類的實現:

在Spring中配置
Java代碼

  1.      
  2. <bean id="cacheManager"    
  3. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">     
  4. <property name="configLocation">     
  5. <value>classpath:ehcache.xmlvalue>     
  6. property>     
  7. bean>     
  8.  
  9. <bean id="localCache"    
  10. class="org.springframework.cache.ehcache.EhCacheFactoryBean">     
  11. <property name="cacheManager" ref="cacheManager" />     
  12. <property name="cacheName"    
  13. value="×××.cache.LOCAL_CACHE" />     
  14. bean>     
  15.  
  16. <bean id="cacheService"    
  17. class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">     
  18. <property name="cacheServerList" value="${cache.servers}"/>     
  19. <property name="cacheServerWeights" value="${cache.cacheServerWeights}"/>     
  20. <property name="cacheCluster" value="${cache.cluster}"/>     
  21. <property name="localCache" ref="localCache"/>     
  22. bean>    
  23.  
  24. <bean id="cacheManager" 
  25. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
  26. <property name="configLocation"> 
  27. <value>classpath:ehcache.xmlvalue> 
  28. property> 
  29. bean> 
  30. <bean id="localCache" 
  31. class="org.springframework.cache.ehcache.EhCacheFactoryBean"> 
  32. <property name="cacheManager" ref="cacheManager" /> 
  33. <property name="cacheName" 
  34. value="×××.cache.LOCAL_CACHE" /> 
  35. bean> 
  36.  
  37. <bean id="cacheService" 
  38. class="×××.core.cache.CacheService" init-method="init" destroy-method="destory"> 
  39. <property name="cacheServerList" value="${cache.servers}"/> 
  40. <property name="cacheServerWeights" value="${cache.cacheServerWeights}"/> 
  41. <property name="cacheCluster" value="${cache.cluster}"/> 
  42. <property name="localCache" ref="localCache"/> 
  43. bean> 

在properties文件中配置${cache.servers} ${cache.cacheServerWeights} ${cache.cluster}

具體工具類的代碼

Java代碼

  1. /**   
  2. * @author Marc   
  3. *    
  4. */    
  5. public class CacheService {     
  6. private Log logger = LogFactory.getLog(getClass());     
  7. private Cache localCache;     
  8. String cacheServerList;     
  9. String cacheServerWeights;     
  10. boolean cacheCluster = false;     
  11. int initialConnections = 10;     
  12. int minSpareConnections = 5;     
  13. int maxSpareConnections = 50;     
  14. long maxIdleTime = 1000 * 60 * 30// 30 minutes    
  15. long maxBusyTime = 1000 * 60 * 5// 5 minutes    
  16. long maintThreadSleep = 1000 * 5// 5 seconds    
  17. int socketTimeOut = 1000 * 3// 3 seconds to block on reads    
  18. int socketConnectTO = 1000 * 3// 3 seconds to block on initial    
  19. // connections. If 0, then will use blocking    
  20. // connect (default)    
  21. boolean failover = false// turn off auto-failover in event of server    
  22. // down    
  23. boolean nagleAlg = false// turn off Nagle's algorithm on all sockets in    
  24. // pool    
  25. MemCachedClient mc;     
  26. public CacheService(){     
  27. mc = new MemCachedClient();     
  28. mc.setCompressEnable(false);     
  29. }     
  30. /**   
  31. * 放入   
  32. *    
  33. */    
  34. public void put(String key, Object obj) {     
  35. Assert.hasText(key);     
  36. Assert.notNull(obj);     
  37. Assert.notNull(localCache);     
  38. if (this.cacheCluster) {     
  39. mc.set(key, obj);     
  40. else {     
  41. Element element = new Element(key, (Serializable) obj);     
  42. localCache.put(element);     
  43. }     
  44. }     
  45. /**   
  46. * 刪除    
  47. */    
  48. public void remove(String key){     
  49. Assert.hasText(key);     
  50. Assert.notNull(localCache);     
  51. if (this.cacheCluster) {     
  52. mc.delete(key);     
  53. }else{     
  54. localCache.remove(key);     
  55. }     
  56. }     
  57. /**   
  58. * 得到   
  59. */    
  60. public Object get(String key) {     
  61. Assert.hasText(key);     
  62. Assert.notNull(localCache);     
  63. Object rt = null;     
  64. if (this.cacheCluster) {     
  65. rt = mc.get(key);     
  66. else {     
  67. Element element = null;     
  68. try {     
  69. element = localCache.get(key);     
  70. catch (CacheException cacheException) {     
  71. throw new DataRetrievalFailureException("Cache failure: "    
  72. + cacheException.getMessage());     
  73. }     
  74. if(element != null)     
  75. rt = element.getValue();     
  76. }     
  77. return rt;     
  78. }     
  79. /**   
  80. * 判斷是否存在   
  81. *    
  82. */    
  83. public boolean exist(String key){     
  84. Assert.hasText(key);     
  85. Assert.notNull(localCache);     
  86. if (this.cacheCluster) {     
  87. return mc.keyExists(key);     
  88. }else{     
  89. return this.localCache.isKeyInCache(key);     
  90. }     
  91. }     
  92. private void init() {     
  93. if (this.cacheCluster) {     
  94. String[] serverlist = cacheServerList.split(",");     
  95. Integer[] weights = this.split(cacheServerWeights);     
  96. // initialize the pool for memcache servers    
  97. SockIOPool pool = SockIOPool.getInstance();     
  98. pool.setServers(serverlist);     
  99. pool.setWeights(weights);     
  100. pool.setInitConn(initialConnections);     
  101. pool.setMinConn(minSpareConnections);     
  102. pool.setMaxConn(maxSpareConnections);     
  103. pool.setMaxIdle(maxIdleTime);     
  104. pool.setMaxBusyTime(maxBusyTime);     
  105. pool.setMaintSleep(maintThreadSleep);     
  106. pool.setSocketTO(socketTimeOut);     
  107. pool.setSocketConnectTO(socketConnectTO);     
  108. pool.setNagle(nagleAlg);     
  109. pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);     
  110. pool.initialize();     
  111. logger.info("初始化memcached pool!");     
  112. }     
  113. }     
  114.  
  115. private void destory() {     
  116. if (this.cacheCluster) {     
  117. SockIOPool.getInstance().shutDown();     
  118. }     
  119. }     
  120. }    
  121. /**  
  122. * @author Marc  
  123.  
  124. */ 
  125. public class CacheService {  
  126. private Log logger = LogFactory.getLog(getClass());  
  127. private Cache localCache;  
  128. String cacheServerList;  
  129. String cacheServerWeights;  
  130. boolean cacheCluster = false;  
  131. int initialConnections = 10;  
  132. int minSpareConnections = 5;  
  133. int maxSpareConnections = 50;  
  134. long maxIdleTime = 1000 * 60 * 30// 30 minutes  
  135. long maxBusyTime = 1000 * 60 * 5// 5 minutes  
  136. long maintThreadSleep = 1000 * 5// 5 seconds  
  137. int socketTimeOut = 1000 * 3// 3 seconds to block on reads  
  138. int socketConnectTO = 1000 * 3// 3 seconds to block on initial  
  139. // connections. If 0, then will use blocking  
  140. // connect (default)  
  141. boolean failover = false// turn off auto-failover in event of server  
  142. // down  
  143. boolean nagleAlg = false// turn off Nagle's algorithm on all sockets in  
  144. // pool  
  145. MemCachedClient mc;  
  146. public CacheService(){  
  147. mc = new MemCachedClient();  
  148. mc.setCompressEnable(false);  
  149. }  
  150. /**  
  151. * 放入  
  152.  
  153. */ 
  154. public void put(String key, Object obj) {  
  155. Assert.hasText(key);  
  156. Assert.notNull(obj);  
  157. Assert.notNull(localCache);  
  158. if (this.cacheCluster) {  
  159. mc.set(key, obj);  
  160. else {  
  161. Element element = new Element(key, (Serializable) obj);  
  162. localCache.put(element);  
  163. }  
  164. }  
  165. /**  
  166. * 刪除   
  167. */ 
  168. public void remove(String key){  
  169. Assert.hasText(key);  
  170. Assert.notNull(localCache);  
  171. if (this.cacheCluster) {  
  172. mc.delete(key);  
  173. }else{  
  174. localCache.remove(key);  
  175. }  
  176. }  
  177. /**  
  178. * 得到  
  179. */ 
  180. public Object get(String key) {  
  181. Assert.hasText(key);  
  182. Assert.notNull(localCache);  
  183. Object rt = null;  
  184. if (this.cacheCluster) {  
  185. rt = mc.get(key);  
  186. else {  
  187. Element element = null;  
  188. try {  
  189. element = localCache.get(key);  
  190. catch (CacheException cacheException) {  
  191. throw new DataRetrievalFailureException("Cache failure: " 
  192. + cacheException.getMessage());  
  193. }  
  194. if(element != null)  
  195. rt = element.getValue();  
  196. }  
  197. return rt;  
  198. }  
  199. /**  
  200. * 判斷是否存在  
  201.  
  202. */ 
  203. public boolean exist(String key){  
  204. Assert.hasText(key);  
  205. Assert.notNull(localCache);  
  206. if (this.cacheCluster) {  
  207. return mc.keyExists(key);  
  208. }else{  
  209. return this.localCache.isKeyInCache(key);  
  210. }  
  211. }  
  212. private void init() {  
  213. if (this.cacheCluster) {  
  214. String[] serverlist = cacheServerList.split(",");  
  215. Integer[] weights = this.split(cacheServerWeights);  
  216. // initialize the pool for memcache servers  
  217. SockIOPool pool = SockIOPool.getInstance();  
  218. pool.setServers(serverlist);  
  219. pool.setWeights(weights);  
  220. pool.setInitConn(initialConnections);  
  221. pool.setMinConn(minSpareConnections);  
  222. pool.setMaxConn(maxSpareConnections);  
  223. pool.setMaxIdle(maxIdleTime);  
  224. pool.setMaxBusyTime(maxBusyTime);  
  225. pool.setMaintSleep(maintThreadSleep);  
  226. pool.setSocketTO(socketTimeOut);  
  227. pool.setSocketConnectTO(socketConnectTO);  
  228. pool.setNagle(nagleAlg);  
  229. pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);  
  230. pool.initialize();  
  231. logger.info("初始化memcachedpool!");  
  232. }  
  233. }  
  234. private void destory() {  
  235. if (this.cacheCluster) {  
  236. SockIOPool.getInstance().shutDown();  
  237. }  
  238. }  
  239. }  

然后實現函數的AOP攔截類,用來在函數執行前返回緩存內容

Java代碼

  1. public class CachingInterceptor implements MethodInterceptor {     
  2.  
  3. private CacheService cacheService;     
  4. private String cacheKey;     
  5.  
  6. public void setCacheKey(String cacheKey) {     
  7. this.cacheKey = cacheKey;     
  8. }     
  9.  
  10. public void setCacheService(CacheService cacheService) {     
  11. this.cacheService = cacheService;     
  12. }     
  13.  
  14. public Object invoke(MethodInvocation invocation) throws Throwable {     
  15. Object result = cacheService.get(cacheKey);     
  16. //如果函數返回結果不在Cache中,執行函數并將結果放入Cache    
  17. if (result == null) {     
  18. result = invocation.proceed();     
  19. cacheService.put(cacheKey,result);     
  20. }     
  21. return result;     
  22. }     
  23. }    
  24. public class CachingInterceptor implements MethodInterceptor {  
  25.  
  26. private CacheService cacheService;  
  27. private String cacheKey;  
  28.  
  29. public void setCacheKey(String cacheKey) {  
  30. this.cacheKey = cacheKey;  
  31. }  
  32.  
  33. public void setCacheService(CacheService cacheService) {  
  34. this.cacheService = cacheService;  
  35. }  
  36.  
  37. public Object invoke(MethodInvocation invocation) throws Throwable {  
  38. Object result = cacheService.get(cacheKey);  
  39. //如果函數返回結果不在Cache中,執行函數并將結果放入Cache  
  40. if (result == null) {  
  41. result = invocation.proceed();  
  42. cacheService.put(cacheKey,result);  
  43. }  
  44. return result;  
  45. }  

Spring的AOP配置如下:

Java代碼

  1. <aop:config proxy-target-class="true">     
  2. <aop:advisor     
  3. pointcut="execution(* ×××.PoiService.getOne(..))"    
  4. advice-ref="PoiServiceCachingAdvice" />     
  5. aop:config>     
  6.  
  7. <bean id="BasPoiServiceCachingAdvice"    
  8. class="×××.core.cache.CachingInterceptor">     
  9. <property name="cacheKey" value="PoiService" />     
  10. <property name="cacheService" ref="cacheService" />     
  11. bean>  

【編輯推薦】

  1. .NET分布式緩存之Memcached執行速度檢測
  2. 從memcached看MySQL和關系數據庫的未來
  3. 分布式緩存系統memcached簡介與實踐
  4. Google App Engine的Java持久性與數據存儲
  5. Java正則表達式實現條件查詢淺析

【責任編輯:彭凡 TEL:(010)68476606】

責任編輯:彭凡 來源: ITPUB
相關推薦

2011-09-06 14:59:20

UbuntuMemcached

2019-08-06 19:36:25

RedisMemcached緩存

2021-06-03 08:04:13

LinuxMySQL配置

2010-08-11 10:24:46

Flex開發

2011-04-01 16:56:57

NetBeansBlackBerry BlackBerry

2018-06-28 13:38:59

云計算云服務云安全

2009-03-09 09:45:07

MVCAjax.Net

2010-09-14 09:24:29

C語言

2011-06-29 10:18:20

LINUX QT ARM

2014-03-19 09:19:44

KDE應用GNOME

2011-10-31 15:59:56

SQLiteiPhoneiOS

2013-06-26 15:58:33

CentOS 5.6Memcached

2009-11-20 09:10:21

C#開發環境

2017-06-07 09:48:21

Oracle RAC應用連續性

2009-06-04 20:38:15

MyEclipseWeblogicWeb應用管理

2015-07-20 10:06:12

2009-06-29 15:09:00

Java環境搭建Ubuntu

2010-03-17 15:58:08

Python環境

2010-09-16 15:33:48

Java環境變量

2019-12-09 11:10:24

LinuxDjangoPython
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 美国一级毛片a | 国产精品欧美一区二区 | 久久最新精品 | 亚洲欧美日韩一区二区 | 午夜影院网站 | 国产精品久久久久久久 | 欧美一区 | 亚洲超碰在线观看 | 免费在线看黄 | 欧美成人精品一区二区男人看 | 午夜激情视频 | 成人黄页在线观看 | 色.com| 中文字幕av网 | 久久高清 | 成人精品一区 | 999热精品视频 | 91久久精品国产91久久 | 久久综合香蕉 | 国产精品久久7777777 | www.亚洲| 波多野结衣先锋影音 | 6080yy精品一区二区三区 | 久久久久国产精品一区二区 | www.中文字幕av| 亚洲性视频网站 | 最新午夜综合福利视频 | 涩涩视频在线观看免费 | 亚洲精品456 | 日韩一区精品 | 999久久久久久久久 国产欧美在线观看 | 国产精品成人在线播放 | 精品av天堂毛片久久久借种 | 久久成人18免费网站 | 波多野结衣一区二区三区在线观看 | 国产一区二区电影 | 欧美伊人影院 | 中文字幕1区2区 | 三级av网址 | 日韩视频中文字幕 | 久久午夜视频 |