Multi-level cache implementation

1. What is a multi-level cache

The traditional caching strategy is generally to query Redis first after the request reaches Tomcat, and query the database if it misses, as shown in the figure:

insert image description here

There are following problems:

• Requests are processed by Tomcat, and the performance of Tomcat becomes the bottleneck of the entire system

• When the Redis cache fails, it will impact the database

Multi-level cache is to make full use of each link of request processing, add cache separately, reduce the pressure on Tomcat, and improve service performance:

  • When the browser accesses static resources, it preferentially reads the browser's local cache
  • When accessing non-static resources (ajax query data), access the server
  • After the request reaches Nginx, read the Nginx local cache first
  • If the Nginx local cache misses, then query Redis directly (without Tomcat)
  • Query Tomcat if Redis query misses
  • After the request enters Tomcat, the JVM process cache is first queried
  • Query the database if the JVM process cache misses

insert image description here

In the multi-level cache architecture, Nginx needs to write the business logic of local cache query, Redis query, and Tomcat query. Therefore, such nginx service is no longer a reverse proxy server , but a web server for writing business .

Therefore, such a business Nginx service also needs to build a cluster to improve concurrency, and then have a special nginx service as a reverse proxy, as shown in the figure:

insert image description here

In addition, our Tomcat service will also be deployed in cluster mode in the future:

insert image description here

It can be seen that there are two keys to multi-level caching:

  • One is to write business in nginx to realize nginx local cache, Redis, Tomcat query

  • The other is to implement the JVM process cache in Tomcat

Among them, Nginx programming will use the OpenResty framework combined with languages ​​such as Lua.

This is also the difficulty and the key point.

2. JVM process cache

In order to demonstrate the case of multi-level caching, we first prepare a commodity query business.

2.1. Import case

Reference material "Multi-level cache case description"

2.2. Getting to know Caffeine

Cache plays a vital role in daily development. Because it is stored in memory, the reading speed of data is very fast, which can greatly reduce the access to the database and reduce the pressure on the database. We divide caches into two categories:

  • Distributed cache, such as Redis:
    • Advantages: larger storage capacity, better reliability, and can be shared between clusters
    • Disadvantage: accessing the cache has network overhead
    • Scenario: The amount of cached data is large, the reliability requirements are high, and it needs to be shared between clusters
  • Process local cache, such as HashMap, GuavaCache:
    • Advantages: read local memory, no network overhead, faster
    • Disadvantages: limited storage capacity, low reliability, cannot be shared
    • Scenario: high performance requirements, small amount of cached data

Today we will use the Caffeine framework to implement the JVM process cache.

Caffeine is a high-performance local cache library developed based on Java 8 that provides a near-optimal hit rate. Currently, Spring's internal cache uses Caffeine. GitHub address:
The performance of Caffeine is very good. The following figure is the official performance comparison:

insert image description here

It can be seen that the performance of Caffeine is far ahead!

The basic API used by the cache:

@Test
void testBasicOps() {
    
    
    // 构建cache对象
    Cache<String, String> cache = Caffeine.newBuilder().build();

    // 存数据
    cache.put("gf", "迪丽热巴");

    // 取数据
    String gf = cache.getIfPresent("gf");
    System.out.println("gf = " + gf);

    // 取数据,包含两个参数:
    // 参数一:缓存的key
    // 参数二:Lambda表达式,表达式参数就是缓存的key,方法体是查询数据库的逻辑
    // 优先根据key查询JVM缓存,如果未命中,则执行参数二的Lambda表达式
    String defaultGF = cache.get("defaultGF", key -> {
    
    
        // 根据key去数据库查询数据
        return "柳岩";
    });
    System.out.println("defaultGF = " + defaultGF);
}

Since Caffeine is a kind of cache, it must have a cache clearing strategy, otherwise the memory will always be exhausted.

Caffeine provides three cache eviction strategies:

  • Capacity-based : set an upper limit on the number of caches

    // 创建缓存对象
    Cache<String, String> cache = Caffeine.newBuilder()
        .maximumSize(1) // 设置缓存大小上限为 1
        .build();
    
  • Time-based : Set the effective time of the cache

    // 创建缓存对象
    Cache<String, String> cache = Caffeine.newBuilder()
        // 设置缓存有效期为 10 秒,从最后一次写入开始计时 
        .expireAfterWrite(Duration.ofSeconds(10)) 
        .build();
    
    
  • Reference-based : Set the cache as a soft or weak reference, and use GC to reclaim the cached data. Poor performance, not recommended.

Note : By default, when a cached element expires, Caffeine will not automatically clean and evict it immediately. Instead, the eviction of stale data is done after a read or write operation, or during idle time.

2.3. Realize JVM process cache

2.3.1. Requirements

Use Caffeine to achieve the following requirements:

  • Add a cache to the business of querying products based on id, and query the database when the cache misses
  • Add a cache to the business of querying commodity inventory based on id, and query the database when the cache misses
  • Cache initial size is 100
  • The cache limit is 10000

