由于项目中要使用到缓存,memcached当然是第一选择了,memcached在php用使用比较多,用java调用相对少点。我们系统是java开发,看看文档用起来一觉得比较爽。
memcached安装就不用多说了,网上资料很多,我只说下java的调用。
先下载memcached-release-2.0.1.jar客户端开发包,有兴趣的可以把源代码也下下来看看。
别人喜欢spring,我都是用spring管理bean,下面通过注入使用memcahed。
spring注入配置:
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.1.233:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>
<property name="minConn">
<value>10</value>
</property>
<property name="maxConn">
<value>50</value>
</property>
<property name="maintSleep">
<value>30</value>
</property>
<property name="nagle">
<value>false</value>
</property>
<property name="socketTO">
<value>3000</value>
</property>
</bean>
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<property name="compressEnable">
<value>true</value>
</property>
<property name="compressThreshold">
<value>4096</value>
</property>
</bean>
其实也可以自己写客户端调用SockIOPool和MemCachedClient。
MemCachedClient memcachedClient;
public void setMemcachedClient(MemCachedClient memcachedClient) {
this.memcachedClient = memcachedClient;
}
public void test(){
memcachedClient.set(key, value);//往cache中保存数据
memcachedClient.get(key); //获取数据
}
就那么简单,由于项目需要,我单独把缓存处理独立出来作为一个工程,通过hesian提供远程调用,这样使用起来还觉得比较爽。
而在项目部署使用中我们用maven来管理部署,使用起来还是比较方便的。
在一个大型项目中如果采用J2EE结构,我觉得能够使用如下组合应该比较完美
spring,ibatis,memched,hessian,maven,mysql,jboss
再按照规范的J2EE结构去开发,把业务逻辑层web层,数据层分清楚。那应该是一个很错的项目。
memcached安装就不用多说了,网上资料很多,我只说下java的调用。
先下载memcached-release-2.0.1.jar客户端开发包,有兴趣的可以把源代码也下下来看看。
别人喜欢spring,我都是用spring管理bean,下面通过注入使用memcahed。
spring注入配置:
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.1.233:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>
<property name="minConn">
<value>10</value>
</property>
<property name="maxConn">
<value>50</value>
</property>
<property name="maintSleep">
<value>30</value>
</property>
<property name="nagle">
<value>false</value>
</property>
<property name="socketTO">
<value>3000</value>
</property>
</bean>
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<property name="compressEnable">
<value>true</value>
</property>
<property name="compressThreshold">
<value>4096</value>
</property>
</bean>
其实也可以自己写客户端调用SockIOPool和MemCachedClient。
MemCachedClient memcachedClient;
public void setMemcachedClient(MemCachedClient memcachedClient) {
this.memcachedClient = memcachedClient;
}
public void test(){
memcachedClient.set(key, value);//往cache中保存数据
memcachedClient.get(key); //获取数据
}
就那么简单,由于项目需要,我单独把缓存处理独立出来作为一个工程,通过hesian提供远程调用,这样使用起来还觉得比较爽。
而在项目部署使用中我们用maven来管理部署,使用起来还是比较方便的。
在一个大型项目中如果采用J2EE结构,我觉得能够使用如下组合应该比较完美
spring,ibatis,memched,hessian,maven,mysql,jboss
再按照规范的J2EE结构去开发,把业务逻辑层web层,数据层分清楚。那应该是一个很错的项目。