-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3d28fe
commit 17ce6cb
Showing
11 changed files
with
100 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 26 additions & 16 deletions
42
src/main/kotlin/com/group4/ticketingservice/config/CacheConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,43 @@ | ||
package com.group4.ticketingservice.config | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import org.springframework.cache.CacheManager | ||
import org.springframework.cache.annotation.CacheEvict | ||
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 | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration | ||
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer | ||
import org.springframework.data.redis.serializer.RedisSerializationContext | ||
import java.time.Duration | ||
|
||
@Configuration | ||
@EnableCaching | ||
class CacheConfig { | ||
class CacheConfig( | ||
private val redisConnectionFactory: RedisConnectionFactory | ||
) { | ||
companion object { | ||
private const val EVENT_CACHE = "getEvents" | ||
} | ||
|
||
@Bean | ||
fun cacheManager(): CacheManager { | ||
val simpleCacheManager = SimpleCacheManager() | ||
simpleCacheManager.setCaches( | ||
listOf( | ||
ConcurrentMapCache(EVENT_CACHE) | ||
) | ||
) | ||
return simpleCacheManager | ||
} | ||
val objectMapper = ObjectMapper() | ||
.findAndRegisterModules() | ||
.enable(SerializationFeature.INDENT_OUTPUT) | ||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.registerModules(JavaTimeModule()) | ||
|
||
@CacheEvict(allEntries = true, value = [EVENT_CACHE]) | ||
@Scheduled(fixedDelay = 10 * 60 * 1000, initialDelay = 500) | ||
fun cacheEvict() {} | ||
val builder = RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory) | ||
val configuration: RedisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer(objectMapper))) | ||
.entryTtl(Duration.ofMinutes(30)) | ||
builder.cacheDefaults(configuration) | ||
return builder.build() | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/group4/ticketingservice/config/RedisConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.group4.ticketingservice.config | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||
|
||
@Configuration | ||
class RedisConfig { | ||
@Value("\${spring.data.redis.host}") | ||
private val host: String? = null | ||
|
||
@Value("\${spring.data.redis.port}") | ||
private val port = 0 | ||
|
||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
return LettuceConnectionFactory( | ||
RedisStandaloneConfiguration(host!!, port) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters