Skip to content

Commit

Permalink
add dep, config, service, controller, app.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 8, 2024
1 parent 090ec41 commit 99819db
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
22 changes: 22 additions & 0 deletions projects_basic/RedisCache1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
<properties>
<java.version>17</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -40,6 +42,26 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.0.11</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
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();
//
// }
//
//}
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();
}
}
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);
}
}
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();
}
}
}
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

0 comments on commit 99819db

Please sign in to comment.