From 3b59a3182cc439a53fbd406551936a631c2a42eb Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:28:30 +0800 Subject: [PATCH] add redis conf, add annotation --- .../java/com/yen/mdblog/BlogApplication.java | 2 ++ .../yen/mdblog/config/RedisCacheConfig.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 springBootBlog/src/main/java/com/yen/mdblog/config/RedisCacheConfig.java diff --git a/springBootBlog/src/main/java/com/yen/mdblog/BlogApplication.java b/springBootBlog/src/main/java/com/yen/mdblog/BlogApplication.java index 8a137c626..dbdfad312 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/BlogApplication.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/BlogApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +@EnableCaching @SpringBootApplication public class BlogApplication { public static void main(String[] args) { diff --git a/springBootBlog/src/main/java/com/yen/mdblog/config/RedisCacheConfig.java b/springBootBlog/src/main/java/com/yen/mdblog/config/RedisCacheConfig.java new file mode 100644 index 000000000..68afe3fff --- /dev/null +++ b/springBootBlog/src/main/java/com/yen/mdblog/config/RedisCacheConfig.java @@ -0,0 +1,27 @@ +package com.yen.mdblog.config; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +//import org.springframework.cache.redis.RedisCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +@Configuration +@EnableCaching +public class RedisCacheConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + return template; + } + + @Bean + public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { + return RedisCacheManager.builder(connectionFactory).build(); + } +} \ No newline at end of file