(没有用注解)springboot+EHcache 实现文章浏览量的缓存和超时更新

2019-09-28 07:58:57

问题描述

当我们需要统计文章的浏览量的时候,最常规的做法就是:

1.访问文章链接www.abc.com/article/{id}

2.在控制层获取Article实体

3.得到文章浏览量count并且count++

4.最后update实体Article。

这么做对没有访问量的网站来说很棒,如果网站访问量很大,这么不停的读写数据库,会对服务器造成很大的压力。

解决思路

引入Ehcache,将文章的访问量存在cache中,每点击一次文章,将cache中的count加1.在有效的时间内访问文章只是将cache中的数据+1,超过指定时间则进行一次数据库更新。

解决方案

本文是在springboot整合ehcache的环境下验证的。springboot版本1.5.2 。ehcache版本2.6.11。springboot整合ehcache的步骤很简单,下面简单提一下,在pom文件中引入ehcache依赖

<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.6.11</version>
</dependency>

在类路径下存放ehcache.xml文件。

在application.yml中指定:

spring: 
 cache:
  jcache:
   config: classpath:ehcache.xml

最后在启动类标注@EnableCaching

引入缓存之后,接着我们的正题

在ehcache.xml文件中定义dayHits缓存

<cache name="dayHits" maxEntriesLocalHeap="500" eternal="true" overflowToDisk="true"> 
</cache>

表示保存当日点击量的

在controller层定义缓存点击量的方法

 public Integer cacheCount(Long articleId){
    Content content = contentRepository.findOne(articleId);
    Ehcache cache = cacheManager.getEhcache("dayHits");
    Element element = cache.get(articleId+"_count");
    Integer count = 0;
    if(element!=null){
      count = (Integer) element.getValue();
    }else{
      count = content.getHits()== null?0:content.getHits();
    }
    count++;
    cache.put(new Element(articleId+"_count",count));
    cache.put(new Element(articleId+"_dayHitsDate",SystemUtils.getNowDate()));
    Long time = System.currentTimeMillis();
    if(time > (viewArticleTime+ 300000)){
      viewArticleTime = time;
      content.setHits(count);
      contentRepository.save(content);
      cache.removeAll();
    }
    return count;
  }

3.在查看文章方法中进行调用。

 @RequestMapping(value = "article/{id}",method = RequestMethod. GET)
  public String detail(@PathVariable Long id,ModelMap map){
  Integer hits = cacheCount(id);
  }

4.其中局部变量的定义:

 private static CacheManager cacheManager = CacheManager.newInstance();
  private static Long viewArticleTime = System.currentTimeMillis();

5.保存访问看看效果吧。


  • 2021-01-21 13:52:36

    node.js使用Nodemailer发送邮件

    常常看到一些网站有邮箱获取验证码验证注册或者修改密码等,今天也来了解一下在nodejs + express怎么发送电子邮件。使用模块Nodemailer。这里以qq邮箱举例子。

  • 2021-01-21 13:55:53

    Mongodb字段更新$unset操作符

    当使用$操作符匹配任何数组元素,$unset替换指定的元素为null而不是删除掉指定的元素,此行为保持数组大小和位置一直;

  • 2021-01-22 08:30:02

    Android IO简化之Okio库

    如果之前有使用过Okhttp,那么你一定知道底层的IO读取是由square公司开发的Okio库。它补充了Java.io和java.nio的不足,以便能够更加方便,快速的访问、存储和处理你的数据。而在一般的开发中,我们也可以使用Okio来做IO读写,非常方便深得我心

  • 2021-01-22 21:56:48

    emcc生成wasm,wast,bc文件的方法

    Emscripten实现把C/C++文件转成wasm,wast(wasm的可读形式),llvm字节码(bc格式),ll格式(llvm字节码的可读形式)的步骤。

  • 2021-01-22 21:59:34

    emcc编译与部分重要参数选取

    C/C++代码通过emcc编译为字节码,然后根据不同的目标编译为asm.js或wasm。emcc和gcc编译选项类似,例如-s OPTIONS=VALUE、-O等。另外为了适应Web环境,emcc增加了一些特有的选项,如–pre-js 、–post-js 等。

  • 2021-01-22 22:01:19

    Emscripten Compiler Frontend (emcc)

    The Emscripten Compiler Frontend (emcc) is used to call the Emscripten compiler from the command line. It is effectively a drop-in replacement for a standard compiler like gcc or clang.