Repository navigation
Fix/docker demo stability #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
802fe34
docs: activate launch preparation tasks
meilingrl 0fa5d6d
feat: establish launch preparation wave one
meilingrl 902770e
merge: launch preparation wave one
meilingrl cc6cde4
feat(backend): add optional Redis caches
meilingrl 7bcd7d5
chore(compose): add Redis runtime wiring
meilingrl 115430f
fix(frontend): stabilize local demo product views
meilingrl ff52401
fix(logistics): show Amap address markers locally
meilingrl a19e4e5
docs: record Redis launch rehearsal guidance
meilingrl 7be260b
Add Docker demo expansion seed
meilingrl a1bf912
Stabilize Docker uploads and seed startup
meilingrl 5bfbb8d
Improve frontend media resilience
meilingrl 67b5735
Adjust shop detail and avatar role access
meilingrl fc3330c
Document Docker demo fixes
meilingrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
103 changes: 103 additions & 0 deletions
103
backend/src/main/java/com/youyu/backend/common/cache/RedisCacheSupport.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.youyu.backend.common.cache; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.youyu.backend.config.RedisCacheProperties; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class RedisCacheSupport { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(RedisCacheSupport.class); | ||
| private static final TypeReference<List<Map<String, Object>>> LIST_OF_MAPS = new TypeReference<>() { | ||
| }; | ||
|
|
||
| private final RedisCacheProperties properties; | ||
| private final StringRedisTemplate redisTemplate; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| public RedisCacheSupport(RedisCacheProperties properties, | ||
| StringRedisTemplate redisTemplate, | ||
| ObjectMapper objectMapper) { | ||
| this.properties = properties; | ||
| this.redisTemplate = redisTemplate; | ||
| this.objectMapper = objectMapper; | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return properties.isEnabled(); | ||
| } | ||
|
|
||
| public Optional<List<Map<String, Object>>> getList(String key) { | ||
| if (!isEnabled()) { | ||
| return Optional.empty(); | ||
| } | ||
| String namespacedKey = namespacedKey(key); | ||
| try { | ||
| String value = redisTemplate.opsForValue().get(namespacedKey); | ||
| if (value == null || value.isBlank()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(objectMapper.readValue(value, LIST_OF_MAPS)); | ||
| } catch (RuntimeException ex) { | ||
| log.warn("Redis cache read failed", ex); | ||
| return Optional.empty(); | ||
| } catch (Exception ex) { | ||
| log.warn("Redis cache payload is unreadable", ex); | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| public void putList(String key, List<Map<String, Object>> value, Duration ttl) { | ||
| if (!isEnabled() || value == null || ttl == null || ttl.isZero() || ttl.isNegative()) { | ||
| return; | ||
| } | ||
| String namespacedKey = namespacedKey(key); | ||
| try { | ||
| redisTemplate.opsForValue().set(namespacedKey, objectMapper.writeValueAsString(value), ttl); | ||
| } catch (RuntimeException ex) { | ||
| log.warn("Redis cache write failed", ex); | ||
| } catch (Exception ex) { | ||
| log.warn("Redis cache payload could not be serialized", ex); | ||
| } | ||
| } | ||
|
|
||
| public void evict(String key) { | ||
| if (!isEnabled()) { | ||
| return; | ||
| } | ||
| String namespacedKey = namespacedKey(key); | ||
| try { | ||
| redisTemplate.delete(namespacedKey); | ||
| } catch (RuntimeException ex) { | ||
| log.warn("Redis cache eviction failed", ex); | ||
| } | ||
| } | ||
|
|
||
| public void evictByPrefix(String prefix) { | ||
| if (!isEnabled()) { | ||
| return; | ||
| } | ||
| String namespacedPrefix = namespacedKey(prefix); | ||
| try { | ||
| Set<String> keys = redisTemplate.keys(namespacedPrefix + "*"); | ||
| if (keys != null && !keys.isEmpty()) { | ||
| redisTemplate.delete(keys); | ||
| } | ||
| } catch (RuntimeException ex) { | ||
| log.warn("Redis cache prefix eviction failed for prefix={}", namespacedPrefix, ex); | ||
| } | ||
| } | ||
|
|
||
| private String namespacedKey(String key) { | ||
| return properties.getKeyPrefix() + key; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
redisTemplate.keys()is a blocking O(N) operation that scans the entire keyspace of the Redis database. In production or high-traffic environments, this can block the single-threaded Redis event loop, leading to latency spikes, timeouts, and potential service degradation. Since this method is triggered on frequent operations like order completion, product updates, and review submissions, it should be refactored to use a non-blockingSCANcursor.