Redis كمزوّد للتخزين المؤقت
اضبط التخزين المؤقت المدعوم من Redis
Redis كمزوّد للتخزين المؤقت درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Redis for Caching
Redis is an in-memory data store that works as a distributed cache shared across many app instances.
- Survives app restarts
- Shared by all nodes
- Supports TTL and eviction
Adding the Dependency
Add Spring Data Redis to switch the cache backend to Redis.
// build.gradle
implementation "org.springframework.boot:spring-boot-starter-data-redis"Connection Settings
Point Spring at your Redis server in properties.
# application.properties
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.cache.type=redisAuto-Configured RedisCacheManager
With Redis on the classpath and spring.cache.type=redis, Spring Boot auto-configures a RedisCacheManager. Your existing @Cacheable methods now use Redis with no code change.
Customizing the CacheManager
Define a RedisCacheManager bean for fine control over defaults.
@Bean
public RedisCacheManager cacheManager(
RedisConnectionFactory factory) {
RedisCacheConfiguration config =
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10));
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
}Serialization
Cached values are serialized before storage. JDK serialization is the default; JSON is more portable and readable.
RedisCacheConfiguration config =
RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(
SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()));Serializable Entities
For JDK serialization, cached objects must implement Serializable. JSON serialization avoids this requirement.
public class Product implements Serializable {
private Long id;
private String name;
}Key Prefixes
Redis cache keys are prefixed with the cache name by default (e.g. products::1), keeping caches namespaced in the shared store.
Per-Cache Configuration
Give individual caches their own TTL via a map of configurations.
Map<String, RedisCacheConfiguration> configs =
Map.of("products",
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5)));
return RedisCacheManager.builder(factory)
.withInitialCacheConfigurations(configs)
.build();RedisTemplate for Direct Access
Beyond the cache abstraction, RedisTemplate lets you read and write Redis directly.
@Autowired
RedisTemplate<String, String> redisTemplate;
redisTemplate.opsForValue()
.set("greeting", "hello",
Duration.ofSeconds(30));Resilience to Redis Outages
If Redis is unreachable, cache operations fail. Consider a fallback strategy or error handler so a cache outage does not take down the app.
Quick Check
Test your understanding of Redis caching.
Recap
You learned to use Redis as a cache:
- Add the data-redis starter, set
spring.cache.type=redis - Auto-configured
RedisCacheManager - Customize TTL and JSON serialization via a bean
- Keys are namespaced by cache name
الأسئلة الشائعة
هل درس «Redis كمزوّد للتخزين المؤقت» مجاني؟
نعم — نص درس «Redis كمزوّد للتخزين المؤقت» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
ماذا ستتعلم في «Redis كمزوّد للتخزين المؤقت»؟
اضبط التخزين المؤقت المدعوم من Redis تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟
لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «Redis كمزوّد للتخزين المؤقت»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟
نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تجريد التخزين المؤقت في Spring
- @Cacheable و@CachePut و@CacheEvict
- Redis كمزوّد للتخزين المؤقت
- مدة TTL وإبطال التخزين المؤقت