This project implements a distributed in-memory cache system using Java and Spring Boot. It features an LRU eviction policy, thread-safe operations, a simple REST API for cache operations, and basic clustering support with consistent hashing.
- LRU Eviction Policy: Automatically removes the least recently used entries when the cache reaches its limit.
- Thread-Safe Operations: Utilizes
ConcurrentHashMapto ensure safe access to cache entries in a multi-threaded environment. - REST API: Provides endpoints for creating, retrieving, and deleting cache entries.
- Clustering Support: Implements consistent hashing to distribute cache entries across multiple nodes.
- Java
- Spring Boot
- ConcurrentHashMap
- REST API
distributed-cache-system
βββ src
β βββ main
β β βββ java
β β β βββ com
β β β βββ example
β β β βββ cache
β β β β βββ CacheApplication.java
β β β β βββ controller
β β β β β βββ CacheController.java
β β β β βββ service
β β β β β βββ CacheService.java
β β β β βββ model
β β β β β βββ CacheEntry.java
β β β β βββ clustering
β β β β βββ ConsistentHashing.java
β β βββ resources
β β β βββ application.properties
β β β βββ static
β β β βββ dashboard.html
β βββ test
β β βββ java
β β βββ com
β β βββ example
β β βββ cache
β β βββ CacheServiceTest.java
βββ pom.xml
βββ README.md
-
Clone the repository:
git clone https://github.com/yourusername/distributed-cache-system.git cd distributed-cache-system -
Build the project using Maven:
mvn clean install -
Run the application:
mvn spring-boot:run -
Access the REST API at
http://localhost:8080/api/cache.
-
Create Cache Entry
- Endpoint:
POST /api/cache - Request Body:
{ "key": "string", "value": "string" } - Response:
201 Created
- Endpoint:
-
Get Cache Entry
- Endpoint:
GET /api/cache/{key} - Response:
200 OKwith cache entry details or404 Not Found
- Endpoint:
-
Delete Cache Entry
- Endpoint:
DELETE /api/cache/{key} - Response:
204 No Content
- Endpoint:
Unit tests for the CacheService class are located in src/test/java/com/example/cache/CacheServiceTest.java. Run the tests using:
mvn test
A simple HTML dashboard is provided to monitor cache statistics. Access it at http://localhost:8080/dashboard.html.
This project is licensed under the MIT License. See the LICENSE file for details.