2.3.2. Implementation

First of all, we need to define two Caffeine cache objects to save the cache data of commodities and inventory respectively.

com.heima.item.configDefine the class under the item-service package CaffeineConfig:

package com.heima.item.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.heima.item.pojo.Item;
import com.heima.item.pojo.ItemStock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CaffeineConfig {
    
    

    @Bean
    public Cache<Long, Item> itemCache(){
    
    
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(10_000)
                .build();
    }

    @Bean
    public Cache<Long, ItemStock> stockCache(){
    
    
        return Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(10_000)
                .build();
    }
}

Then, modify com.heima.item.webthe ItemController class under the package in item-service and add caching logic:

@RestController
@RequestMapping("item")
public class ItemController {
    
    

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemStockService stockService;

    @Autowired
    private Cache<Long, Item> itemCache;
    @Autowired
    private Cache<Long, ItemStock> stockCache;
    
    // ...其它略
    
    @GetMapping("/{id}")
    public Item findById(@PathVariable("id") Long id) {
    
    
        return itemCache.get(id, key -> itemService.query()
                .ne("status", 3).eq("id", key)
                .one()
        );
    }

    @GetMapping("/stock/{id}")
    public ItemStock findStockById(@PathVariable("id") Long id) {
    
    
        return stockCache.get(id, key -> stockService.getById(key));
    }
}

3. Introduction to Lua grammar

Nginx programming needs to use the Lua language, so we must first get started with the basic syntax of Lua.

3.1. Getting to know Lua for the first time

Lua is a lightweight and compact scripting language written in standard C language and open in the form of source code. It is designed to be embedded in applications to provide flexible expansion and customization functions for applications. Official website: https://www.lua.org/

insert image description here

Lua is often embedded in programs developed in C language, such as game development, game plug-ins, etc.

Nginx itself is also developed in C language, so it also allows expansion based on Lua.

3.1.HelloWorld

CentOS7 has installed the Lua language environment by default, so you can run Lua code directly.

1) In any directory of the Linux virtual machine, create a hello.lua file

insert image description here

2) Add the following content

print("Hello World!")  

3) run

3.2. Variables and loops

Learning any language is inseparable from variables, and the declaration of variables must first know the type of data.

3.2.1. Lua data types

Common data types supported in Lua include:

insert image description here

In addition, Lua provides the type() function to determine the data type of a variable:

insert image description here

3.2.2. Declaring variables

Lua does not need to specify the data type when declaring variables, but uses local to declare variables as local variables:

-- 声明字符串,可以用单引号或双引号,
local str = 'hello'
-- 字符串拼接可以使用 ..
local str2 = 'hello' .. 'world'
-- 声明数字
local num = 21
-- 声明布尔类型
local flag = true

The table type in Lua can be used both as an array and as a map in Java. An array is a special table, and the key is just an array subscript:

-- 声明数组 ,key为角标的 table
local arr = {
    
    'java', 'python', 'lua'}
-- 声明table,类似java的map
local map =  {
    
    name='Jack', age=21}

The array subscripts in Lua start from 1, and the access is similar to that in Java:

-- 访问数组,lua数组的角标从1开始
print(arr[1])

Tables in Lua can be accessed using keys:

-- 访问table
print(map['name'])
print(map.name)

3.2.3. Cycle

For table, we can use for loop to traverse. However, arrays and ordinary table traversal are slightly different.

Iterate over the array:

-- 声明数组 key为索引的 table
local arr = {
    
    'java', 'python', 'lua'}
-- 遍历数组
for index,value in ipairs(arr) do
    print(index, value) 
end

Traverse ordinary table

-- 声明map,也就是table
local map = {
    
    name='Jack', age=21}
-- 遍历table
for key,value in pairs(map) do
   print(key, value) 
end

3.3. Conditional control, function

Conditional control and function declarations in Lua are similar to those in Java.

3.3.1. Functions

Syntax for defining a function:

function 函数名( argument1, argument2..., argumentn)
    -- 函数体
    return 返回值
end

For example, define a function to print an array:

function printArr(arr)
    for index, value in ipairs(arr) do
        print(value)
    end
end

3.3.2. Condition control

Java-like conditional control, such as if, else syntax:

if(布尔表达式)
then
   --[ 布尔表达式为 true 时执行该语句块 --]
else
   --[ 布尔表达式为 false 时执行该语句块 --]
end

Unlike java, logical operations in Boolean expressions are based on English words:

insert image description here

3.3.3. Case

Requirement: Customize a function that can print table, and when the parameter is nil, print error message

function printArr(arr)
    if not arr then
        print('数组不能为空!')
    end
    for index, value in ipairs(arr) do
        print(value)
    end
end

4. Implement multi-level caching

The realization of multi-level caching is inseparable from Nginx programming, and Nginx programming is inseparable from OpenResty.

4.1. Install OpenResty

