This file contains hidden or 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
| local members = redis.call('smembers', KEYS[1]) | |
| for mem =1,#members do | |
| if redis.call('EXISTS', KEYS[2]..':'..members[mem]) ~= 1 then | |
| redis.call('SREM', KEYS[1], members[mem]) | |
| end | |
| end | |
| return redis.call('smembers', KEYS[1]) |
This file contains hidden or 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
| private val processingSomethingJobKey = "application:job:processingSomething" | |
| @Scheduled(cron = "0 0 0/1 1/1 * ?") | |
| fun processingSomething() { | |
| val canRun = redisTemplate.boundValueOps(processingSomethingJobKey) | |
| .setIfAbsent(UUID.randomUUID().toString(), 5L, TimeUnit.MINUTES) | |
| if (canRun != null && canRun) { | |
| log.info("Lock adquired. Running something in this instance") | |
| } else { | |
| log.info("Lock unavailable! Job is running in other instance") |
This file contains hidden or 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
| fun findByApplicationId(applicationId: String): Set<InstanceDefinition> { | |
| val instances = applicationRepository.findInstancesByApplicationId(applicationId) | |
| return instances.mapNotNull { instance -> | |
| getInstance(applicationId, instance) | |
| }.toSet() | |
| } | |
| private fun getInstance(applicationId: String, instance: String): InstanceDefinition? { | |
| val instanceDefinition = instanceRepository.getInstance(applicationId, instance) | |
| if (instanceDefinition == null) { |
This file contains hidden or 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
| @Configuration | |
| class RedisConfiguration { | |
| @Bean | |
| fun redisTemplate( | |
| connectionFactory: RedisConnectionFactory, | |
| objectMapper: ObjectMapper | |
| ): RedisTemplate<String, InstanceDefinition> { | |
| val template = RedisTemplate<String, InstanceDefinition>() |
This file contains hidden or 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
| @Repository | |
| class InstanceRepository(private val redisTemplate: RedisTemplate<String, InstanceDefinition>) { | |
| private val instancePrefixKey = "morethancache:instance" | |
| fun saveInstance( | |
| instanceDefinition: InstanceDefinition, | |
| instanceTtlInMinutes: Long | |
| ) { | |
| val key = "$instancePrefixKey:${instanceDefinition.application}:${instanceDefinition.uuid}" |
This file contains hidden or 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
| package br.com.devcave.redis.repository | |
| import org.springframework.data.redis.core.StringRedisTemplate | |
| import org.springframework.stereotype.Repository | |
| @Repository | |
| class ApplicationRepository(private val redisTemplate: StringRedisTemplate) { | |
| private val applicationPrefixKey = "morethancache:application" |
This file contains hidden or 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
| class CustomRedisCacheWriter( | |
| private val connectionFactory: RedisConnectionFactory, | |
| private val duration: Duration | |
| ) : RedisCacheWriter { | |
| private val log = LoggerFactory.getLogger(javaClass) | |
| // Other methods | |
| override fun get(name: String, key: ByteArray): ByteArray? { |
This file contains hidden or 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
| return RedisCacheManager.RedisCacheManagerBuilder.fromCacheWriter(lockingRedisCacheWriter) | |
| // .cacheDefaults(cache1MinuteConfiguration) // Uncomment the line for default config | |
| // .disableCreateOnMissingCache() // Uncomment the line for avoid unexpected caches | |
| .withCacheConfiguration("findAll", cache1MinuteConfiguration) | |
| .withCacheConfiguration("count", cache1MinuteConfiguration) | |
| .withCacheConfiguration("findById", cache60MinuteConfiguration) | |
| .build() | |
| } |
This file contains hidden or 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
| @Configuration | |
| class CacheConfiguration( | |
| private val connectionFactory: RedisConnectionFactory, | |
| private val objectMapper: ObjectMapper, | |
| @Value("\${spring.application.name}") | |
| private val applicationName: String | |
| ) { | |
| @Bean | |
| fun cacheManager(): CacheManager { |
This file contains hidden or 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
| @Cacheable("findById") | |
| fun findById(id: Long): Employee? { | |
| // some code | |
| } | |
| @Cacheable("findAll") | |
| fun findAll(): List<Employee> { | |
| // some code | |
| } |