Skip to content

Commit

Permalink
Feat/137 event caching (#140)
Browse files Browse the repository at this point in the history
* feat: enable cache when get events

* test: check cache exist

* docs: update open-api.yaml

---------

Co-authored-by: Git Actions <no-reply@github.com>
  • Loading branch information
minjun3021 and Git Actions authored Nov 12, 2023
1 parent 38304fe commit 293ca91
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-jdbc")
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("com.ninja-squad:springmockk:4.0.2")
Expand Down
4 changes: 2 additions & 2 deletions docs/open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,12 @@ components:
type: string
isEnabled:
type: boolean
username:
type: string
authorities:
type: array
items:
$ref: '#/components/schemas/GrantedAuthority'
username:
type: string
isAccountNonExpired:
type: boolean
isAccountNonLocked:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.group4.ticketingservice.Event

import com.group4.ticketingservice.AbstractIntegrationTest
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.cache.CacheManager
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders

@AutoConfigureMockMvc
class EventControllerTest : AbstractIntegrationTest() {
@Autowired
private lateinit var mockMvc: MockMvc

@Autowired
private lateinit var cacheManager: CacheManager

@Test
fun `getEvents caches the result`() {
mockMvc.perform(
MockMvcRequestBuilders.get("/events")
.param("page", "0")
.param("size", "10")

)

val cache = cacheManager.getCache("getEvents")
val cachedValue = cache!!.get(0)

assert(cachedValue != null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package com.group4.ticketingservice

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching

@SpringBootApplication
@EnableCaching
class TicketingserviceApplication

fun main(args: Array<String>) {
runApplication<TicketingserviceApplication>(*args)
}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/group4/ticketingservice/config/CacheConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.group4.ticketingservice.config

import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.concurrent.ConcurrentMapCache
import org.springframework.cache.support.SimpleCacheManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableCaching
class CacheConfig {
@Bean
fun cacheManager(): CacheManager {
val simpleCacheManager = SimpleCacheManager()
simpleCacheManager.setCaches(
listOf(
ConcurrentMapCache("getEvents")
)
)
return simpleCacheManager
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.swagger.v3.oas.annotations.Hidden
import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.Cacheable
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
Expand Down Expand Up @@ -82,6 +83,7 @@ class EventController @Autowired constructor(
}

@GetMapping
@Cacheable(value = ["getEvents"], key = "#pageable.pageNumber")
fun getEvents(
request: HttpServletRequest,
@RequestParam(required = false) name: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.cache.CacheManager
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.FilterType
import org.springframework.data.domain.Page
Expand All @@ -34,11 +35,12 @@ import java.time.OffsetDateTime
@WebMvcTest(
EventController::class,
includeFilters = arrayOf(
ComponentScan.Filter(value = [(SecurityConfig::class), (TokenProvider::class), (JwtAuthorizationEntryPoint::class)], type = FilterType.ASSIGNABLE_TYPE)
ComponentScan.Filter(value = [(SecurityConfig::class), (TokenProvider::class), (JwtAuthorizationEntryPoint::class), (CacheManager::class)], type = FilterType.ASSIGNABLE_TYPE)
)
)
class EventControllerTest(
@Autowired val mockMvc: MockMvc
@Autowired val mockMvc: MockMvc,
@Autowired val cacheManager: CacheManager
) {
@MockkBean
private lateinit var eventService: EventService
Expand Down Expand Up @@ -178,6 +180,7 @@ class EventControllerTest(
@Test
fun `GET List of events should return list of events with pagination and sorting`() {
// Given

every { eventService.getEvents(any(), any()) } returns page

// When
Expand Down

0 comments on commit 293ca91

Please sign in to comment.