OpenResty® is a high-performance web platform based on Nginx, which is used to easily build dynamic web applications, web services and dynamic gateways that can handle ultra-high concurrency and high scalability. Has the following characteristics:

  • With full functionality of Nginx
  • Based on the Lua language, it integrates a large number of excellent Lua libraries and third-party modules
  • Allows the use of Lua to customize business logic and custom libraries

Official website: https://openresty.org/cn/

insert image description here

To install Lua, you can refer to "Installing OpenResty" provided by the materials

4.2. OpenResty quick start

The multi-level cache architecture we hope to achieve is shown in the figure:

insert image description here

in:

  • Nginx on windows is used as a reverse proxy service to proxy the ajax request of the front-end query product to the OpenResty cluster

  • OpenResty cluster is used to write multi-level cache business

4.2.1. Reverse proxy process

Now, the product detail page is using fake product data. However, in the browser, you can see that the page initiates an ajax request to query real product data.

This request is as follows:

insert image description here

The request address is localhost, the port is 80, and it is received by the Nginx service installed on windows. Then proxy to the OpenResty cluster:

insert image description here

We need to write business in OpenResty, query product data and return to the browser.

But this time, we first receive the request in OpenResty and return fake product data.

4.2.2. OpenResty listens for requests

Many functions of OpenResty depend on the Lua library in its directory. You need to specify the directory of the dependent library in nginx.conf and import the dependencies:

1) Add the loading of OpenResty's Lua module

Modify /usr/local/openresty/nginx/conf/nginx.confthe file, add the following code under http:

#lua 模块
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
#c模块     
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";  

2) Listen to the /api/item path

Modify /usr/local/openresty/nginx/conf/nginx.confthe file, add a monitor for the path /api/item under the server of nginx.conf:

location  /api/item {
    # 默认的响应类型
    default_type application/json;
    # 响应结果由lua/item.lua文件来决定
    content_by_lua_file lua/item.lua;
}

This monitoring is similar to doing path mapping in SpringMVC @GetMapping("/api/item").

Instead content_by_lua_file lua/item.lua, it is equivalent to calling the item.lua file, executing the business in it, and returning the result to the user. It is equivalent to calling service in java.

4.2.3. Write item.lua

1) /usr/loca/openresty/nginxCreate a folder in the directory: lua

insert image description here

2) /usr/loca/openresty/nginx/luaUnder the folder, create a new file: item.lua

insert image description here

3) Write item.lua, return fake data

In item.lua, use the ngx.say() function to return data to Response

ngx.say('{"id":10001,"name":"SALSA AIR","title":"RIMOWA 21寸托运箱拉杆箱 SALSA AIR系列果绿色 820.70.36.4","price":17900,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t6934/364/1195375010/84676/e9f2c55f/597ece38N0ddcbc77.jpg!q70.jpg.webp","category":"拉杆箱","brand":"RIMOWA","spec":"","status":1,"createTime":"2019-04-30T16:00:00.000+00:00","updateTime":"2019-04-30T16:00:00.000+00:00","stock":2999,"sold":31290}')

4) Reload configuration

nginx -s reload

Refresh the product page: http://localhost/item.html?id=1001, you can see the effect:

insert image description here

4.3. Request parameter processing

In the previous section, we received front-end requests in OpenResty, but returned fake data.

To return real data, you must query the product information according to the product id passed from the front end.

So how to get the commodity parameters passed by the front end?

4.3.1. API for obtaining parameters

OpenResty provides some APIs to obtain different types of front-end request parameters:

insert image description here

4.3.2. Get parameters and return

The ajax request initiated at the front end is shown in the figure:

insert image description here

You can see that the product id is passed as a path placeholder, so you can use regular expression matching to get the ID

1) Get the product id

Modify /usr/loca/openresty/nginx/nginx.confthe code that monitors /api/item in the file, and use regular expressions to get the ID:

location ~ /api/item/(\d+) {
    # 默认的响应类型
    default_type application/json;
    # 响应结果由lua/item.lua文件来决定
    content_by_lua_file lua/item.lua;
}

2) Splice the ID and return

Modify /usr/loca/openresty/nginx/lua/item.luathe file, get the id and splice it into the result to return:

-- 获取商品id
local id = ngx.var[1]
-- 拼接并返回
ngx.say('{"id":' .. id .. ',"name":"SALSA AIR","title":"RIMOWA 21寸托运箱拉杆箱 SALSA AIR系列果绿色 820.70.36.4","price":17900,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t6934/364/1195375010/84676/e9f2c55f/597ece38N0ddcbc77.jpg!q70.jpg.webp","category":"拉杆箱","brand":"RIMOWA","spec":"","status":1,"createTime":"2019-04-30T16:00:00.000+00:00","updateTime":"2019-04-30T16:00:00.000+00:00","stock":2999,"sold":31290}')

3) Reload and test

Run the command to reload the OpenResty configuration:

nginx -s reload

Refresh the page to see that the ID is already included in the result:

insert image description here

4.4. Query Tomcat

After getting the product ID, we should go to the cache to query the product information, but we have not yet established nginx and redis caches. Therefore, here we first go to tomcat to query product information according to the product id. We realize the part shown in the figure:

