//(放入memache中,每访问5次更新数据库一次)
if(memcachedClient.get(CommonConstant.SOCIETY_FLEMARKET_BROWSECOUNT_PREFIX+fleaMarket.getId())!=null){
int browseCount = Integer.parseInt(String.valueOf(memcachedClient.get(CommonConstant.SOCIETY_FLEMARKET_BROWSECOUNT_PREFIX+fleaMarket.getId())))+1;
fleaMarket.setBrowseCount(fleaMarket.getBrowseCount()+browseCount);
memcachedClient.replace(CommonConstant.SOCIETY_FLEMARKET_BROWSECOUNT_PREFIX+fleaMarket.getId(), browseCount);
if(browseCount%5==0){ //每5次更新一次数据库
fleaMarketService.saveOrUpdate(fleaMarket);
memcachedClient.replace(CommonConstant.SOCIETY_FLEMARKET_BROWSECOUNT_PREFIX+fleaMarket.getId(), 0);
}
}else{
memcachedClient.add(CommonConstant.SOCIETY_FLEMARKET_BROWSECOUNT_PREFIX+fleaMarket.getId(), fleaMarket.getBrowseCount()+1);
}memache减少数据库的更新次数
缓存计数与数据库更新策略
最新推荐文章于 2025-02-01 20:51:32 发布
本文介绍了一种利用Memcached进行浏览计数的方法,通过缓存减轻数据库负担。每访问五次更新一次数据库,确保了数据一致性的同时减少了数据库的压力。
1015

被折叠的 条评论
为什么被折叠?



