记录些今天倒腾的东西

1、 搜索maven库,搜索出自己想要的库的groupid等信息

http://mvnrepository.com/

2、 spring下面整合memcache

memcache的java客户端选用的是xmemcache,可以在maven中心库中直接搜索到并使用

[codesyntax lang="xml"]

	<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean">
		<property name="servers">
			<value>127.0.0.1:11211</value>
		</property>
	</bean>

[/codesyntax]

这样就可以在程序中使用MemcachedClient连接类了

[codesyntax lang="java"]

public class Index {

	@Autowired
	private MemcachedClient memcachedClient;

	public void execute(Context context) {

		try {
			WebSiteDO web = new WebSiteDO();
			memcachedClient.set("test111", 30, web);
			WebSiteDO webCache = memcachedClient.get("test111");
			System.out.println(webCache.getImagesPath());
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

[/codesyntax]

3、webx中使用memcache来存储session

修改webx.xml中的session部分

[codesyntax lang="xml"]

 	<session>
            <id>
                <cookie path="/" maxAge="2048" httpOnly="true" />
            </id>
           <stores>
			    <session-stores:store id="simple" class="cn.yundev.xzy.common.MemCacheSession"/> 
			</stores>
            <store-mappings>
                <match name="*" store="simple" />
            </store-mappings>
        </session>

[/codesyntax]

扩展session只需要继承接口SessionStore接口

[codesyntax lang="java"]

public class MemCacheSession implements SessionStore {

	private MemcachedClient memcachedClient;

	public void init(String storeName, SessionConfig sessionConfig) throws Exception {
		ApplicationContext atx = new ClassPathXmlApplicationContext("xmemcache.xml");
		memcachedClient = (MemcachedClient) atx.getBean("memcachedClient");
	}

	public Iterable<String> getAttributeNames(String sessionID,
			StoreContext storeContext) {
		try {
			SessionDO session = memcachedClient.get(sessionID);
			if (session == null) {
				return emptyList();
			} else {
				return session.getSessionData().keySet();
			}
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object loadAttribute(String attrName, String sessionID,
			StoreContext storeContext) {
		try {
			SessionDO session = memcachedClient.get(sessionID);
			if (session != null) {
				Map<String, Object> sessionData = session.getSessionData();
				return sessionData.get(attrName);
			}
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void invaldiate(String sessionID, StoreContext storeContext) {
		try {
			memcachedClient.delete(sessionID);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}

	public void commit(Map<String, Object> attrs, String sessionID,
			StoreContext storeContext) {
		Map<String, Object> sessionData = new HashMap<String, Object>();
		try {
			SessionDO session = memcachedClient.get(sessionID);
			if (session != null) {
				sessionData = session.getSessionData();
			}

			for (Map.Entry<String, Object> entry : attrs.entrySet()) {
				String attrName = entry.getKey();
				Object attrValue = entry.getValue();

				if (attrValue == null) {
					sessionData.remove(attrName);
				} else {
					sessionData.put(attrName, attrValue);
				}
			}
			SessionDO sessionDO = new SessionDO();
			sessionDO.setSessionData(sessionData);
			boolean r = memcachedClient.set(sessionID, 1200, sessionDO);
			System.out.println(r);

		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}

	}

}

[/codesyntax]