insert image description here

It should be noted that our OpenResty is on a virtual machine, and Tomcat is on a Windows computer. The two IPs must not be confused.

insert image description here

4.4.1. API for sending http requests

nginx provides an internal API for sending http requests:

local resp = ngx.location.capture("/path",{
    
    
    method = ngx.HTTP_GET,   -- 请求方式
    args = {
    
    a=1,b=2},  -- get方式传参数
})

The returned response content includes:

  • resp.status: response status code
  • resp.header: response header, which is a table
  • resp.body: response body, which is the response data

Note: The path here is a path, not including IP and port. This request will be monitored and processed by the server inside nginx.

But we want this request to be sent to the Tomcat server, so we need to write a server to reverse proxy this path:

 location /path {
     # 这里是windows电脑的ip和Java服务端口,需要确保windows防火墙处于关闭状态
     proxy_pass http://192.168.150.1:8081; 
 }

The principle is shown in the figure:

insert image description here

4.4.2. Package http tool

Next, we encapsulate a tool for sending Http requests, and query tomcat based on ngx.location.capture.

1) Add reverse proxy to windows Java service

Because the interfaces in item-service all start with /item, we listen to the /item path and proxy to the tomcat service on windows.

Modify /usr/local/openresty/nginx/conf/nginx.confthe file and add a location:

location /item {
    proxy_pass http://192.168.150.1:8081;
}

In the future, as long as we call ngx.location.capture("/item"), we will be able to send requests to the tomcat service of windows.

2) Encapsulation tool class

As we said before, OpenResty will load the tool files in the following two directories when it starts:

insert image description here

Therefore, custom http tools also need to be placed in this directory.

In /usr/local/openresty/lualibthe directory, create a new common.lua file:

vi /usr/local/openresty/lualib/common.lua

The content is as follows:

-- 封装函数,发送http请求,并解析响应
local function read_http(path, params)
    local resp = ngx.location.capture(path,{
    
    
        method = ngx.HTTP_GET,
        args = params,
    })
    if not resp then
        -- 记录错误信息,返回404
        ngx.log(ngx.ERR, "http请求查询失败, path: ", path , ", args: ", args)
        ngx.exit(404)
    end
    return resp.body
end
-- 将方法导出
local _M = {
    
      
    read_http = read_http
}  
return _M

This tool encapsulates the read_http function into a variable of the table type _M and returns it, which is similar to exporting.

When using, you can use require('common')to import the function library, where common is the file name of the function library.

3) Realize product query

Finally, we modify /usr/local/openresty/lua/item.luathe file and use the function library just encapsulated to query tomcat:

-- 引入自定义common工具模块,返回值是common中返回的 _M
local common = require("common")
-- 从 common中获取read_http这个函数
local read_http = common.read_http
-- 获取路径参数
local id = ngx.var[1]
-- 根据id查询商品
local itemJSON = read_http("/item/".. id, nil)
-- 根据id查询商品库存
local itemStockJSON = read_http("/item/stock/".. id, nil)

The result of the query here is a json string, and contains two json strings of goods and inventory. What the page finally needs is to splice the two json into one json:

insert image description here

This requires us to convert JSON into a lua table first, and then convert it to JSON after completing data integration.

4.4.3. CJSON tool class

OpenResty provides a cjson module to handle JSON serialization and deserialization.

Official address: https://github.com/openresty/lua-cjson/

1) Import the cjson module:

local cjson = require "cjson"

2) Serialization:

local obj = {
    
    
    name = 'jack',
    age = 21
}
-- 把 table 序列化为 json
local json = cjson.encode(obj)

3) Deserialization:

local json = '{"name": "jack", "age": 21}'
-- 反序列化 json为 table
local obj = cjson.decode(json);
print(obj.name)

4.4.4. Realize Tomcat query

Next, we modify the previous business in item.lua and add json processing function:

-- 导入common函数库
local common = require('common')
local read_http = common.read_http
-- 导入cjson库
local cjson = require('cjson')

-- 获取路径参数
local id = ngx.var[1]
-- 根据id查询商品
local itemJSON = read_http("/item/".. id, nil)
-- 根据id查询商品库存
local itemStockJSON = read_http("/item/stock/".. id, nil)

-- JSON转化为lua的table
local item = cjson.decode(itemJSON)
local stock = itemStockJSON.decode(stockJSON)

-- 组合数据
item.stock = stock.stock
item.sold = stock.sold

-- 把item序列化为json 返回结果
ngx.say(cjson.encode(item))

4.4.5. ID-based load balancing

In the code just now, our tomcat is deployed on a single machine. In actual development, tomcat must be in cluster mode:

insert image description here

Therefore, OpenResty needs to load balance the tomcat cluster.

The default load balancing rule is polling mode, when we query /item/10001:

  • For the first time, the tomcat service on port 8081 will be accessed, and a JVM process cache will be formed inside the service
  • For the second time, the tomcat service on port 8082 will be accessed. There is no JVM cache inside the service (because the JVM cache cannot be shared), and the database will be queried.

