`

SimpleCache

阅读更多

缓存现在用的已经比较多了,现存的缓存我们有redis,memcached。

现在我们接触一下本地缓存。SimpleCache,主要用于小量,并且长时间不变的。好处是:不走协议,很快

应用类中有GameAuthorizedServiceImpl。

http://iamzhongyong.iteye.com/blog/2038982

@Override
	public List<FriendInGame> getAuthOfGameCountList(List<String> clientIdList) {
		List<FriendInGame> friendInGameList = new ArrayList<FriendInGame>(clientIdList.size());
		List<String> notFoundGameIdList = new ArrayList<String>(clientIdList.size());
		// 访问本地缓存,获取缓存中的对象
		for (String gameId : clientIdList) {
			Object object = simpleCacheBean.get("getAuthOfGameCountList" + gameId);
			if (object != null) {
				friendInGameList.add((FriendInGame)object);
			} else {
				notFoundGameIdList.add(gameId);
			}
		}
		if (!notFoundGameIdList.isEmpty()) {
			List<FriendInGame> friendInGameTempList =oauthUserAppDao.findAuthOfGameCountList(notFoundGameIdList);
			if (friendInGameTempList != null && !friendInGameTempList.isEmpty()) {
				friendInGameList.addAll(friendInGameTempList);
				for (FriendInGame g : friendInGameTempList) {
					simpleCacheBean.add("getAuthOfGameCountList" + g.getGameId(), g, 3, TimeUnit.DAYS);
				}
			}
		}
		return friendInGameList;	
	}

 

 我们在dao层 写两个实现类,CacheDao和SqlDao,我们dao层利用memcached缓存。我们怎么用simple缓存呢?我们利用Aop,先写一个注解Cacheable,然后spring拦截所有的@Cacheable,然后我们就对service返回的object加缓存。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics