About the parameter analysis of @Cacheable in springBoot for caching

Parameters – value : the name of the cache, defined in the spring configuration file, at least one
parameter must be specified – key : the key of the cache, which can be empty, if specified, it should be written according to the SpEL expression, if not specified, it will default to the method’s All parameters are combined
Parameters – condition : The condition of the cache, which can be empty, written using SpEL expressions, returns true or false, and only caches when it is true

Presumably you must want to know what a SpEL expression is, let's briefly introduce it below:
SpEL expression: Spring 3 introduces the Spring Expression Language (Spring Expression Language, SpEL), which can express values ​​in a powerful and concise way Assembled into bean properties and constructor parameters, the expressions used in this process will be calculated at runtime to get
values

Syntax of @Cacheable:

@Cacheable(value=”缓存的名称”,key=”#xxx”)

The syntax of redis command line client using hset is:

hset key field value

Using the @Cacheable annotation, the set function of redis is called, and the generated value and key are connected with ::. The result is as follows:
The result of the test class

Test class try:
create a service:

package com.tanhua.server.test;

import com.tanhua.model.db.UserInfo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserInfoCacheService {
    
    

    //根据id查询
    @Cacheable(value = "user",key = "#userId")
    public UserInfo queryById(Long userId) {
    
    
        //从数据库查询
        System.out.println("从数据库查询");
        UserInfo user = new UserInfo();
        user.setId(userId);
        user.setNickname("ceshi");
        return user;
    }
}

Create a test class:

package com.tanhua.server.test;

import com.tanhua.server.AppServerApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = AppServerApplication.class)
@RunWith(SpringRunner.class)
public class CacheTest {
    
    

    @Autowired
    private UserInfoCacheService userInfoCacheService;

    @Test
    public void test1() {
    
    
        for (int i = 1; i <= 5; i++)
            System.out.println(userInfoCacheService.queryById(1l));
    }
}

The result of the first execution:
insert image description here
The result of the second execution:
insert image description here
From here we can see that the data has been cached, and a file is generated in redis. The file structure is as shown in the figure:
insert image description here
user is value 1 is key
insert image description here
if there is an error please correct me

Guess you like

Origin blog.csdn.net/qq_54042324/article/details/122072736