You see, because of polling, the JVM cache formed by querying 8081 for the first time does not take effect, and it will not take effect until the next access to 8081, and the cache hit rate is too low.

what to do?

If the same product can access the same tomcat service every time it is queried, then the JVM cache will definitely take effect.

In other words, we need to do load balancing based on the product id instead of polling.

1) Principle

Nginx provides an algorithm for load balancing based on request paths:

Nginx performs a hash operation according to the request path, and takes the remainder of the obtained value from the number of tomcat services. If the remainder is a few, it will access the number of services to achieve load balancing.

For example:

  • Our request path is /item/10001
  • The total number of tomcat is 2 (8081, 8082)
  • The result of the remainder of the hash operation on the request path /item/1001 is 1
  • Then access the first tomcat service, which is 8081

As long as the id remains unchanged, the result of each hash operation will not change, so the same product can be guaranteed to access the same tomcat service all the time, ensuring that the JVM cache takes effect.

2) Realize

Modify /usr/local/openresty/nginx/conf/nginx.confthe file to achieve load balancing based on ID.

First, define the tomcat cluster and set up path-based load balancing:

upstream tomcat-cluster {
    hash $request_uri;
    server 192.168.150.1:8081;
    server 192.168.150.1:8082;
}

Then, modify the reverse proxy for the tomcat service, and the target points to the tomcat cluster:

location /item {
    proxy_pass http://tomcat-cluster;
}

Reload OpenResty

nginx -s reload

3) test

Start two tomcat services:

insert image description here

Simultaneously start:

insert image description here

After clearing the log, visit the page again, you can see products with different ids, and access to different tomcat services:

insert image description here

insert image description here

4.5. Redis cache warm-up

Redis cache will face cold start problem:

Cold start : When the service is just started, there is no cache in Redis. If all product data is cached at the first query, it may bring great pressure to the database.

Cache warm-up : In actual development, we can use big data to count hot data accessed by users, and query and save these hot data in advance in Redis when the project starts.

We have a small amount of data, and there is no function related to data statistics. Currently, all data can be put into the cache at startup.

1) Install Redis using Docker

docker run --name redis -p 6379:6379 -d redis redis-server --appendonly yes

2) Introduce Redis dependency in item-service service

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

3) Configure the Redis address

spring:
  redis:
    host: 192.168.150.101

4) Write the initialization class

Cache warming needs to be completed when the project starts, and must be obtained after RedisTemplate.

Here we use the InitializingBean interface to implement, because InitializingBean can be executed after the object is created by Spring and all member variables are injected.

package com.heima.item.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heima.item.pojo.Item;
import com.heima.item.pojo.ItemStock;
import com.heima.item.service.IItemService;
import com.heima.item.service.IItemStockService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisHandler implements InitializingBean {
    
    

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemStockService stockService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        // 初始化缓存
        // 1.查询商品信息
        List<Item> itemList = itemService.list();
        // 2.放入缓存
        for (Item item : itemList) {
    
    
            // 2.1.item序列化为JSON
            String json = MAPPER.writeValueAsString(item);
            // 2.2.存入redis
            redisTemplate.opsForValue().set("item:id:" + item.getId(), json);
        }

        // 3.查询商品库存信息
        List<ItemStock> stockList = stockService.list();
        // 4.放入缓存
        for (ItemStock stock : stockList) {
    
    
            // 2.1.item序列化为JSON
            String json = MAPPER.writeValueAsString(stock);
            // 2.2.存入redis
            redisTemplate.opsForValue().set("item:stock:id:" + stock.getId(), json);
        }
    }
}

4.6. Query Redis cache

Now that the Redis cache is ready, we can implement the logic of querying Redis in OpenResty. As shown in the red box below:

insert image description here

After the request enters OpenResty:

  • Query the Redis cache first
  • If the Redis cache misses, then query Tomcat

4.6.1. Package Redis tools

OpenResty provides a module to operate Redis, we can use it directly as long as we import this module. But for convenience, we encapsulate the Redis operation into the previous common.lua tool library.

Modify /usr/local/openresty/lualib/common.luathe file:

1) Introduce the Redis module and initialize the Redis object

-- 导入redis
local redis = require('resty.redis')
-- 初始化redis
local red = redis:new()
red:set_timeouts(1000, 1000, 1000)

2) The encapsulation function is used to release the Redis connection, which is actually put into the connection pool

-- 关闭redis连接的工具方法,其实是放入连接池
local function close_redis(red)
    local pool_max_idle_time = 10000 -- 连接的空闲时间,单位是毫秒
    local pool_size = 100 --连接池大小
    local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
    if not ok then
        ngx.log(ngx.ERR, "放入redis连接池失败: ", err)
    end
end

3) Encapsulate function, query Redis data according to key

