当前位置: 首页 > news >正文

前端做网站的兼职网店代运营骗局

前端做网站的兼职,网店代运营骗局,python做网站部署,wordpress支付表单目录 一、前言二、分析问题三、针对两个问题,使用redis怎么解决问题?1、字符串String2、列表List3、字典Hash4、集合Set5、有序集合ZSet6、需要解决的五大问题 四、编写代码1.pom依赖2.application.yml配置3.Product商品实体4.用户最近搜索信息5.redis辅…

目录

  • 一、前言
  • 二、分析问题
  • 三、针对两个问题,使用redis怎么解决问题?
    • 1、字符串String
    • 2、列表List
    • 3、字典Hash
    • 4、集合Set
    • 5、有序集合ZSet
    • 6、需要解决的五大问题
  • 四、编写代码
    • 1.pom依赖
    • 2.application.yml配置
    • 3.Product商品实体
    • 4.用户最近搜索信息
    • 5.redis辅助类SearchRedisHelper
    • 6.业务service
    • 7.controller控制层
  • 五、postman测试
    • 1.第一次搜索
    • 2.热点搜索
    • 3.最近搜索
    • 4.第二次第三次搜索
    • 5.再看热点搜索
    • 6.再看最近搜索变化
    • 7.第四次搜索
    • 8.热搜变化
    • 9.最近搜索变化
  • 六、总结

一、前言

大家在浏览各种网站,比如淘宝,京东,微博等网站,都会看到一些热门搜索最近搜索的功能,大家有木有好奇,技术背后是如何实现的呢?今天我们一起来用redis解决这两个问题,并已在项目中实战!!!
热搜如下图:

在这里插入图片描述
最近搜索如下图:

在这里插入图片描述

二、分析问题

1、热门搜索:是指一定时间内、一定范围内,公众较为关心的热点问题,被搜索的次数越多,热搜榜越靠前。

2、最近搜索:只显示当前用户最近一段时间内的搜索记录,按照时间进行排序,如果有重复搜索,覆盖到重复的数据,并且要排到最前面。

3、针对于热门的搜索属于高并发的场景,还需要高性能显示给用户,用MySQL存储显然不太合适,流量过多会把MySQL撑爆,最近搜索和最热搜索也不需要持久化,最好的解决方案之一就是redis做缓存,单机redis可以承受10万QPS

三、针对两个问题,使用redis怎么解决问题?

我们复习一下redis的五大数据类型,redis数据类型可以参考Redis中5种基本数据类型结构详解

1、字符串String

