The previous article mainly introduced the use of cache in spring , but the cases in this article all use local memory as the storage medium, but in fact, after our project goes online, it will basically be deployed in the form of a cluster. If the data is stored in In local memory, clusters cannot be shared. We can store data in redis to achieve cache sharing. Let's take a look at how @EnableCaching connects to redis in Spring .
install redis
Download address: https://redis.io/download
Introduce redis configuration in pom.xml
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.3</version>
</dependency>
Create a redis configuration file in the project
Newly created com/javacode2018/cache/demo2/redis.yml
, the content is as follows:
singleServerConfig:
address: "redis://127.0.0.1:6379"
password: null
clientName: null
database: 7 #选择使用哪个数据库0~15
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
codec:
class: "org.redisson.codec.JsonJacksonCodec"
Create redis-related beans
package com.javacode2018.cache.demo2;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
@ComponentScan
@EnableCaching //@1
public class MainConfig2 {
@Bean //@2
public CacheManager cacheManager() throws IOException {
RedissonSpringCacheManager cacheManager = new RedissonSpringCacheManager(this.redissonClient());
cacheManager.setCacheNames(Arrays.asList("cache1"));
return cacheManager;
}
@Bean //@3
public RedissonClient redissonClient() throws IOException {
InputStream is = MainConfig2.class.getResourceAsStream("/com/javacode2018/cache/demo2/redis.yml");
Config config = Config.fromYAML(is);
return Redisson.create(config);
}
}
@1: Enable the spring cache function.
@2: Customize the cache manager in spring. Here we define a redis type manager, and the bottom layer uses redis as the storage medium for the cache.
@3: Create a RedissonClient through the redis.yml configuration file to interact with redis.
Come to a test class
package com.javacode2018.cache.demo2;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class BookService {
@Cacheable(cacheNames = "cache1", key = "#root.targetClass.name+'-'+#root.method.name")
public List<String> list() {
System.out.println("---模拟从db中获取数据---");
return Arrays.asList("java高并发", "springboot", "springcloud");
}
}
test case
@Test
public void test7() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
BookService bookService = context.getBean(BookService.class);
System.out.println(bookService.list());
System.out.println(bookService.list());
{
System.out.println("下面打印出cache1缓存中的key列表");
RedissonSpringCacheManager cacheManager = context.getBean(RedissonSpringCacheManager.class);
RedissonCache cache1 = (RedissonCache) cacheManager.getCache("cache1");
cache1.getNativeCache().keySet().stream().forEach(System.out::println);
}
}
run output
--- Simulate getting data from db --- [java high concurrency, springboot, springcloud] [java high concurrency, springboot, springcloud] The following prints out the key list com.javacode2018.cache.demo2.BookService-list in the cache1 cache
RedisDesktopManager
At this point the data has entered redis, let's take a look with the redis client tool .
RedisDesktopManager
download link
Link: https://pan.baidu.com/s/1WCd-tk8dDDJnFIKciVIQsA Extraction code: x728
After decompression, click the following to run directly
Case source code
https://gitee.com/javacode2018/spring-series
Passerby A All java case codes will be put on this in the future, everyone watch it, you can continue to pay attention to the dynamics.
Source: https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648936334&idx=2&sn=7565a7528bb24d090ce170e456e991ce&scene=21#wechat_redirect