-- 查询redis的方法 ip和port是redis地址,key是查询的key
local function read_redis(ip, port, key)
    -- 获取一个连接
    local ok, err = red:connect(ip, port)
    if not ok then
        ngx.log(ngx.ERR, "连接redis失败 : ", err)
        return nil
    end
    -- 查询redis
    local resp, err = red:get(key)
    -- 查询失败处理
    if not resp then
        ngx.log(ngx.ERR, "查询Redis失败: ", err, ", key = " , key)
    end
    --得到的数据为空处理
    if resp == ngx.null then
        resp = nil
        ngx.log(ngx.ERR, "查询Redis数据为空, key = ", key)
    end
    close_redis(red)
    return resp
end

4) export

-- 将方法导出
local _M = {
    
      
    read_http = read_http,
    read_redis = read_redis
}  
return _M

The complete common.lua:

-- 导入redis
local redis = require('resty.redis')
-- 初始化redis
local red = redis:new()
red:set_timeouts(1000, 1000, 1000)

-- 关闭redis连接的工具方法,其实是放入连接池
local function close_redis(red)
    local pool_max_idle_time = 10000 -- 连接的空闲时间,单位是毫秒
    local pool_size = 100 --连接池大小
    local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
    if not ok then
        ngx.log(ngx.ERR, "放入redis连接池失败: ", err)
    end
end

-- 查询redis的方法 ip和port是redis地址,key是查询的key
local function read_redis(ip, port, key)
    -- 获取一个连接
    local ok, err = red:connect(ip, port)
    if not ok then
        ngx.log(ngx.ERR, "连接redis失败 : ", err)
        return nil
    end
    -- 查询redis
    local resp, err = red:get(key)
    -- 查询失败处理
    if not resp then
        ngx.log(ngx.ERR, "查询Redis失败: ", err, ", key = " , key)
    end
    --得到的数据为空处理
    if resp == ngx.null then
        resp = nil
        ngx.log(ngx.ERR, "查询Redis数据为空, key = ", key)
    end
    close_redis(red)
    return resp
end

-- 封装函数,发送http请求,并解析响应
local function read_http(path, params)
    local resp = ngx.location.capture(path,{
    
    
        method = ngx.HTTP_GET,
        args = params,
    })
    if not resp then
        -- 记录错误信息,返回404
        ngx.log(ngx.ERR, "http查询失败, path: ", path , ", args: ", args)
        ngx.exit(404)
    end
    return resp.body
end
-- 将方法导出
local _M = {
    
      
    read_http = read_http,
    read_redis = read_redis
}  
return _M

4.6.2. Realize Redis query

Next, we can modify the item.lua file to query Redis.

The query logic is:

  • Query Redis based on id
  • Continue to query Tomcat if the query fails
  • Return query results

1) Modify /usr/local/openresty/lua/item.luathe file and add a query function:

-- 导入common函数库
local common = require('common')
local read_http = common.read_http
local read_redis = common.read_redis
-- 封装查询函数
function read_data(key, path, params)
    -- 查询本地缓存
    local val = read_redis("127.0.0.1", 6379, key)
    -- 判断查询结果
    if not val then
        ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key)
        -- redis查询失败,去查询http
        val = read_http(path, params)
    end
    -- 返回数据
    return val
end

2) Then modify the business of commodity query and inventory query:

insert image description here

3) Complete item.lua code:

-- 导入common函数库
local common = require('common')
local read_http = common.read_http
local read_redis = common.read_redis
-- 导入cjson库
local cjson = require('cjson')

-- 封装查询函数
function read_data(key, path, params)
    -- 查询本地缓存
    local val = read_redis("127.0.0.1", 6379, key)
    -- 判断查询结果
    if not val then
        ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key)
        -- redis查询失败,去查询http
        val = read_http(path, params)
    end
    -- 返回数据
    return val
end

-- 获取路径参数
local id = ngx.var[1]

-- 查询商品信息
local itemJSON = read_data("item:id:" .. id,  "/item/" .. id, nil)
-- 查询库存信息
local stockJSON = read_data("item:stock:id:" .. id, "/item/stock/" .. id, nil)

-- JSON转化为lua的table
local item = cjson.decode(itemJSON)
local stock = cjson.decode(stockJSON)
-- 组合数据
item.stock = stock.stock
item.sold = stock.sold

-- 把item序列化为json 返回结果
ngx.say(cjson.encode(item))

4.7. Nginx local cache

Now, there is only the last link in the entire multi-level cache, which is the local cache of nginx. As shown in the picture:

insert image description here

4.7.1. Local cache API

OpenResty provides Nginx with the function of shard dict , which can share data among multiple workers of nginx and realize the caching function.

1) Open the shared dictionary and add the configuration under http in nginx.conf:

 # 共享字典,也就是本地缓存,名称叫做:item_cache,大小150m
 lua_shared_dict item_cache 150m; 

2) Operate the shared dictionary:

-- 获取本地缓存对象
local item_cache = ngx.shared.item_cache
-- 存储, 指定key、value、过期时间,单位s,默认为0代表永不过期
item_cache:set('key', 'value', 1000)
-- 读取
local val = item_cache:get('key')

4.7.2. Realize local cache query

