我有一个使用休眠和 MySQL 作为数据库的 Spring Boot 应用程序。
我将Redis
用于缓存,这是我的 Spring 配置:
@SpringBootApplication
@EnableAutoConfiguration
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(AbcApplication.class, args);
}
}
我在类路径中的属性文件:
spring.cache.type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
用户型号:
@Entity
@Table(name = "USER")
public class User implements Serializable {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
和存储库接口:
public interface UserRepository extends JpaRepository<User, Long> {
}
我已将 Caching 注释放在控制器端点上:
@GetMapping("/{id}")
@Cacheable(value = "user", key = "#id")
public ResponseEntity getUserDetail(@PathVariable("id") long id) {
User user = userService.getById(id);
return ResponseEntity.ok("user id: " + user.getId());
}
但是当我调用这个端点时,抛出了这个异常:
Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool