Skip to content
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

Add caching to weather endpoint #2062

Merged
merged 4 commits into from
Jul 26, 2024
Merged
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
1 change: 1 addition & 0 deletions server/wfss-pointid-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
<version>1.3</version>
<scope>test</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
12 changes: 12 additions & 0 deletions server/wfss-pointid-api/wfss-pointid-api-rest-endpoints/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

<!-- Caching -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>

<!-- Logging -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ca.bc.gov.mof.wfpointid;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CachingConfig {

@Bean
public Caffeine<Object, Object> caffeineConfig() {
return Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.initialCapacity(10)
.maximumSize(500);
}
@Bean
public CacheManager cacheManager(Caffeine caffeine) {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
caffeineCacheManager.setCaffeine(caffeine);
return caffeineCacheManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

@SpringBootApplication
@EnableScheduling
@ComponentScan
public class PointIdServiceApplication {

public static final String HTTPS_PROTOCOLS = "TLSv1,TLSv1.1,TLSv1.2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -199,6 +200,7 @@ public WeatherResource getWeatherData(
@ApiResponse(code = 400, message = "Bad Request", response = Messages.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = Messages.class) })
@RequestMapping(value = "/weatherStation", method = RequestMethod.GET)
@Cacheable(value = "weatherStationData", unless = "#result == null", key = "{#code, #hourstamp, #durationHours, #durationDays, #duration}")
public WeatherResource getWeatherStationData(
@ApiParam("The station ID code.") @RequestParam("code") String code,
@ApiParam("The hour of day used to filter weather events, default now.") @RequestParam(value = "hour", required = false) String hourstamp,
Expand Down
Loading