1) Modify /usr/local/openresty/lua/item.luathe file, modify the read_data query function, and add local cache logic:

-- 导入共享词典,本地缓存
local item_cache = ngx.shared.item_cache

-- 封装查询函数
function read_data(key, expire, path, params)
    -- 查询本地缓存
    local val = item_cache:get(key)
    if not val then
        ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key)
        -- 查询redis
        val = read_redis("127.0.0.1", 6379, key)
        -- 判断查询结果
        if not val then
            ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key)
            -- redis查询失败,去查询http
            val = read_http(path, params)
        end
    end
    -- 查询成功,把数据写入本地缓存
    item_cache:set(key, val, expire)
    -- 返回数据
    return val
end

2) Modify the business of querying goods and inventory in item.lua to implement the latest read_data function:

insert image description here

In fact, there are more cache time parameters. After the expiration, the nginx cache will be automatically deleted, and the cache can be updated next time you visit.

Here, the timeout period for the basic information of the product is set to 30 minutes, and the inventory is set to 1 minute.

Because the inventory update frequency is high, if the cache time is too long, it may be quite different from the database.

3) Complete item.lua file:

-- 导入common函数库
local common = require('common')
local read_http = common.read_http
local read_redis = common.read_redis
-- 导入cjson库
local cjson = require('cjson')
-- 导入共享词典,本地缓存
local item_cache = ngx.shared.item_cache

-- 封装查询函数
function read_data(key, expire, path, params)
    -- 查询本地缓存
    local val = item_cache:get(key)
    if not val then
        ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key)
        -- 查询redis
        val = read_redis("127.0.0.1", 6379, key)
        -- 判断查询结果
        if not val then
            ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key)
            -- redis查询失败,去查询http
            val = read_http(path, params)
        end
    end
    -- 查询成功,把数据写入本地缓存
    item_cache:set(key, val, expire)
    -- 返回数据
    return val
end

-- 获取路径参数
local id = ngx.var[1]

-- 查询商品信息
local itemJSON = read_data("item:id:" .. id, 1800,  "/item/" .. id, nil)
-- 查询库存信息
local stockJSON = read_data("item:stock:id:" .. id, 60, "/item/stock/" .. id, nil)

-- JSON转化为lua的table
local item = cjson.decode(itemJSON)
local stock = cjson.decode(stockJSON)
-- 组合数据
item.stock = stock.stock
item.sold = stock.sold

-- 把item序列化为json 返回结果
ngx.say(cjson.encode(item))

5. Cache synchronization

In most cases, what the browser queries is cached data. If there is a large difference between the cached data and the database data, serious consequences may occur.

So we must ensure the consistency of database data and cache data, which is the synchronization between cache and database.

5.1. Data synchronization strategy

There are three common ways to cache data synchronization:

Set validity period : Set a validity period for the cache, and it will be automatically deleted after expiration. update when querying again

  • Advantages: simple and convenient
  • Disadvantages: poor timeliness, may be inconsistent before the cache expires
  • Scenario: business with low update frequency and low timeliness requirements

Synchronous double write : directly modify the cache while modifying the database

  • Advantages: strong timeliness, strong consistency between cache and database
  • Disadvantages: code intrusion, high coupling;
  • Scenario: Cache data that requires high consistency and timeliness

**Asynchronous notification: **Send an event notification when the database is modified, and related services modify the cached data after listening to the notification

  • Advantages: low coupling, multiple cache services can be notified at the same time
  • Disadvantages: general timeliness, there may be intermediate inconsistencies
  • Scenario: General timeliness requirements, multiple services need to be synchronized

The asynchronous implementation can be implemented based on MQ or Canal:

1) MQ-based asynchronous notification:

insert image description here

Interpretation:

  • After the commodity service finishes modifying the data, it only needs to send a message to MQ.
  • The cache service listens to the MQ message, and then completes the update of the cache

There is still a small amount of code intrusion.

2) Notification based on Canal

insert image description here

Interpretation:

  • After the commodity service completes the commodity modification, the business ends directly without any code intrusion
  • Canal monitors MySQL changes, and immediately notifies the cache service when a change is found
  • The cache service receives the canal notification and updates the cache

code zero intrusion

5.2. Install Canal

5.2.1. Understanding Canal

Canal [kə'næl] , translated as waterway/pipe/ditch, canal is an open source project under Alibaba, developed based on Java. Provides incremental data subscription & consumption based on database incremental log analysis. GitHub address: https://github.com/alibaba/canal

Canal is implemented based on mysql master-slave synchronization. The principle of MySQL master-slave synchronization is as follows:

insert image description here

  • 1) MySQL master writes data changes to the binary log (binary log), and the recorded data is called binary log events
  • 2) MySQL slave copies the master's binary log events to its relay log (relay log)
  • 3) MySQL slave replays the events in the relay log and reflects the data changes to its own data

And Canal pretends to be a slave node of MySQL, so as to monitor the binary log changes of the master. Then notify the Canal client of the obtained change information, and then complete the synchronization of other databases.

