月度归档:2012年11月

spring整合memcache,webx采用memcache存储session

记录些今天倒腾的东西

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]

velocity基本语法

1、声明:#set ($var=XXX)
左边可以是以下的内容
Variable reference
String literal
Property reference
Method reference
Number literal #set ($i=1)
ArrayList #set ($arr=[“yt1″,”t2”])
算术运算符

2、注释:
单行## XXX
多行#* xxx
xxxx
xxxxxxxxxxxx*#

References 引用的类型
3、变量 Variables
以 “$” 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a .. z or A .. Z).
变量可以包含的字符有以下内容:
alphabetic (a .. z, A .. Z)
numeric (0 .. 9)
hyphen (“-“)
underscore (“_”)

4、Properties
$Identifier.Identifier
$user.name
hashtable user中的的name值.类似:user.get(“name”)

5、Methods
object user.getName() = $user.getName()

6、Formal Reference Notation
用{}把变量名跟字符串分开


#set ($user=”csy”}
${user}name
返回csyname

$username
$!username
$与$!的区别
当找不到username的时候,$username返回字符串”$username”,而$!username返回空字符串””

7、双引号 与 引号
#set ($var=”helo”)
test”$var” 返回testhello
test’$var’ 返回test’$var’
可以通过设置 stringliterals.interpolate=false改变默认处理方式

8、条件语句
#if( $foo )
<strong>Velocity!</strong>
#end
#if($foo)
#elseif()
#else
#end
当$foo为null或为Boolean对象的false值执行.

9、逻辑运算符:== && || !

10、循环语句#foreach($var in $arrays ) // 集合包含下面三种Vector, a Hashtable or an Array
#end
#foreach( $product in $allProducts )
<li>$product</li>
#end

#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end

#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end

11、velocityCount变量在配置文件中定义
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1

12、包含文件
#include( “one.gif”,”two.txt”,”three.htm” )

13、Parse导入脚本
#parse(“me.vm” )

14、#stop 停止执行并返回

15、定义宏Velocimacros ,相当于函数 支持包含功能
#macro( d )
<tr><td></td></tr>
#end
调用
#d()

16、带参数的宏
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end

17、Range Operator
#foreach( $foo in [1..5] )