A lightning-fast โก caching implementation demo using Spring Boot and Caffeine Cache to supercharge your application performance! ๐ฏ
This project demonstrates a powerful caching system with a product management example, featuring:
- ๐พ In-memory product storage with simulated slow service
- โ Caffeine cache integration with Spring Boot
- ๐ Real-time cache statistics monitoring
- ๐ RESTful API endpoints for product operations
- โ 100% test coverage
- ๐ Interactive shell script for cache behavior demonstration
- โ Java 21
- ๐ Spring Boot 3.2.0
- โก Caffeine Cache 3.1.8
- ๐๏ธ Lombok
- ๐จ Maven
- ๐งช JUnit 5 with AssertJ
Supercharge your application with these cache settings! ๐ฅ
Caffeine.newBuilder()
.initialCapacity(100) // ๐ฑ Initial cache size
.maximumSize(500) // ๐ Maximum cache entries
.expireAfterWrite(5, TimeUnit.MINUTES) // โฐ Entry expiration time
.recordStats() // ๐ Enable statistics| Method | Endpoint | Description |
|---|---|---|
| GET | /api/products/{id} |
๐ Retrieve a product |
| PUT | /api/products |
๐ Update a product |
| DELETE | /api/products/{id} |
๐๏ธ Delete a product |
| DELETE | /api/products/cache |
๐งน Clear the cache |
| GET | /api/products/cache/stats |
๐ Get cache statistics |
Smart caching with Spring annotations:
- ๐ฅ
@Cacheable: Caches method results - ๐ค
@CachePut: Updates cache entries - ๐๏ธ
@CacheEvict: Removes entries from cache - โจ
@EnableCaching: Enables Spring's caching magic
Monitor your cache like a pro:
- ๐ฏ Hit/miss counts
- ๐ Hit rate
- ๐ Eviction count
- ๐ฆ Cache size
Watch the caching magic happen with a simulated delay:
private void simulateSlowService() {
Thread.sleep(3000L); // โณ 3-second delay
}We take testing seriously! ๐ฏ
- ๐ฌ Unit tests for all components
- ๐ฏ Cache behavior verification
- ๐ก๏ธ Edge case handling
- ๐ JaCoCo test coverage reporting (80%+ coverage)
Test drive the cache with our shell script! ๐
./test-cache.sh [iterations]Watch it perform: ๐ฌ
- ๐ Initial cache stats check
- ๐ First product retrieval (uncached)
- โก Second product retrieval (cached)
- ๐ Product update
- ๐งน Cache clearing
- ๐ Post-clear retrieval
- โ Java 21 or higher
- ๐จ Maven 3.8.0 or higher
mvn clean installmvn spring-boot:runApplication launches on port 8989 ๐
Quality is our priority! ๐ช
- ๐ Google Java Format enforcement
- ๐ Maven Enforcer Plugin
- ๐ JavaDoc documentation
- ๐ JaCoCo code coverage
- โจ Consistent formatting
Experience these amazing performance gains:
- ๐ Reduced database load
- โก Lightning-fast response times
- ๐ Smart cache eviction
- โ๏ธ Flexible configuration
-
๐ฏ Cache Configuration
- ๐ฆ Optimal cache sizes
- โฐ Smart expiration policies
- ๐ Performance monitoring
-
๐ก๏ธ Error Handling
- ๐ Exception management
- ๐งต Thread handling
- โ Null safety
-
๐งช Testing
- ๐ฏ Complete coverage
- ๐ฌ Edge cases
- ๐ Performance metrics
-
๐ Code Organization
- ๐๏ธ Clean architecture
- ๐ Separation of concerns
- ๐ Dependency injection