Simple-Spring-Memcached 使用简介

来源:互联网 发布:手机电脑桌面软件2016 编辑:程序博客网 时间:2024/06/06 02:43
1.SSM 下载
从开源中国社区提供的链接:
http://www.oschina.net/p/simple-spring-memcached
到官方网站下载文档说明,和各个版本的 ssm 下载。我们下载当前最新版本:
simple-spring-memcached-3.0.2-dist.tar.gz
这个版本要求 Spring 3.0 以上。(注:社区的简介是针对 1.0 版本的)
2.Spring 配置
解压 simple-spring-memcached-3.0.2-dist.tar.gz,复制 lib 目录中所有
jar 到工程 lib 中,如果有相同的 jar,保留版本高的,删除版本低的。复制 dist
目录中除了带有 test 的之外所有 jar 到工程的 lib 中。
在 Sping 工程中,增加一个配置文件,如:applicationContext-ssm.xml 内
容如下:
其中:<property name="address" value="127.0.0.1:12000" />为缓存服务
器的 IP 和端口,将在后面说明。
ssm 缓存
- 2 -
3.利用 SSM 注解使用缓存
在调用 Dao 的地方,使用 ssm:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/
beans
http://www.springframework.org/schema/beans/spring-beans-3
.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.x
sd">
<!-- Simple Spring Memcached -->
<import resource="classpath:simplesm-context.xml" />
<aop:aspectj-autoproxy />
<bean name="defaultMemcachedClient"
class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean
class="com.google.code.ssm.providers.xmemcached.MemcacheClien
tFactoryImpl" />
</property>
<property name="addressProvider">
<bean
class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="127.0.0.1:12000"
/>
</bean>
</property>
<property name="configuration">
<bean
class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true"
/>
</bean>
</property>
</bean>
</beans>
ssm 缓存
- 3 -
import com.google.code.ssm.api.ReadThroughSingleCache;
import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ParameterDataUpdateContent;
import com.google.code.ssm.api.UpdateSingleCache;
import com.google.code.ssm.api.InvalidateSingleCache;
……
//取一个User对象。 缓存的key组成=user+id,缓存时间=300秒
@Override
@Transactional(readOnly=true)
@ReadThroughSingleCache(namespace = "user", expiration = 300)
public User getById(@ParameterValueKeyProvider Long id) {
return userDao.getById(id);
}
//因数据修改时,更新名字空间为 user 的缓存
@Override
@UpdateSingleCache(namespace = "user", expiration = 3)
public void update(@ParameterDataUpdateContent
@ParameterValueKeyProvider User user) {
userDao.update(user);
}
这里必须在对象User中配置@CacheKeyMethod
//删除数据时,删除缓存中的对象
@Override
@InvalidateSingleCache(namespace = "user")
public void removeById(@ParameterValueKeyProvider Long id) {
userDao.deleteById(id);
}
//分页缓存
@Transactional(readOnly=true)
import com.google.code.ssm.api.CacheKeyMethod;
……
@CacheKeyMethod
public String cacheKey() {
return userid.toString();
}
ssm 缓存
- 4 -
@ReadThroughSingleCache(namespace = "users", expiration = 300)
public Page findPage(@ParameterValueKeyProvider UserQuery query) {
return userDao.findPage(query);
}
如果UserQuery对象超过256个字节,同样要在对象UserQuery中配置
@CacheKeyMethod
这里使用了查询参数:名字+电话+手机+邮箱+页码作为 key 的参数。
4.Memcached(for Windows)服务器
从下列链接中:
http://www.oschina.net/p/memcached+for+win32
找到:win32 binary: memcached-1.2.6-win32-bin.zip
1)解压缩 memcached 到磁盘,例如:
D:\memcached.exe
2)安装
memcached.exe –d install
3)配置
编辑注册表,regedit 找到:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server
修改:ImagePath,在后面加上下列语句:
-l 127.0.0.1 -m 64 -p 12000
完成后如:
4)启动
import com.google.code.ssm.api.CacheKeyMethod;
……
@CacheKeyMethod
public String cacheKey() {
return username + "/" + phone + "/" + mobile + "/"
+ email + "/" + super.getPageNumber();
}
ssm 缓存
- 5 -
memcached.exe –d start
注:以后随系统自动启动
5)监控
在命令窗口输入:
telnet 127.0.0.1 12000
连接上后,输入 stats 可以查看状态:
随着缓存的使用,上面状态将会丌断改变。
现在发布你的 spring 工程,可以尝试一下 ssm 缓存给你带来的愉悦感受。
0 0