From 99819dbcd3abca9c100d7883ed48e1a30a236d8c Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 8 Dec 2024 08:36:01 +0800 Subject: [PATCH] add dep, config, service, controller, app.properties --- projects_basic/RedisCache1/pom.xml | 22 +++++++ .../RedisCache1/RedisCache1Application.java | 2 + .../yen/RedisCache1/config/CacheConfig.java | 60 +++++++++++++++++++ .../RedisCache1/config/RedisCacheConfig.java | 27 +++++++++ .../controller/UserController.java | 21 +++++++ .../yen/RedisCache1/service/UserService.java | 24 ++++++++ .../src/main/resources/application.properties | 6 ++ 7 files changed, 162 insertions(+) create mode 100644 projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/CacheConfig.java create mode 100644 projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/RedisCacheConfig.java create mode 100644 projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/controller/UserController.java create mode 100644 projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/service/UserService.java diff --git a/projects_basic/RedisCache1/pom.xml b/projects_basic/RedisCache1/pom.xml index e99992191..a5d08b4ea 100644 --- a/projects_basic/RedisCache1/pom.xml +++ b/projects_basic/RedisCache1/pom.xml @@ -29,7 +29,9 @@ 17 + + org.springframework.boot spring-boot-starter-web @@ -40,6 +42,26 @@ spring-boot-starter-test 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/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/RedisCache1Application.java b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/RedisCache1Application.java index 82f90f854..2213c1eb5 100644 --- a/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/RedisCache1Application.java +++ b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/RedisCache1Application.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication +@EnableCaching public class RedisCache1Application { public static void main(String[] args) { diff --git a/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/CacheConfig.java b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/CacheConfig.java new file mode 100644 index 000000000..dce236679 --- /dev/null +++ b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/CacheConfig.java @@ -0,0 +1,60 @@ +//package com.yen.RedisCache1.config; +// +//import org.springframework.cache.annotation.CachingConfigurerSupport; +//import org.springframework.cache.annotation.EnableCaching; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//import javax.crypto.KeyGenerator; +//import java.lang.reflect.Method; +// +//@Configuration +//@EnableCaching +//public class CacheConfig extends CachingConfigurerSupport { +// +// @Bean +// public JedisConnectionFactory redisConnectionFactory() { +// +// return new JedisConnectionFactory(); +// } +// +// // key值命名 +// @Bean +// public KeyGenerator wiselyKeyGenerator() { +// return new KeyGenerator() { +// @Override +// public Object generate(Object target, Method method, Object... params) { +// StringBuilder sb = new StringBuilder(); +// sb.append(target.getClass().getName()); +// sb.append(method.getName()); +// for (Object obj : params) { +// sb.append(obj.toString()); +// } +// +// return sb.toString(); +// } +// }; +// } +// +// @Bean +// public RedisTemplate redisTemplate(RedisConnectionFactory factory) { +// RedisTemplate redisTemplate = new RedisTemplate(); +// redisTemplate.setConnectionFactory(factory); +// return redisTemplate; +// } +// +// @Bean +// public CacheManager cacheManager(RedisConnectionFactory factory) { +// +// RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair +// .fromSerializer(new GenericJackson2JsonRedisSerializer()); +// RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() +// .serializeValuesWith(pair) // 序列化方式 +// .entryTtl(Duration.ofHours(1)); // 過期時間 +// +// return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(factory)) +// .cacheDefaults(defaultCacheConfig).build(); +// +// } +// +//} \ No newline at end of file diff --git a/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/RedisCacheConfig.java b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/RedisCacheConfig.java new file mode 100644 index 000000000..a77615d84 --- /dev/null +++ b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/RedisCacheConfig.java @@ -0,0 +1,27 @@ +package com.yen.RedisCache1.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 diff --git a/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/controller/UserController.java b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/controller/UserController.java new file mode 100644 index 000000000..b356cc34c --- /dev/null +++ b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/controller/UserController.java @@ -0,0 +1,21 @@ +package com.yen.RedisCache1.controller; + +import com.yen.RedisCache1.service.UserService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class UserController { + + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + @GetMapping("/user/{id}") + public String getUser(@PathVariable String id) { + return userService.getUserById(id); + } +} \ No newline at end of file diff --git a/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/service/UserService.java b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/service/UserService.java new file mode 100644 index 000000000..0cf6b9f47 --- /dev/null +++ b/projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/service/UserService.java @@ -0,0 +1,24 @@ +package com.yen.RedisCache1.service; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + @Cacheable(value = "users", key = "#id") + public String getUserById(String id) { + // Simulate a time-consuming database operation + simulateDelay(); + System.out.println(">>> Query DB get user info"); + return "User" + id; + } + + private void simulateDelay() { + try { + Thread.sleep(3000); // 3 seconds + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/projects_basic/RedisCache1/src/main/resources/application.properties b/projects_basic/RedisCache1/src/main/resources/application.properties index 7b041dac9..37791e76d 100644 --- a/projects_basic/RedisCache1/src/main/resources/application.properties +++ b/projects_basic/RedisCache1/src/main/resources/application.properties @@ -1 +1,7 @@ spring.application.name=RedisCache1 + +# redis +spring.cache.type=redis +spring.redis.host=localhost +spring.redis.port=6379 +spring.redis.timeout=60000 \ No newline at end of file