600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜

Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜

时间:2019-01-28 00:04:38

相关推荐

Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜

Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜

一直想用Spring boot 搭建一个属于自己的博客系统,刚好前段时间学习了叶神的牛客项目课受益匪浅,乘热打铁也主要是学习,好让自己熟悉这类项目开发的基本流程。系统采用Spring boot+MyBatis+MySQL的框架进行项目开发。

项目源码:Jblog

个人主页:tuzhenyu’s page

原文地址:Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜

0. 思路

文章点击量是记录访问文章页的次数,可以通过Spring拦截器或Spring AOP实现对文章页请求的拦截,每次拦截到请求后增加相应文章的一次点击量。阅读排行榜是对每篇文章的点击量进行排序,取出排序后的点击量排名前10的文章作为热门文章。

如果使用数据库记录点击次数,每次用户访问文章页需要写一次数据库,当并发量较大的时候写数据库是一种很影响性能的操作。可以在系统中使用Redis作为内存数据库用来存储页面点击量等信息,同时Redis具有丰富的数据结构,其中的Sorted Sets可以用来做根据点击量的热门文章排序。

1. 数据模型

系统采用Redis的zset数据结构存储文章点击量,zset是一种有序集合。每个有序集合的成员都关联着一个评分,这个评分用于把有序集合中的成员按最低分到最高分排列。在热门文章排序中,用于有序集合排序的就是每篇文章的点击量。

引入Redis的java操作jar包

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.0</version><type>jar</type><scope>compile</scope></dependency>

有序集合zset的操作函数

@Servicepublic class JedisService implements InitializingBean{private JedisPool pool;@Overridepublic void afterPropertiesSet() throws Exception {pool = new JedisPool("redis://localhost:6379/1");}public double zincrby(String key,String value){Jedis jedis = pool.getResource();double result = jedis.zincrby(key,1,value);jedis.close();return result;}public Set<String> zrevrange(String key,int start, int end){Jedis jedis = pool.getResource();Set<String> set = jedis.zrevrange(key,start,end);jedis.close();return set;}}

2. 文章点击量

利用Spring拦截器拦截多有文章页的请求,并在Redis中修改相应文章页的点击量,用于热门文章排序。

实现文章点击量的拦截器

@Componentpublic class ArticleClickInterceptor implements HandlerInterceptor {@Autowiredprivate JedisService jedisService;@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {String uri = httpServletRequest.getServletPath().split("/")[2];String uriKey = RedisKeyUntil.getClickCountKey(uri);jedisService.zincrby("hotArticles",uriKey);return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}}

在系统中添加此拦截器

@Componentpublic class WebConfiguration extends WebMvcConfigurerAdapter{@Autowiredprivate PassportInterceptor passportInterceptor;@Autowiredprivate LoginRequestInterceptor loginRequestInterceptor;@Autowiredprivate ArticleClickInterceptor articleClickInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(passportInterceptor);registry.addInterceptor(loginRequestInterceptor).addPathPatterns("/create");registry.addInterceptor(articleClickInterceptor).addPathPatterns("/article/*");super.addInterceptors(registry);}}

3. 热门文章排行榜

利用Redis的有序结合zset存放文章点击量,有序集合可以很方便的获取按照点击量的文章排序。从zset中用函数zrevrange获取点击量最多的10篇文章作为热门文章,显示在主页的热门文章列表。

@RequestMapping(path = {"/","/index"})public String index(Model model){List<ViewObject> vos = new ArrayList<>();List<Article> articles = articleService.getLatestArticles(0,4);for (Article article:articles){ViewObject vo = new ViewObject();List<Tag> tags = tagService.getTagByArticleId(article.getId());String clickCount = jedisService.get(RedisKeyUntil.getClickCountKey("/article/"+article.getId()));if (clickCount==null)clickCount = "0";vo.set("clickCount",clickCount);vo.set("article",article);vo.set("tags",tags);vos.add(vo);}model.addAttribute("vos",vos);List<Article> hotArticles = new ArrayList<>();Set<String> set = jedisService.zrevrange("hotArticles",0,6);for (String str : set){int articleId = Integer.parseInt(str.split(":")[1]);Article article = articleService.getArticleById(articleId);hotArticles.add(article);}model.addAttribute("hotArticles",hotArticles);return "index";}

4. 总结

文章点击量的实现可以利用Spring拦截器拦截文章页的访问请求,也可以利用Spring AOP面向切面编程,将改变点击量的代码织入文章页访问请求的controller处理器中。考虑到在并发量很大的时候,如果每次访问页面都要写数据库会很影响系统性能,引入Redis内存型Key-Value数据库存储文章的点击量。同时Redis的丰富的数据结构也为按照点击量排序获取热门文章列表提供了极大的便利。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。