عرض عملي: ذاكرة Caffeine مؤقتة محلية مع مقاييس
في هذا الدرس 3 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
add a Caffeine cache to a Shop endpoint and read its stats. Prepares MID-037-LAB1.
تفاصيل المخطط المبدئي
خطوة بخطوة
- Add the Caffeine dependency and build a
Cache<Long, ProductDto>withmaximumSize(1000),expireAfterWrite(5, MINUTES), andrecordStats(). - Wrap
findProduct(id):cache.get(id, this::loadFromDb). - Expose
cache.stats()through Micrometer (hit count, miss count, load time). - Generate load with a small script; show the hit ratio rising after warm-up.
- Planned mistake: cache the per-customer discounted price by product id only; a second customer sees the first customer's price. Fix: cache the shared base price and apply the discount afterwards, or include the customer tier in the key.
- Verify Caffeine version and Micrometer binding names before recording.
اختبر فهمك
Why is recordStats() needed?
أظهر الإجابة
Without it the cache reports no hit or miss counts.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.