redis-cache-patterns
Skill1 starsChanged 45 days agoMITView on GitHub
Mô hình thiết kế và tối ưu bộ nhớ đệm Redis Cache tốc độ cao cho hệ thống Realtime. Kích hoạt skill này khi xây dựng các cơ chế Token/Session Cache, Idempotency Keys (TTL 24h), Redis Pub/Sub channels, tối ưu Zero Connection Leak và độ phức tạp O(1)/O(N).
1 other project uses a version of this.
Adopted by
Most recently taken up by
- salmandev/everything-geminitheir own version, first seen Oct 17, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- salmandev/everything-gemini★ 2same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository83 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- adopt-repoSkill
- agent-strict-guardSkill
- analyze-featureSkill
- backend-api-design-flowSkill
- clean-code-naming-conventionsSkill
- code-review-gateSkill
- dangerous-action-blockerSkill
- database-transaction-managementSkill
- db-interface-procedure-flowSkill
- db-migrationSkill
- defensive-troubleshooting-guideSkill
- git-workflowSkill
- human-authority-overrideSkill
- human-master-mapSkill
- implement-featureSkill
- jpa-n-plus-one-optimizationSkill
- knowledge-graphSkill
- multi-language-error-handlingSkill
- post-code-verificationSkill
- project-doctorSkill
- project-governance-dashboardSkill
- release-notesSkill
- safe-git-rebaseSkill
- security-secret-scannerSkill
- speckit-analyzeSkill
- speckit-auditSkill
- speckit-checklistSkill
- speckit-clarifySkill
- speckit-constitutionSkill
- speckit-helpSkill
- speckit-implementSkill
- speckit-ingestSkill
- speckit-planSkill
- speckit-specifySkill
- speckit-tasksSkill
- spec-writerSkill
- spring-middleware-pipelineSkill
- sql-repository-patternSkill
- system-dna-managerSkill
- test-featureSkill
- adopt-repoSkill
- agent-strict-guardSkill
- analyze-featureSkill
- backend-api-design-flowSkill
- clean-code-naming-conventionsSkill
- code-review-gateSkill
- dangerous-action-blockerSkill
- database-transaction-managementSkill
- db-interface-procedure-flowSkill
- db-migrationSkill
- defensive-troubleshooting-guideSkill
- git-workflowSkill
- human-authority-overrideSkill
- human-master-mapSkill
- implement-featureSkill
- jpa-n-plus-one-optimizationSkill
- knowledge-graphSkill
- multi-language-error-handlingSkill
- The first 60 shown.
The file
Files
<!-- GENERATED FROM registry/skills/redis-cache-patterns/SKILL.md — DO NOT EDIT DIRECTLY --><!-- GENERATED FROM .shared/skills/02-codestyle/redis-cache-patterns/SKILL.md — DO NOT EDIT DIRECTLY -->Kỹ Thuật Tối Ưu Redis Cache & Pub/Sub Đồng Bộ Thời Gian Thực
Skill này cung cấp các nguyên tắc kiến trúc và mẫu thiết kế chuẩn mực khi làm việc với cụm Redis Cache / Redis Cluster trong các hệ thống Backend Microservices nhằm đảm bảo tốc độ phản hồi tính bằng mili-giây (< 5ms) và an toàn giao dịch.
1. 🔌 Nguyên Tắc Tối Ưu Kết Nối: Zero Connection Leak
- Vấn đề: Khởi tạo kết nối Redis client mới (
new Jedis()hoặcLettuceConnectionFactory.create()) trong mỗi request của Service sẽ nhanh chóng làm cạn kiệt socket pool, gây nghẽn kết nối và sập Server. - Giải pháp: Tái sử dụng Bean
RedisTemplate<String, Object>hoặcStringRedisTemplatedạng Singleton do Spring IoC Container quản lý.
@Service
@Slf4j
class IdempotencyService {
@Autowired
private StringRedisTemplate redisTemplate
/**
* Kiểm tra và khóa requestId trong 24h (Idempotency Key)
* Trả về true nếu là request đầu tiên, false nếu bị duplicate
*/
boolean lockRequestId(String requestId, String partnerCode) {
String key = "idempotency:${partnerCode}:${requestId}"
Boolean isNew = redisTemplate.opsForValue().setIfAbsent(key, "PROCESSING", Duration.ofHours(24))
return Boolean.TRUE.equals(isNew)
}
}2. ⚡ Chiến Lược Truy Vấn: $O(1)$ vs $O(N)$
Tận dụng cấu trúc dữ liệu Redis Hash & String Key-Value:
| Thao tác | Cú pháp Spring Redis | Độ phức tạp | Mục đích sử dụng |
| Lấy 1 key đơn | redisTemplate.opsForValue().get(key) | $O(1)$ | Kiểm tra Idempotency Key, Token session |
| Lấy 1 field trong Hash | redisTemplate.opsForHash().get(key, field) | $O(1)$ | Lấy thông tin cấu hình của 1 đối tác / tenant cụ thể |
| Lấy toàn bộ Hash | redisTemplate.opsForHash().entries(key) | $O(N)$ | Lấy toàn bộ danh mục khi khởi động |
| Lệnh CẤM trên Production | KEYS * / FLUSHALL | $O(Total)$ | Tuyệt đối CẤM vì Redis đơn luồng, sẽ gây đóng băng toàn bộ cụm cluster |
3. 📡 Cơ Chế Đồng Bộ Cấu Hình Qua Redis Pub/Sub
Nhằm thỏa mãn nguyên tắc cô lập cấu hình và đồng bộ realtime:
sequenceDiagram
autonumber
actor Admin as Admin System
participant Redis as Redis Cluster (system_channel)
participant Svc1 as Service Instance 1
participant Svc2 as Service Instance 2
Admin->>Redis: PUBLISH system_channel '{"event":"RELOAD_CACHE"}'
Redis-->>Svc1: onMessage(event)
Redis-->>Svc2: onMessage(event)
Svc1->>Svc1: cacheService.reload() nạp lại RAM
Svc2->>Svc2: cacheService.reload() nạp lại RAMCấu hình RedisMessageListenerContainer:
@Configuration
class RedisPubSubConfig {
@Bean
RedisMessageListenerContainer redisContainer(
RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer()
container.setConnectionFactory(connectionFactory)
container.addMessageListener(listenerAdapter, new ChannelTopic("system_channel"))
return container
}
@Bean
MessageListenerAdapter listenerAdapter(LangService langService) {
return new MessageListenerAdapter(new MessageListener() {
@Override
void onMessage(Message message, byte[] pattern) {
String body = new String(message.getBody())
if (body.contains("RELOAD_LANG")) {
langService.reload()
}
}
})
}
}4. ✅ Checklist Tự Kiểm Tra (Redis Cache Checklist)
- Singleton Connection: Sử dụng
StringRedisTemplateđược Spring quản lý, không tạo connection thủ công. - Cài đặt TTL: 100% các key tạm thời (Idempotency, Session, Rate limit) đều có TTL xác định (ví dụ:
24hhoặc604800s). - Đặt Prefix Key: Toàn bộ key tuân theo định dạng có namespace:
service:idempotency:{partnerCode}:{requestId}. - **Không gọi KEYS ***: Dùng
SCANhoặc lưu index vàoSetthay vì tìm kiếm wildcards trên production.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