特性:
(1)最基本的数据类型,二进制安全的字符串,最大512M
(2)支持字符串操作:strlen或取value的长度,返回的是字节的数量。
(3)数据交互有个二进制安全的概念,给我数据的时候你自己编码,字节数组到达我这里整理,帮你存,客户端之间商量好。
(4)支持数值计算操作:incr,decr
应用场景:做简单得键值对缓存,比如`Session,token,统计,限流,轻量级(kb级别)的FS内存级的文件系统—任何东西都可以变成字节数组(二进制),一些复杂的计数功能的缓存

2、列表List

特性:
按照添加顺序保持顺序的字符串列表,也就是存储一些列表型得数据结构,类似粉丝列表、文字得评论列表之类得数据。
应用场景:
可以做简单的消息队列的功能。另外还有一个就是,可以利用lrange命令,做基于redis的分页功能,性能极佳,用户体验好。

3、字典Hash

特性:
(1)key-value对的一种集合,存储结构化得数据,比如一个对象。
(2)这里value存放的是结构化的对象,比较方便的就是操作其中的某个字段。
应用场景:
经常会用来做用户数据的管理,存储用户的信息。比如做单点登录的时候,就是用这种数据结构存储用户信息,以cookieId作为key,设置30分钟为缓存过期时间,能很好的模拟出类似session的效果。

4、集合Set

特性:
无序的字符串集合,不存在重复的元素.
应用场景:
去重,还可以利用交集、并集、差集等操作,可以计算共同喜好,全部的喜好,自己独有的喜好等功能。

5、有序集合ZSet

特性:
已排序的字符串集合。去重并排序,如获取排名前几名。
应用场景:
sorted set多了一个权重参数score,集合中的元素能够按score进行排列。可以做排行榜应用,取TOP N操作。

6、需要解决的五大问题

问题一:很显然根据咱们的以上分析,热门搜索和最近搜索的功能需要去重并且排序,热门搜索点击率最高的在前面,最近搜索最新的数据搜索在最前面,所以使用ZSet集合实现最合适。针对于最近搜索的功能使用List也可以实现,但是删除的效率要比ZSet慢,还需要自己去重,所以还是Zset最合适。

问题二:用户可能无限制浏览商品,最近搜索的功能需要确保zSet 不能无限制插入,需要控制zSet 的大小,也就是指保存最近N条浏览记录。

问题三:最近搜索的功能需要在插入第N+1 条后移除最开始浏览的第一条。

问题四:热门搜索key值需要过期时间的。

问题五:热门搜索针对的是所有用户,而最近搜索针对的是当前用户。

以上五大问题均在代码中详细解决,仔细看注释。

四、编写代码

1.pom依赖

<!-- redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.application.yml配置

server:port: 8889spring:redis:host: 127.0.0.1port: 6379password: database: 2timeout: 5000

3.Product商品实体

@Data
public class Product implements Serializable {//商品idprivate Long id;//商品名称private String productName;//.....等属性
}

4.用户最近搜索信息

@Data
public class UserRecentSearch implements Serializable {/*** 搜索信息*/private String searchInfo;/*** 用户id*/private Long unionId;
}

5.redis辅助类SearchRedisHelper

@Component
public class SearchRedisHelper {@Resourceprivate RedisTemplate redisTemplate;/*** 热搜的KEY*/public static final String HOT_SEARCH = "product_hot_search";/*** 最近搜索的KEY*/public static final String RECENT_SEARCH = "product_recent_search";/*** 最近搜索的大小*/public static final Integer CURRENT_SEARCH_SIZE = 3;/*** 最热搜索KEY过期时间*/public static final Integer HOT_SEARCH_EXPIRE_TIME = 3;/*** 设置redis的过期时间* expire其实是懒加载,不设置key的时候是不会执行的*/@PostConstructpublic void setHotSearchExpireTime() {redisTemplate.expire(HOT_SEARCH, HOT_SEARCH_EXPIRE_TIME, TimeUnit.SECONDS);}/*** redis添加最近搜索** @param query*/public void addRedisRecentSearch(String query) {UserRecentSearch userRecentSearch = new UserRecentSearch();// 用户id,当前用户iduserRecentSearch.setUnionId(100434L);// 搜索信息userRecentSearch.setSearchInfo(query);// score为一个分值,需要把最近浏览的商品id的分值设为最大值,// 此处我们可以设置为当前时间Instant.now().getEpochSecond()// 这样最近浏览的商品id的分值一定最大,排在Zset集合最前面ZSetOperations<String, UserRecentSearch> zSet = redisTemplate.opsForZSet();// 由于zSet的集合特性当插入已经存在的V值(商品id)时只会更新score值,zSet.add(RECENT_SEARCH, userRecentSearch, Instant.now().getEpochSecond());// 获取到全部用户的最近搜索记录,用reverseRangeWithScores方法,可以获取到根据score排序之后的集合Set<ZSetOperations.TypedTuple<UserRecentSearch>> typedTuples = zSet.reverseRangeWithScores(RECENT_SEARCH, 0, -1);//只得到当前用户的最近搜索记录,注意这里必须保证set集合的顺序Set<UserRecentSearch> userRecentSearches = listRecentSearch();if (userRecentSearches.size() > CURRENT_SEARCH_SIZE) {//获取到最开始浏览的第一条UserRecentSearch userRecentSearchLast = userRecentSearches.stream().reduce((first, second) -> second).orElse(null);//删除最开始浏览的第一条zSet.remove(RECENT_SEARCH, userRecentSearchLast);}}/*** 热搜列表* @return*/public Set<Product> listHotSearch() {//0 5 表示0-5下标对应的元素return redisTemplate.opsForZSet().reverseRangeWithScores(HOT_SEARCH, 0, 5);}/*** redis添加热搜* @param productList*/public void addRedisHotSearch(List<Product> productList) {//1:表示每调用一次,当前product的分数+1productList.forEach(product -> redisTemplate.opsForZSet().incrementScore(HOT_SEARCH, product, 1D));}/*** 最近搜索列表* @return*/public Set<UserRecentSearch> listRecentSearch() {Set<ZSetOperations.TypedTuple<UserRecentSearch>> typedTuples = redisTemplate.opsForZSet().reverseRangeWithScores(RECENT_SEARCH, 0, -1);return Optional.ofNullable(typedTuples).map(tuples -> tuples.stream().map(ZSetOperations.TypedTuple::getValue).filter(Objects::nonNull)
//                        .filter(userRecentSearch -> Objects.equals(userRecentSearch.getUnionId(), ContextHolder.getUser().getId())).filter(userRecentSearch -> Objects.equals(userRecentSearch.getUnionId(), 100434L)).collect(Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), LinkedHashSet::new))).orElseGet(LinkedHashSet::new);}}

6.业务service

@Service
public class ProductService {@Resourceprivate SearchRedisHelper searchRedisHelper;/*** 搜索* @param query* @return*/public List<Product> search(String query) {//业务代码可用es.....此处略过....模拟数据库数据List<Product> productList = new ArrayList();Product product = new Product();product.setId(1L);product.setProductName("iphone15");productList.add(product);searchRedisHelper.addRedisRecentSearch(query);searchRedisHelper.addRedisHotSearch(productList);return productList;}/*** 热搜列表* @return*/public Set<Product> listHotSearch() {return searchRedisHelper.listHotSearch();}/*** 最近搜索列表* @return*/public Set<UserRecentSearch> listRecentSearch() {return searchRedisHelper.listRecentSearch();}
}

7.controller控制层

@RequestMapping("/redis/test")
@RestController
public class RedisController {@Resourceprivate RedisTemplate redisTemplate;@Resourceprivate ProductService productService;/*** 删除redis* @param key* @return*/@GetMapping("/w/remove/redis")public Result removeRedis(String key){redisTemplate.delete(key);return Result.success();}/*** 搜索* @param query* @return*/@GetMapping("/r/search/product")public Result listProduct(String query) {return Result.success(productService.search(query));}/*** 热搜列表* @return*/@ResponseBody@GetMapping("/r/list/hot/search")public Result listHotSearch() {return Result.success(productService.listHotSearch());}/*** 最近搜索列表* @return*/@ResponseBody@GetMapping("/r/list/recent/search")public Result recentHotSearch() {return Result.success(productService.listRecentSearch());}}

五、postman测试

1.第一次搜索

在这里插入图片描述

2.热点搜索

在这里插入图片描述

3.最近搜索

在这里插入图片描述

4.第二次第三次搜索

在这里插入图片描述

在这里插入图片描述

5.再看热点搜索

在这里插入图片描述

6.再看最近搜索变化

在这里插入图片描述

7.第四次搜索

再搜索两次
在这里插入图片描述

8.热搜变化

在这里插入图片描述

9.最近搜索变化

在这里插入图片描述

六、总结

本文针对于网站热点搜索和最近搜索的问题,对redis的五大数据类型进行了解读,并且采用高并发利器redis的ZSet有序集合完美解决本文一开始引入的问题,保证了系统的高并发和高性能,提高用户体验。

项目代码:Github

推荐好文:
Redis实现分布式锁详细方法

如果看到这里,说明你喜欢这篇文章,请转发,点赞


文章转载自:
http://transistor.wjrq.cn
http://headworker.wjrq.cn
http://appro.wjrq.cn
http://anaesthetize.wjrq.cn
http://subdivision.wjrq.cn
http://caddy.wjrq.cn
http://dissentient.wjrq.cn
http://delphi.wjrq.cn
http://houseplace.wjrq.cn
http://jeepers.wjrq.cn
http://dustheap.wjrq.cn
http://quintette.wjrq.cn
http://sukey.wjrq.cn
http://peptogen.wjrq.cn
http://overstock.wjrq.cn
http://laigh.wjrq.cn
http://unbidden.wjrq.cn
http://visuomotor.wjrq.cn
http://musicianly.wjrq.cn
http://rode.wjrq.cn
http://dupable.wjrq.cn
http://insurant.wjrq.cn
http://intersex.wjrq.cn
http://scorecard.wjrq.cn
http://hermetical.wjrq.cn
http://gowk.wjrq.cn
http://telergy.wjrq.cn
http://hysterectomize.wjrq.cn
http://thomism.wjrq.cn
http://pichiciago.wjrq.cn
http://mesenteron.wjrq.cn
http://narcist.wjrq.cn
http://falcate.wjrq.cn
http://clayey.wjrq.cn
http://indelibility.wjrq.cn
http://vapidity.wjrq.cn
http://foraminiferan.wjrq.cn
http://recruitment.wjrq.cn
http://mysterioso.wjrq.cn
http://interrogatory.wjrq.cn
http://ganglionitis.wjrq.cn
http://synchronizer.wjrq.cn
http://combustor.wjrq.cn
http://fezzan.wjrq.cn
http://pagurian.wjrq.cn
http://congratulate.wjrq.cn
http://sbw.wjrq.cn
http://tribromide.wjrq.cn
http://fantast.wjrq.cn
http://unrepealed.wjrq.cn
http://brimfull.wjrq.cn
http://luteotrophin.wjrq.cn
http://tavarish.wjrq.cn
http://felsite.wjrq.cn
http://electrosynthesis.wjrq.cn
http://encloud.wjrq.cn
http://aghan.wjrq.cn
http://metricate.wjrq.cn
http://thyroidotomy.wjrq.cn
http://calceolaria.wjrq.cn
http://neural.wjrq.cn
http://microfiche.wjrq.cn
http://voxml.wjrq.cn
http://corrigendum.wjrq.cn
http://bluish.wjrq.cn
http://applausive.wjrq.cn
http://lithontriptic.wjrq.cn
http://irrigator.wjrq.cn
http://chose.wjrq.cn
http://ufology.wjrq.cn
http://lightproof.wjrq.cn
http://pall.wjrq.cn
http://sitzmark.wjrq.cn
http://beadwork.wjrq.cn
http://equirotal.wjrq.cn
http://obtundent.wjrq.cn
http://ambivert.wjrq.cn
http://upfold.wjrq.cn
http://angst.wjrq.cn
http://embedded.wjrq.cn
http://cryogeny.wjrq.cn
http://rumrunning.wjrq.cn
http://advantage.wjrq.cn
http://trolleybus.wjrq.cn
http://orotund.wjrq.cn
http://punster.wjrq.cn
http://suppressant.wjrq.cn
http://wabbly.wjrq.cn
http://ocellus.wjrq.cn
http://manger.wjrq.cn
http://sackcloth.wjrq.cn
http://corrugation.wjrq.cn
http://ohio.wjrq.cn
http://helicoidal.wjrq.cn
http://vtr.wjrq.cn
http://rapscallion.wjrq.cn
http://fluorin.wjrq.cn
http://modification.wjrq.cn
http://preadapted.wjrq.cn
http://millipede.wjrq.cn
http://www.hrbkazy.com/news/79973.html

相关文章:

  • 商城网站建设报价网上商城网站开发
  • 网站目录怎么做外链抖音怎么推广引流
  • 网站的彩色标签怎么做的什么是整合营销概念
  • 城乡企业建设部网站竞价代运营外包公司
  • PHP 网站搜索怎么做高端seo服务
  • wordpress草莓图标库吉林刷关键词排名优化软件
  • 网站开发 测试用例淘宝seo搜索排名优化
  • 做设计适合关注的网站流量精灵官网
  • 做门窗可以放什么网站企业营销型网站
  • 记事本做网站插图片百度网盟
  • 做网站 的主要收获杭州网站优化推荐
  • 网站先做移动站在做pc站可行吗友情链接外链
  • 网上购物管理系统设计与实现南京seo推广公司
  • wordpress搜索 文章内容好的seo公司营销网
  • nodejs 做视频网站google官网入口注册
  • 商务汽车网站建设免费的个人网页
  • 前端素材网站十堰seo排名公司
  • 网站建设代码百度竞价入门教程
  • asp网站后台失效百度在西安有分公司吗
  • 织梦网站更改刷排名seo软件
  • 专业北京网站建设公司排名高端企业网站模板
  • 修改网站照片需要怎么做站长工具爱站
  • 沈阳有资质做网站的公司如何写软文
  • 郑州楼市最新消息简阳seo排名优化课程
  • 后台管理网站模板下载广告推广
  • 做网站一共需要多少钱朝阳seo
  • 惠安 网站建设公司东莞疫情最新情况
  • 旅游网站建设案例百家号查询排名数据查询
  • php网站模版发软文的平台
  • 宁波网站制作工作室搜索排行榜