偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Java環(huán)境下Memcached應(yīng)用詳解

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

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

這些天在設(shè)計SNA的架構(gòu),接觸了一些遠(yuǎn)程緩存、集群、session復(fù)制等的東西,以前做企業(yè)應(yīng)用的時候感覺作用不大,現(xiàn)在設(shè)計面對internet的系統(tǒng)架構(gòu)時就非常有用了,而且在調(diào)試后看到壓力測試的情況還是比較好的。

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

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

目標(biāo),對舊框架緩存部分進(jìn)行改造:

1、緩存工具類

2、hibernate的provider

3、用緩存實現(xiàn)session機(jī)制

今天先研究研究緩存工具類的改造,在舊框架中部分函數(shù)用了ehcache對執(zhí)行結(jié)果進(jìn)行了緩存處理,現(xiàn)在目標(biāo)是提供一個緩存工具類,在配置文件中配置使用哪種緩存(memcached或ehcached),使其它程序?qū)唧w的緩存不依賴,同時使用AOP方式來對方法執(zhí)行結(jié)果進(jìn)行緩存。

首先是工具類的實現(xiàn):

在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. }  

然后實現(xiàn)函數(shù)的AOP攔截類,用來在函數(shù)執(zhí)行前返回緩存內(nèi)容

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. //如果函數(shù)返回結(jié)果不在Cache中,執(zhí)行函數(shù)并將結(jié)果放入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. //如果函數(shù)返回結(jié)果不在Cache中,執(zhí)行函數(shù)并將結(jié)果放入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執(zhí)行速度檢測
  2. 從memcached看MySQL和關(guān)系數(shù)據(jù)庫的未來
  3. 分布式緩存系統(tǒng)memcached簡介與實踐
  4. Google App Engine的Java持久性與數(shù)據(jù)存儲
  5. Java正則表達(dá)式實現(xiàn)條件查詢淺析

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

責(zé)任編輯:彭凡 來源: ITPUB
相關(guān)推薦

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開發(fā)

2011-04-01 16:56:57

NetBeansBlackBerry BlackBerry

2018-06-28 13:38:59

云計算云服務(wù)云安全

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應(yīng)用GNOME

2011-10-31 15:59:56

SQLiteiPhoneiOS

2013-06-26 15:58:33

CentOS 5.6Memcached

2009-11-20 09:10:21

C#開發(fā)環(huán)境

2017-06-07 09:48:21

Oracle RAC應(yīng)用連續(xù)性

2009-06-04 20:38:15

MyEclipseWeblogicWeb應(yīng)用管理

2015-07-20 10:06:12

2009-06-29 15:09:00

Java環(huán)境搭建Ubuntu

2010-03-17 15:58:08

Python環(huán)境

2010-09-16 15:33:48

Java環(huán)境變量

2019-12-09 11:10:24

LinuxDjangoPython
點贊
收藏

51CTO技術(shù)棧公眾號