日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發布時間:2023/12/18 windows 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring boot 搭建個人博客系統(六)——文章點擊量和閱讀排行榜

一直想用Spring boot 搭建一個屬于自己的博客系統,剛好前段時間學習了葉神的??晚椖空n受益匪淺,乘熱打鐵也主要是學習,好讓自己熟悉這類項目開發的基本流程。系統采用Spring boot+MyBatis+MySQL的框架進行項目開發。

項目源碼:Jblog
個人主頁:tuzhenyu’s page
原文地址:Spring boot 搭建個人博客系統(六)——文章點擊量和閱讀排行榜

0. 思路

  文章點擊量是記錄訪問文章頁的次數,可以通過Spring攔截器或Spring AOP實現對文章頁請求的攔截,每次攔截到請求后增加相應文章的一次點擊量。閱讀排行榜是對每篇文章的點擊量進行排序,取出排序后的點擊量排名前10的文章作為熱門文章。
 如果使用數據庫記錄點擊次數,每次用戶訪問文章頁需要寫一次數據庫,當并發量較大的時候寫數據庫是一種很影響性能的操作??梢栽谙到y中使用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的操作函數
@Service public 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中修改相應文章頁的點擊量,用于熱門文章排序。

  • 實現文章點擊量的攔截器
@Component public 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 {} }
  • 在系統中添加此攔截器
@Component public 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的豐富的數據結構也為按照點擊量排序獲取熱門文章列表提供了極大的便利。

總結

以上是生活随笔為你收集整理的Spring boot 搭建个人博客系统(六)——文章点击量和阅读排行榜的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。