0Pricing
Spring Boot 4 Microservices & REST APIs · درس

تجريد التخزين المؤقت في Spring

خزّن نتائج الدوال مؤقتًا باستخدام التعليقات التوضيحية

تجريد التخزين المؤقت في Spring درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Caching

Caching stores the result of expensive operations so repeated calls return instantly from memory instead of recomputing or re-querying.

  • Lower latency
  • Less database load
  • Better throughput

The Cache Abstraction

Spring provides a cache abstraction — a uniform API over many backends (in-memory, Redis, Caffeine). You annotate methods; Spring handles the storage.

Enabling Caching

Add @EnableCaching to a configuration class to turn on annotation processing.

@Configuration
@EnableCaching
public class CacheConfig {
}

The CacheManager

A CacheManager owns the named caches. Spring Boot auto-configures one based on what is on the classpath.

@Autowired
CacheManager cacheManager;

Cache productCache = cacheManager.getCache("products");

Default In-Memory Cache

With no cache library on the classpath, Spring Boot uses a simple ConcurrentMapCacheManager backed by a map.

Named Caches

Each cached method targets a named cache. Names let you tune and clear caches independently.

@Cacheable("products")
public Product findById(Long id) {
    return repository.findById(id).orElseThrow();
}

How a Cache Hit Works

On call, Spring builds a key, checks the cache: if present (a hit), it returns the stored value and skips the method body. On a miss, it runs the method and stores the result.

Cache Keys

By default the key is derived from the method arguments. You can override it with a SpEL expression.

@Cacheable(value = "products", key = "#id")
public Product findById(Long id) { ... }

Choosing a Provider

Add a library to switch backends automatically: Caffeine for fast local caching, Redis for a shared distributed cache.

// build.gradle for Caffeine
implementation "com.github.ben-manes.caffeine:caffeine"

Configuring Caffeine

Caffeine supports size limits and expiry via properties.

# application.properties
spring.cache.cache-names=products
spring.cache.caffeine.spec=maximumSize=500,expireAfterWrite=600s

When Not to Cache

Caching suits read-heavy, slow-changing data. Avoid it for rapidly changing values or when stale reads are unacceptable without proper eviction.

Quick Check

Test your understanding of the cache abstraction.

Recap

You learned the cache abstraction:

  • @EnableCaching turns the feature on
  • A CacheManager owns named caches
  • Default backend is an in-memory ConcurrentMap
  • Swap to Caffeine or Redis by adding the library

الأسئلة الشائعة

هل درس «تجريد التخزين المؤقت في Spring» مجاني؟

نعم — نص درس «تجريد التخزين المؤقت في Spring» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «تجريد التخزين المؤقت في Spring»؟

خزّن نتائج الدوال مؤقتًا باستخدام التعليقات التوضيحية تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «تجريد التخزين المؤقت في Spring»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تجريد التخزين المؤقت في Spring
  2. @Cacheable و@CachePut و@CacheEvict
  3. Redis كمزوّد للتخزين المؤقت
  4. مدة TTL وإبطال التخزين المؤقت
← العودة إلى Spring Boot 4 Microservices & REST APIs