gamecenter有用代码(一)
1、接口XCacheFactory
public interface XCacheFactory { /** * 根据指定prefix来获取XCache实例 * * @param <T> * @param prefix * @param klass value的类型 * @param isCounter 是否用作counter * @return */ public <T> XCache<T> getCache(String prefix, Class<T> klass, boolean isCounter);}
public class XCacheFactoryImpl implements XCacheFactory { private MemcachedClientFactory memcachedClientFactory = new MemcachedClientFactory(); public MemCachedClient getMemcachedClient(String namespace) { return memcachedClientFactory.getClientByNamespace(namespace); }@Overridepublic <T> XCache<T> getCache(String prefix, Class<T> valueClass, boolean isCounter) {return new XCacheImpl<T>(prefix, this, valueClass, isCounter);}}
public MemcachedClientFactory() { init(); } /** * 初始化 */ public void init() { namespaceFactory = ZooKeeperBasedNamespaceFactory.getInstance(); poolFactory = ZooKeeperBasedPoolFactory.getInstance(); poolFactory.addCallback(poolConfigModifiedCallback); //注册回调 }
long cur3 = System.currentTimeMillis();if (logger.isDebugEnabled()) { logger.debug("build apppage:" + (System.currentTimeMillis() - cur3)); }
// 匹配appPageSupport if (CollectionUtils.isNotEmpty(appPageSupportList)) { for (Iterator<AppPageSupport> iterator = appPageSupportList.iterator(); iterator .hasNext();) { AppPageSupport appPageSupportTemp = (AppPageSupport) iterator.next(); if (appPageSupportTemp.getAppPageId() == appPageEntity.getId()) { appPageSupports.add(appPageSupportTemp); iterator.remove(); } } }
List<Brand> brands = cache.get(TOP_PAGE_KEY + os, List.class);String hotStr = RESOURCE_BUNDLE.getString("hot" + os);if (hotStr != null) { JSONArray jarr = JSONArray.fromObject(hotStr); brands = (List<Brand>) JSONArray.toCollection(jarr, Brand.class); }
if (CollectionUtils.isNotEmpty(brands)) { Collections.sort(brands, new Comparator<Brand>() { public int compare(Brand o1, Brand o2) { return o1.getOrder() - o2.getOrder(); }; }); cache.save(TOP_PAGE_KEY + os, brands, List.class, pageDefaultCacheTime); }
sort(appPageList, new ComparatorAppPage() { @Override public int compare(AppPage page0, AppPage page1) { int compareOrder = 0; //如展示顺序相同则比较更新时间 if (page0.getAddTime() != null && page1.getAddTime() != null) { compareOrder = page0.getAddTime().compareTo(page1.getAddTime()); if (compareOrder == 0) { return page0.getDisplayOrder() - page1.getDisplayOrder(); } else { return compareOrder; } } else { return 0; } } });
public void afterPropertiesSet() throws Exception { Assert.notNull(appPageRelationDAO, "appPageRelationDAO is required!"); Assert.notNull(appPageDAO, "appPageDAO is required!"); Assert.notNull(appPageSupportDAO, "appPageSupportDAO is required!"); Assert.notNull(friendsFacade, "friendsFacade is required!"); }
public int updateAppPageSupport(AppPageSupport appPageSupport) { int result = appPageSupportDAO.update(appPageSupport); reloadApp(appPageSupport.getAppPageId()); return result; }
private String[] convertToStringArray(List<Integer> keys) { if (CollectionUtils.isEmpty(keys)) { return null; } int listSize = keys.size(); String[] array = new String[listSize]; int emptyKey = 0; for (int i = 0; i < listSize; i++) { Integer key = keys.get(i); if (key != null) { array[i] = key.toString(); } else { emptyKey++; } } if (emptyKey == listSize) { return null; } return array; }
private List<Integer> convertToIntegerList(Integer[] keys) { if (ArrayUtils.isEmpty(keys)) { return null; } List<Integer> list = new ArrayList<Integer>(); for (Integer key : keys) { list.add(key); } return list; }