A high-performance caching service with intelligent cache invalidation strategies
Features โข Quick Start โข API Documentation โข Architecture โข Contributing
The Cache Invalidation Service is a robust Spring Boot application designed to optimize data retrieval performance through intelligent caching mechanisms. It provides seamless cache management with automatic invalidation strategies to ensure data consistency and optimal performance.
Perfect for: Microservices architecture, high-traffic applications, and systems requiring real-time data consistency with caching benefits.
|
|
|
|
Component | Technology | Version |
---|---|---|
Language | 17+ | |
Framework | 3.x | |
Web Layer | Latest | |
Caching | Built-in | |
Build Tool | 3.6+ |
graph TB
A[Client Request] --> B[Controller Layer]
B --> C{Cache Check}
C -->|Cache Hit| D[Return Cached Data]
C -->|Cache Miss| E[Service Layer]
E --> F[Database Query]
F --> G[Update Cache]
G --> H[Return Fresh Data]
I[Update Request] --> J[Controller Layer]
J --> K[Service Layer]
K --> L[Update Database]
L --> M[Invalidate Cache]
M --> N[Return Success]
style C fill:#e1f5fe
style G fill:#c8e6c9
style M fill:#ffcdd2
src/main/java/com/cache/service/
โ
โโโ ๐ฎ controller/
โ โโโ DataController.java # REST API endpoints
โ
โโโ ๐ง service/
โ โโโ DataService.java # Business logic layer
โ โโโ CacheService.java # Cache management
โ
โโโ ๐ฆ dto/
โ โโโ DataRequest.java # Request payload
โ โโโ DataResponse.java # Response payload
โ
โโโ ๐๏ธ entity/
โ โโโ DataEntity.java # Data model
โ
โโโ ๐ repository/
โ โโโ DataRepository.java # Data access layer
โ
โโโ โ๏ธ config/
โโโ CacheConfig.java # Cache configuration
๐ GET /data/{id} - Retrieve Data
Retrieves data by ID with intelligent caching.
Request:
GET /data/123
Accept: application/json
Response:
{
"id": 123,
"data": "Sample data content",
"lastModified": "2024-01-15T10:30:00Z",
"cached": true,
"cacheHitTime": "1ms"
}
Response Codes:
200 OK
- Data retrieved successfully404 NOT FOUND
- Data not found500 INTERNAL SERVER ERROR
- Server error
๐ POST /data/{id} - Update Data
Updates data and invalidates cache for consistency.
Request:
POST /data/123
Content-Type: application/json
{
"newData": "Updated content with new information"
}
Response:
{
"id": 123,
"data": "Updated content with new information",
"lastModified": "2024-01-15T11:45:00Z",
"cacheInvalidated": true,
"updateTime": "5ms"
}
Response Codes:
200 OK
- Data updated successfully400 BAD REQUEST
- Invalid request payload404 NOT FOUND
- Data not found500 INTERNAL SERVER ERROR
- Server error
๐ GET /cache/stats - Cache Statistics
Retrieves cache performance statistics.
Response:
{
"totalRequests": 1542,
"cacheHits": 1234,
"cacheMisses": 308,
"hitRatio": 0.8,
"cacheSize": 150,
"averageResponseTime": "2.5ms"
}
โ Java 17+ |
๐ฆ Maven 3.6+ Download from Apache Maven |
๐ป IDE |
git clone https://github.com/khan-sk-dev/CacheInvalidationService.git
cd CacheInvalidationService
# Clean and compile
mvn clean compile
# Run tests
mvn test
# Package the application
mvn clean install
# Option 1: Using Maven
mvn spring-boot:run
# Option 2: Using JAR file
java -jar target/cache-invalidation-service-1.0.0.jar
# Option 3: With custom profile
mvn spring-boot:run -Dspring-boot.run.profiles=dev
# Check application health
curl http://localhost:8080/actuator/health
# Test the API
curl http://localhost:8080/data/1
๐ Application is now running at: http://localhost:8080
# First request (cache miss)
curl -X GET http://localhost:8080/data/42 \
-H "Accept: application/json"
# Subsequent requests (cache hit)
curl -X GET http://localhost:8080/data/42 \
-H "Accept: application/json"
curl -X POST http://localhost:8080/data/42 \
-H "Content-Type: application/json" \
-d '{
"newData": "This is updated content that will invalidate the cache"
}'
# Get cache statistics
curl -X GET http://localhost:8080/cache/stats \
-H "Accept: application/json"
# Server Configuration
server.port=8080
server.servlet.context-path=/
# Cache Configuration
spring.cache.type=simple
spring.cache.cache-names=dataCache
spring.cache.caffeine.spec=maximumSize=1000,expireAfterWrite=30m
# Logging Configuration
logging.level.com.cache.service=INFO
logging.level.org.springframework.cache=DEBUG
# Management Endpoints
management.endpoints.web.exposure.include=health,info,metrics,cache
management.endpoint.health.show-details=always
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("dataCache");
cacheManager.setCaffeine(
Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(30, TimeUnit.MINUTES)
.recordStats()
);
return cacheManager;
}
}
Metric | Target | Actual |
---|---|---|
Cache Hit Ratio | > 80% | 85% |
Average Response Time | < 5ms | 2.5ms |
Peak Throughput | 1000 req/sec | 1250 req/sec |
Memory Usage | < 512MB | 256MB |
P95 Response Time | < 10ms | 7ms |
# Unit tests
mvn test
# Integration tests
mvn verify
# Test with coverage
mvn test jacoco:report
# View coverage report
open target/site/jacoco/index.html
# Static analysis with SpotBugs
mvn spotbugs:check
# Code formatting with Spotless
mvn spotless:apply
# Dependency security check
mvn dependency-check:check
- Application Health:
GET /actuator/health
- Cache Metrics:
GET /actuator/metrics/cache.gets
- Memory Usage:
GET /actuator/metrics/jvm.memory.used
# Add to application.yml for Prometheus integration
management:
endpoints:
web:
exposure:
include: prometheus
metrics:
export:
prometheus:
enabled: true
FROM openjdk:17-jre-slim
WORKDIR /app
COPY target/cache-invalidation-service-*.jar app.jar
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/actuator/health || exit 1
ENTRYPOINT ["java", "-jar", "app.jar"]
version: '3.8'
services:
cache-service:
build: .
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=docker
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
We welcome contributions! Please check out our contribution guidelines.
- ๐ด Fork the repository
- ๐ Create a feature branch:
git checkout -b feature/amazing-feature
- ๐ป Develop your feature with tests
- โ
Test your changes:
mvn test
- ๐ Commit your changes:
git commit -m 'Add amazing feature'
- ๐ Push to branch:
git push origin feature/amazing-feature
- ๐ Create a Pull Request
- Follow Google Java Style Guide
- Write comprehensive tests (minimum 80% coverage)
- Update documentation for new features
- Ensure all CI checks pass
- Redis Integration - Distributed caching support
- Cache Warming - Proactive cache population
- Advanced Metrics - Detailed performance analytics
- Health Dashboard - Real-time monitoring UI
- Multi-level Caching - L1/L2 cache hierarchy
- Cache Partitioning - Horizontal cache scaling
- AI-Powered Invalidation - Smart cache eviction
- GraphQL Support - Modern API interface
- ๐ API Documentation
- ๐๏ธ Architecture Guide
- ๐ง Configuration Reference
- ๐ Deployment Guide
- ๐ Input Validation - Comprehensive request validation
- ๐ก๏ธ Rate Limiting - Protection against abuse
- ๐ Audit Logging - Complete operation tracking
- ๐ Security Headers - CORS and security policies
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 Cache Invalidation Service Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction...
- Spring Boot team for the excellent framework
- Caffeine cache for high-performance caching
- Open source community for continuous inspiration