Skip to content

redis integration #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:11-ea-17-jre-slim
FROM openjdk:17-jdk-slim
LABEL maintainer="author@javatodev.com"
VOLUME /main-app
ADD build/libs/device-data-api-mysql-spring-boot-0.0.1-SNAPSHOT.jar app.jar
Expand Down
11 changes: 11 additions & 0 deletions api_request.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GET http://localhost:8080/api/v1/devices

### CREATE DEVICE DATA
POST http://localhost:8080/api/v1/devices
Content-Type: application/json

{
"device_model_id": "NOKIA-1100",
"device_model": "1100",
"serial_number": "A-439048209"
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@EnableCaching
@SpringBootApplication
public class DeviceDataApiMysqlSpringBootApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.javatodev.app.dto.DeviceDataDto;
import com.javatodev.app.service.DeviceDataService;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/javatodev/app/dto/DeviceDataDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.javatodev.app.dto;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -9,7 +11,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class DeviceDataDto {
public class DeviceDataDto implements Serializable {
private String device_model_id;
private String device_model;
private String serial_number;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/javatodev/app/entity/DeviceDataEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.javatodev.app.entity;

import org.springframework.data.redis.core.RedisHash;

import java.io.Serializable;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -12,7 +16,7 @@
@Table(name = "device_data")
@Getter
@Setter
public class DeviceDataEntity {
public class DeviceDataEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import com.javatodev.app.mapper.DeviceDataMapper;
import com.javatodev.app.repository.DeviceDataRepository;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

import lombok.RequiredArgsConstructor;
Expand All @@ -21,11 +24,13 @@ public class DeviceDataService {

DeviceDataMapper deviceDataMapper = new DeviceDataMapper();

@Cacheable(value = "DeviceData")
public List<DeviceDataDto> readDeviceData() {
List<DeviceDataEntity> deviceData = deviceDataRepository.findAll();
return deviceDataMapper.convertToDtoList(deviceData);
}

// @CachePut(value = "DeviceData", key = "#dataDto.device_model_id")
public void createDeviceData(DeviceDataDto dataDto) {
DeviceDataEntity deviceDataEntity = deviceDataRepository.save(deviceDataMapper.convertToEntity(dataDto));
log.info("Device data created {}", deviceDataEntity.getId());
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ spring.datasource.url=jdbc:mysql://127.0.0.1:3306/device-data-api-db
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
# Redis Config
spring.cache.type=redis
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
spring.main.allow-bean-definition-overriding=true