Skip to content

Commit

Permalink
fix: handle RedisException gracefully when retrieving cached data (#1934
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cyyynthia authored Oct 5, 2023
1 parent bc92de4 commit 29bb9fc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2023 Tolgee s.r.o. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.tolgee.component

import org.redisson.client.RedisException
import org.slf4j.LoggerFactory
import org.springframework.cache.Cache
import org.springframework.cache.interceptor.SimpleCacheErrorHandler
import java.lang.RuntimeException

class TolgeeCacheErrorHandler : SimpleCacheErrorHandler() {
private val logger = LoggerFactory.getLogger(TolgeeCacheErrorHandler::class.java)

override fun handleCacheGetError(exception: RuntimeException, cache: Cache, key: Any) {
if (exception is RedisException) {
logger.warn(
"Suppressing RedisException for cache {} on key {}. " +
"This is likely due to outdated cache data, therefore this cache entry has been removed. " +
"If this re-occurs for the same cache and key, it is likely from a bug that should be reported.",
cache.name,
key,
)

logger.warn("The following error occurred while fetching the key", exception)

cache.evictIfPresent(key)
return
}

throw exception
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2023 Tolgee s.r.o. and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.tolgee.configuration

import io.tolgee.component.TolgeeCacheErrorHandler
import org.springframework.cache.annotation.CachingConfigurerSupport
import org.springframework.cache.interceptor.CacheErrorHandler
import org.springframework.context.annotation.Configuration

@Configuration
class CacheConfiguration : CachingConfigurerSupport() {
override fun errorHandler(): CacheErrorHandler {
return TolgeeCacheErrorHandler()
}
}

0 comments on commit 29bb9fc

Please sign in to comment.