From 958438737bfe870f954c0990fa4e79a9e2621fbc Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:25:20 +0800 Subject: [PATCH 1/6] add redis dep, conf --- springBootBlog/pom.xml | 21 +++++++++++++++++++ .../src/main/resources/application.properties | 8 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/springBootBlog/pom.xml b/springBootBlog/pom.xml index 3da190e0e..675cc2ed8 100644 --- a/springBootBlog/pom.xml +++ b/springBootBlog/pom.xml @@ -124,6 +124,27 @@ test + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.springframework.data + spring-data-redis + 3.0.11 + + + + + org.apache.commons + commons-pool2 + 2.11.1 + + diff --git a/springBootBlog/src/main/resources/application.properties b/springBootBlog/src/main/resources/application.properties index 94727f22f..d32eb1da5 100644 --- a/springBootBlog/src/main/resources/application.properties +++ b/springBootBlog/src/main/resources/application.properties @@ -17,4 +17,10 @@ spring.security.oauth2.client.registration.github.clientId=8c3b3cfa5ba107d7ad25 spring.security.oauth2.client.registration.github.clientSecret= # google login # custom error html -server.error.path=/error \ No newline at end of file +server.error.path=/error + +# redis +spring.cache.type=redis +spring.redis.host=localhost +spring.redis.port=6379 +spring.redis.timeout=60000 \ No newline at end of file From 3b59a3182cc439a53fbd406551936a631c2a42eb Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:28:30 +0800 Subject: [PATCH 2/6] 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 From 721d3260d2fe0e8a6b102cd8ff596be1d048772c Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:33:39 +0800 Subject: [PATCH 3/6] fix dep version, make app can run --- springBootBlog/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/springBootBlog/pom.xml b/springBootBlog/pom.xml index 675cc2ed8..7dbe29094 100644 --- a/springBootBlog/pom.xml +++ b/springBootBlog/pom.xml @@ -135,14 +135,14 @@ org.springframework.data spring-data-redis - 3.0.11 + org.apache.commons commons-pool2 - 2.11.1 + From d4a74c01a504df03256fa55617bcf4b752e5667b Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:42:01 +0800 Subject: [PATCH 4/6] add cache to post service --- .../yen/mdblog/service/impl/PostServiceImpl.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java index 3452f7e5a..289d616f6 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java @@ -7,6 +7,7 @@ import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service @@ -16,8 +17,11 @@ public class PostServiceImpl implements PostService { @Autowired PostMapper postMapper; @Override + @Cacheable(value = "authorId", key = "#authorId") public List getPostsById(Integer authorId) { - + // Simulate a time-consuming database operation + simulateDelay(); + System.out.println(">>> Query slow DB get post by userId"); return postMapper.findById(authorId); } @@ -51,4 +55,13 @@ public void updatePost(Post post) { // log.info(">>> updatePost : post = {}", post); postMapper.updatePost(post); } + + private void simulateDelay() { + try { + Thread.sleep(3000); // 3 seconds + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } From 71eb258d843e41d201efe8fbd46375fd5f0ec0b1 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 20 Dec 2024 16:46:26 +0800 Subject: [PATCH 5/6] update delay time --- .../main/java/com/yen/mdblog/service/impl/PostServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java index 289d616f6..bfa504153 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java @@ -58,7 +58,7 @@ public void updatePost(Post post) { private void simulateDelay() { try { - Thread.sleep(3000); // 3 seconds + Thread.sleep(10000); // 10 seconds } catch (InterruptedException e) { Thread.currentThread().interrupt(); } From bd7a469a4b4e2e7af0741a49eafbbbb044131f93 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 21 Dec 2024 17:53:27 +0800 Subject: [PATCH 6/6] add cacheService --- .../com/yen/mdblog/service/CacheService.java | 28 +++++++++++++++++ .../mdblog/service/impl/PostServiceImpl.java | 31 ++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 springBootBlog/src/main/java/com/yen/mdblog/service/CacheService.java diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/CacheService.java b/springBootBlog/src/main/java/com/yen/mdblog/service/CacheService.java new file mode 100644 index 000000000..66d11f9fb --- /dev/null +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/CacheService.java @@ -0,0 +1,28 @@ +package com.yen.mdblog.service; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +/** Cache service created by gpt */ +@Service +public class CacheService { + + /** + * Generic method to cache a result. + * + * @param cacheName the name of the cache + * @param key the cache key + * @param operation the operation to cache + * @param the type of the cached result + * @return the cached result + */ + @Cacheable(value = "#{cacheName}", key = "#{key}") + public T cacheResult(String cacheName, String key, CacheableOperation operation) { + return operation.execute(); + } + + @FunctionalInterface + public interface CacheableOperation { + T execute(); + } +} \ No newline at end of file diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java index bfa504153..c4256d03f 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/PostServiceImpl.java @@ -3,6 +3,7 @@ import com.yen.mdblog.entity.Dto.SearchRequest; import com.yen.mdblog.entity.Po.Post; import com.yen.mdblog.mapper.PostMapper; +import com.yen.mdblog.service.CacheService; import com.yen.mdblog.service.PostService; import java.util.List; import lombok.extern.slf4j.Slf4j; @@ -16,14 +17,28 @@ public class PostServiceImpl implements PostService { @Autowired PostMapper postMapper; - @Override - @Cacheable(value = "authorId", key = "#authorId") - public List getPostsById(Integer authorId) { - // Simulate a time-consuming database operation - simulateDelay(); - System.out.println(">>> Query slow DB get post by userId"); - return postMapper.findById(authorId); - } + @Autowired private CacheService cacheService; + + @Override + @Cacheable(value = "authorId", key = "#authorId") + public List getPostsById(Integer authorId) { + // Simulate a time-consuming database operation + simulateDelay(); + System.out.println(">>> Query slow DB get post by userId"); + return postMapper.findById(authorId); + } +// +// @Override +// public List getPostsById(Integer authorId) { +// return cacheService.cacheResult( +// "authorId", +// "#authorId", +// () -> { +// simulateDelay(); +// System.out.println(">>> Query slow DB get post by userId"); +// return postMapper.findById(authorId); +// }); +// } @Override public List getAllPost() {