insert image description here

5.2.2. Install Canal

Install and configure Canal reference document "Canal Installation and Configuration"

5.3. Monitor Canal

Canal provides clients in various languages. When Canal detects binlog changes, it will notify the Canal client.

insert image description here

We can use the Java client provided by Canal to listen to Canal notification messages. When a change message is received, the cache is updated.

But here we will use the third-party open source canal-starter client on GitHub. Address: https://github.com/NormanGyllenhaal/canal-client

Perfect integration with SpringBoot, automatic assembly, much simpler and easier to use than the official client.

5.3.1. Introducing dependencies:

<dependency>
    <groupId>top.javatool</groupId>
    <artifactId>canal-spring-boot-starter</artifactId>
    <version>1.2.1-RELEASE</version>
</dependency>

5.3.2. Write configuration:

canal:
  destination: heima # canal的集群名字,要与安装canal时设置的名称一致
  server: 192.168.150.101:11111 # canal服务地址

5.3.3. Modify the Item entity class

Complete the mapping between Item and database table fields through @Id, @Column, and other annotations:

package com.heima.item.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;

import javax.persistence.Column;
import java.util.Date;

@Data
@TableName("tb_item")
public class Item {
    
    
    @TableId(type = IdType.AUTO)
    @Id    // 标记表中的ID字段
    private Long id;//商品id
    @Column(name = "name")    // 标记表中与属性名不一致字段
    private String name;//商品名称
    private String title;//商品标题
    private Long price;//价格(分)
    private String image;//商品图片
    private String category;//分类名称
    private String brand;//品牌名称
    private String spec;//规格
    private Integer status;//商品状态 1-正常,2-下架
    private Date createTime;//创建时间
    private Date updateTime;//更新时间
    @TableField(exist = false)    // 标注不属于表中字段
    @Transient
    private Integer stock;
    @TableField(exist = false)
    @Transient
    private Integer sold;
}

5.3.4. Writing listeners

EntryHandler<T>Write a listener by implementing the interface to listen to Canal messages. Note two points:

  • The implementation class @CanalTable("tb_item")specifies the table information to monitor
  • The generic type of EntryHandler is the entity class corresponding to the table
package com.heima.item.canal;

import com.github.benmanes.caffeine.cache.Cache;
import com.heima.item.config.RedisHandler;
import com.heima.item.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@CanalTable("tb_item")
@Component
public class ItemHandler implements EntryHandler<Item> {
    
    

    @Autowired
    private RedisHandler redisHandler;
    @Autowired
    private Cache<Long, Item> itemCache;

    @Override
    public void insert(Item item) {
    
    
        // 写数据到JVM进程缓存
        itemCache.put(item.getId(), item);
        // 写数据到redis
        redisHandler.saveItem(item);
    }

    @Override
    public void update(Item before, Item after) {
    
    
        // 写数据到JVM进程缓存
        itemCache.put(after.getId(), after);
        // 写数据到redis
        redisHandler.saveItem(after);
    }

    @Override
    public void delete(Item item) {
    
    
        // 删除数据到JVM进程缓存
        itemCache.invalidate(item.getId());
        // 删除数据到redis
        redisHandler.deleteItemById(item.getId());
    }
}

The operations on Redis here are all encapsulated in the RedisHandler object, which is a class we wrote when we were doing cache warm-up. The content is as follows:

package com.heima.item.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heima.item.pojo.Item;
import com.heima.item.pojo.ItemStock;
import com.heima.item.service.IItemService;
import com.heima.item.service.IItemStockService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisHandler implements InitializingBean {
    
    

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IItemService itemService;
    @Autowired
    private IItemStockService stockService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        // 初始化缓存
        // 1.查询商品信息
        List<Item> itemList = itemService.list();
        // 2.放入缓存
        for (Item item : itemList) {
    
    
            // 2.1.item序列化为JSON
            String json = MAPPER.writeValueAsString(item);
            // 2.2.存入redis
            redisTemplate.opsForValue().set("item:id:" + item.getId(), json);
        }

        // 3.查询商品库存信息
        List<ItemStock> stockList = stockService.list();
        // 4.放入缓存
        for (ItemStock stock : stockList) {
    
    
            // 2.1.item序列化为JSON
            String json = MAPPER.writeValueAsString(stock);
            // 2.2.存入redis
            redisTemplate.opsForValue().set("item:stock:id:" + stock.getId(), json);
        }
    }

    public void saveItem(Item item) {
    
    
        try {
    
    
            String json = MAPPER.writeValueAsString(item);
            redisTemplate.opsForValue().set("item:id:" + item.getId(), json);
        } catch (JsonProcessingException e) {
    
    
            throw new RuntimeException(e);
        }
    }

    public void deleteItemById(Long id) {
    
    
        redisTemplate.delete("item:id:" + id);
    }
}

If there are any deficiencies, please give more advice,
to be continued, continue to update!
Let's make progress together!

Guess you like

Origin blog.csdn.net/qq_40440961/article/details/128848793