读书人

ehcache兑现网络寻呼

发布时间: 2012-11-10 10:48:50 作者: rapoo

ehcache实现网络寻呼

本篇文章为原创,转载请说明出处

?

寻呼最基本的要求就是及时性,然而B/S架构的系统属于请求应答模式,所以服务器不能直接通知客户端后台的数据变化
一般我们为了解决这个问题就是要前端异步的轮循请求数据,但是这样随着客户端的增加对服务器的压力会随之增大,所以
我们可以取一个折中的办法,就是使用缓存。这样我们的服务器不会每次请求都去数据库中取而是在缓存中查询,这样每次回话
时间就会减少,响应的压力也会有所降低,但不会没有。

?

缓存的切入点:

1.用户登录时将该用户未读的寻呼加入缓存
2.用户退出时将该用户的寻呼缓存清除
3.其他人发寻呼时如果发送对象是在线用户,需要更新缓存
4.用户点击已阅寻呼时,需要更新缓存
5.寻呼回复,需要更新缓存

?

?

缓存采用ehcache+spring集成。如果想看详细配置过程介绍,请看此文章

这里就不详细介绍每个配置的含义了

?

首先创建ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache><diskStore path="java.io.tmpdir/jcs" /><defaultCache maxElementsInMemory="10000" overflowToDisk="true" eternal="false"memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="600"timeToIdleSeconds="3600" timeToLiveSeconds="100000" diskPersistent="false" /><cache name="callCache" overflowToDisk="false" eternal="true" diskPersistent="false" timeToLiveSeconds="3600" timeToIdleSeconds="3600" maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU" ></cache></ehcache>

?

?第二步:配置spring-ehcache.xml

?

<?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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"default-lazy-init="true"><description>EhCache配置文件</description><!-- EHCache 缓存配置 --><bean id="cacheManager" value="classpath:ehcache.xml" /></bean><!-- 网络寻呼缓存 --><bean id="callCache" /></property><property name="cacheName"><value>callCache</value></property></bean></beans>

?

经上面2步配置就完了

接下来编写接口ICallCacheService

public interface ICallCacheService {public int getSizeOfUser(String userId);public void putCallInCache(String userId);public void removeCallFormCache(String userId);public boolean isExist(Serializable key);public List get(Serializable key);public void remove(Serializable key);public List get(Serializable key,int pageSize);public void put(Serializable key,List<Serializable> value);public void removeCall(Serializable key,String receiveId);}

?编写实现类

@Transactional@Component("callCacheService")public class CallCacheService implements ICallCacheService {@Resource(name="callCache")private Cache callCache;@Autowired@Qualifier("callReceiveService")private ICallReceiveService callReceiveService;

?具体怎么实现每一个方法就没贴代码了,不是很复杂

?

最后结合上面提到的缓存切入点

什么时候put进缓存,什么时候clear缓存

?

读书人网 >编程

热点推荐