-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dep, config, service, controller, app.properties
- Loading branch information
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/CacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> redisTemplate(RedisConnectionFactory factory) { | ||
// RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); | ||
// redisTemplate.setConnectionFactory(factory); | ||
// return redisTemplate; | ||
// } | ||
// | ||
// @Bean | ||
// public CacheManager cacheManager(RedisConnectionFactory factory) { | ||
// | ||
// RedisSerializationContext.SerializationPair<Object> 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(); | ||
// | ||
// } | ||
// | ||
//} |
27 changes: 27 additions & 0 deletions
27
projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/config/RedisCacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { | ||
RedisTemplate<Object, Object> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(connectionFactory); | ||
return template; | ||
} | ||
|
||
@Bean | ||
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { | ||
return RedisCacheManager.builder(connectionFactory).build(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
projects_basic/RedisCache1/src/main/java/com/yen/RedisCache1/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
projects_basic/RedisCache1/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
spring.application.name=RedisCache1 | ||
|
||
# redis | ||
spring.cache.type=redis | ||
spring.redis.host=localhost | ||
spring.redis.port=6379 | ||
spring.redis.timeout=60000 |