SpringCloud项目-tensquare练习2 redis

第二章 查询与缓存

1.招聘微服务开发

1.1 表结构分析

招聘微服务主要有两块:企业信息和招聘信息
在这里插入图片描述
招聘信息表 tb_recruit
在这里插入图片描述

appcation.yml:
server:
  port: 9002
spring:
  application:
    name: tensquare-recruit
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.11.131:3306/tensquare_recruit?characterEncoding=UTF8
    username: root
    password: 123456
  jpa:
    database: MySQL
    show-sql: true

1.2.1 查询企业表ishot字段为1的记录

/**
* 根据热门状态获取企业列表 
* @param ishot 
* * @return 
* */
 public List<Enterprise> findByIshot(String ishot);
/**
	 * 	热门企业列表
	 */
	public List<Enterprise> hotlist(){ return enterpriseDao.findByIshot("1"); }

1.3.1 推荐职位列表
需求分析:查询状态为2并以创建日期降序排序,查询前4条记录
(1)在RecruitDao新增方法定义

/** * 查询最新职位列表(按创建日期降序排序) * @return */
public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state);

(2)RecruitService新增方法

/*** 根据状态查询 * @param state * @return */ 
public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state){ return recruitDao.findTop4ByStateOrderByCreatetimeDesc(state); }

1.3.3 最新职位列表
需求分析:查询状态不为0并以创建日期降序排序,查询前12条记录
(1)在RecruitDao新增方法定义

/*** 最新职位列表 * @param state * @return */
 public List<Recruit> findTop12ByStateNotOrderByCreatetimeDesc(String state);

2.问答微服务开发

2.1 表结构分析

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
2.2.1 热门回答列表
需求分析:按回复数降序排序
(1)ProblemDao新增方法定义

/*** 根据标签ID查询热门问题列表 * @param labelId * @param pageable * @return */ 
@Query("select p from Problem p where id in( select problemid from Pl where labelid=?1 ) order by reply desc") 
public Page<Problem> findHotListByLabelId(String labelId, Pageable pageable);2)ProblemService新增方法

/*** 根据标签ID查询热门问题列表 * @param lableId 标签ID * @param page 页码 * @param size 页大小 * @return */

public Page<Problem> findHotListByLabelId(String lableId,int page, int size) { 
	PageRequest pageRequest = PageRequest.of(page‐1, size); 
	return problemDao.findHotListByLabelId(lableId,pageRequest); 
}

(3)ProblemController新增方法
/*** 根据标签ID查询热门问题列表 * @param labelid * @return */
@RequestMapping(value="/hotlist/{labelid}/{page}/{size}",method=RequestMe thod.GET)

public Result findHotListByLabelId(@PathVariable String labelid,@PathVariable int page,@PathVariable int size ){ 
 	Page<Problem> pageList = problemService.findHotListByLabelId(labelid, page, size);
 	 PageResult<Problem> pageResult = new PageResult<> (pageList.getTotalElements(), pageList.getContent()); 
 	 return new Result(true, StatusCode.OK, "查询成功",pageResult);
}

3. 缓存处理

为了提高查询的性能,我们通常采用Redis缓存解决。

3.1 Redis环境搭建

我们以docker的形式搭建Redis 服务

docker run ‐di ‐‐name=tensquare_redis ‐p 6379:6379 redis

3.2 SpringDataRedis

Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问 redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate 提供了redis各种操作。

3.3 实现文章的缓存处理

3.3.1 查询文章操作缓存
(1)在tensquare_article 的pom.xml引入依赖

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

(2)修改application.yml ,在spring节点下添加配置
spring.redis.host

  redis:
    host: 192.168.11.129:6379

(3)修改ArticleService 引入RedisTemplate,并修改findById方法

	@Autowired private 
	RedisTemplate redisTemplate;

  /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Article findById(String id) {
        Article article = (Article) redisTemplate.opsForValue().get("article_" + id);
        if (article == null) {
            article = articleDao.findById(id).get();
            redisTemplate.opsForValue().set("article_" + id, article, 10, TimeUnit.MINUTES);
        }
        return article;
    }

这样在查询的时候,就会自动将文章放入缓存
3.4. 修改或删除后清除缓存

  /**
     * 修改
     *
     * @param article
     */
    public void update(Article article) {
        redisTemplate.delete("articel_" + article.getId());
        articleDao.save(article);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
		redisTemplate.delete("articel_" + id);
		articleDao.deleteById(id);
    }

3.3.3缓存过期处理
修改findById方法 ,设置1天的过期时间

redisTemplate.opsForValue().set("article_" + id, article,1, TimeUnit.DAYS);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值