标签:
package com.castalia.market.blh.impl; import java.io.Serializable; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import com.castalia.market.blh.IgetCampaignPrizeFromCacheBlh; import com.castalia.market.vo.CampaignPrizeRange; @Component public class getCampaignPrizeFromCacheBlhImpl implements IgetCampaignPrizeFromCacheBlh { private static final Logger logger = Logger.getLogger(getCampaignPrizeFromCacheBlhImpl.class); private Cache cache; public getCampaignPrizeFromCacheBlhImpl() { URL url = CampaignPrizeCacheBlhImpl.class.getClassLoader().getResource("/config/ehcache.xml"); CacheManager manager = new CacheManager(url); logger.info(manager.getName()); cache = manager.getCache("sampleCache2"); } @Override public synchronized String getCampaignPrizeFromCache(String key, int num) { // TODO Auto-generated method stub StringBuilder Hashcodes=new StringBuilder(); logger.info("取缓存的键:"+key); try{ Element result = cache.get(key); if(result==null) { logger.info("取缓存的键值: CacheNull"); return "CacheNull"; } @SuppressWarnings("deprecation") List<CampaignPrizeRange> CampaignPrizeRanges=(List<CampaignPrizeRange>)result.getValue(); String ServerIndex=key.substring(0,2); int index=0; List<CampaignPrizeRange> RemoveCampaignPrizeRanges=new ArrayList<CampaignPrizeRange>(); tag: for(CampaignPrizeRange e :CampaignPrizeRanges) { for (long i=e.getStartValue();i<=e.getEndValue();i++) { if(index>=num) { e.setStartValue(i); break tag; } else { Hashcodes.append(ServerIndex+String.format("%06d", i)+","); index++; } } RemoveCampaignPrizeRanges.add(e); } if(index!=num) { logger.info("取缓存的键值: Lack"); return "Lack"; } else { for(CampaignPrizeRange e :RemoveCampaignPrizeRanges) { CampaignPrizeRanges.remove(e); } if(Hashcodes.length()>0) { saveToCache(key,CampaignPrizeRanges); } else { logger.info("取缓存的键值: "); return ""; } } } catch(Exception e) { return "Error"; } String keyValue=Hashcodes.substring(0,Hashcodes.length()-1); logger.info("取缓存的键值:"+keyValue); return keyValue; } @Override public synchronized void saveToCache(String key,Object Value ) { logger.info("进入锁保存数据键:"+key); try{ // 添加数据到缓存中 Element element = new Element(key, (Serializable)Value); cache.put(element); logger.info("保存成功"); } catch(Exception e) { logger.info(e.getMessage()); } } @Override public String get(String key){ Element result = cache.get(key); if(result==null) { return "CacheNull"; } @SuppressWarnings("deprecation") String ret= (String) result.getValue(); return ret; } }
标签:
原文地址:http://www.cnblogs.com/linbl/p/4661881.html