IT袋

当前位置:主页 > 经验教程 > 建站编程 >

多级缓存设计和实战应用

多级缓存设计和实战应用 多层次缓存设计和实际应用(3)

时间:2024-01-27 21:00:21 来源:IT袋 作者:马勇
导读:多级缓存设计和实战应用,第一次访问,打印出SQL,说明查询了数据库: 21:28:49:535 DEBUG 3332 --- [nio-8081-exec-1] c.asdc.item.mapper.ItemMapper.selectOne : ==> Preparing: SELECT id,name,title,price,image,cat

多级缓存设计和实战应用

多级缓存设计和实战应用

第一次访问,打印出SQL,说明查询了数据库:

21:28:49:535 DEBUG 3332 --- [nio-8081-exec-1] c.asdc.item.mapper.ItemMapper.selectOne  : ==>  Preparing: SELECT id,name,title,price,image,category,brand,spec,status,create_time,update_time FROM tb_item WHERE (status <> ? AND id = ?)
21:28:49:536 DEBUG 3332 --- [nio-8081-exec-1] c.asdc.item.mapper.ItemMapper.selectOne  : ==> Parameters: 2(Integer), 10002(Long)
21:28:49:544 DEBUG 3332 --- [nio-8081-exec-1] c.asdc.item.mapper.ItemMapper.selectOne  : <==      Total: 1

第二次访问,未打印出SQL,说明直接查询的本地缓存。

因此,利用Caffeine实现缓存功能生效。

Redis缓存

redis 到底有多快?

根据官方数据,Redis 的 QPS 可以达到约 100000(每秒请求数),感兴趣的可参考:

官方Redis benchmark

以下摘自官网:

With high-end configurations, the number of client connections is also an important factor. Being based on epoll/kqueue, the Redis event loop is quite scalable. Redis has already been benchmarked at more than 60000 connections, and was still able to sustain 50000 q/s in these conditions. As a rule of thumb, an instance with 30000 connections can only process half the throughput achievable with 100 connections. Here is an example showing the throughput of a Redis instance per number of connections:

客户端连接数压测报告:

多级缓存设计和实战应用

X轴:客户端连接数,Y轴:QPS

生产实践中如何使用redis做缓存?

基本流程:

多级缓存设计和实战应用

程序实现:

1、导入依赖

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

2、配置连接

spring:
  redis:
    host: your_ip 
    database: 1
    username: your_username
    password: your_password

3、代码编写:

@Autowired
private StringRedisTemplate redisTemplate;
//JSON 转化
private static final ObjectMapper MAPPER = new ObjectMapper();
@GetMapping("/redis/{id}")
public String queryRedisItemData(@PathVariable("id") Long id) throws JsonProcessingException {
    // 查询Redis缓存
    String cachedResult = redisTemplate.opsForValue().get("item:id:" + id);
    if (Strings.isNotBlank(cachedResult)) {
        System.out.println("从Redis缓存中获取数据:" + cachedResult);
        return cachedResult;
    } else {
        // 1.查询数据库
        Item item = itemService.query().ne("status", 2).eq("id", id).one();
        //转化成json
        String dbResult = MAPPER.writeValueAsString(item);
        System.out.println("从数据库中获取数据:" + dbResult);
        // 将查询结果存入缓存
        redisTemplate.opsForValue().set("item:id:" + id, dbResult);
        return dbResult;
    }
}

4、演示效果:

4.1. 测试前:redis中没有对应key:item:id:10002

相关阅读

  • 编写一个简单的网页结构 网页设计作业html成品

    编写一个简单的网页结构 网页设计作业html成品

    小编为网友们解答编写一个简单的网页结构和网页设计作业html成品方面的介绍,具体详情如下: HTML(HyperText Markup Language)是一种简单、通用的超文本标记语言,Web服务器返回给客户端的最常

  • access数据库的对象是什么 了解access数据库的结构层次

    access数据库的对象是什么 了解access数据库的结构层次

    下面为网友们详细介绍access数据库的对象是什么和了解access数据库的结构层次方面的内容,接下来分享详细内容。 说到数据处理,大家首先想到的是Excel,其实,Office中的数据处理软件除了E

  • web服务器怎么配置 免费个人web服务器推荐

    web服务器怎么配置 免费个人web服务器推荐

    对于许多网友来说web服务器怎么配置和免费个人web服务器推荐方面的内容,具体详情如下: 回想一下一个http请求的过程,你在浏览器输入xxx.com,经过域名解析 发起tcp的3次握手 建立tcp连接后

  • 信息加密、解密与常用算法的基本概念和相关知识

    信息加密、解密与常用算法的基本概念和相关知识

    本文为您带来的是信息加密、解密与常用算法的基本概念和相关知识的话题,请看下面详细的介绍。 一、信息加密概念 信息加密技术是对信息进行伪装,使得信息非法窃取者无法理解信息的真