This Java caching framework is designed to provide a robust and scalable caching mechanism, suitable for various use cases, including multi-threaded environments. The framework features a flexible and generic API, allowing developers to easily integrate caching into their Java applications. It includes a Least Recently Used (LRU) cache implementation and adheres to SOLID design principles and common design patterns such as Singleton and Factory Method.
- Generic Cache Interface: Defines basic operations like
put
,get
,remove
, andclear
. - LRU Cache Implementation: Automatically evicts the least recently used entries when the cache reaches its capacity.
- Thread-Safe Operations: The cache is designed to handle concurrent access in multi-threaded environments.
- Singleton Cache Manager: Manages the lifecycle of cache instances, ensuring that each cache is unique and easily retrievable.
- Extensibility: The framework can be extended with custom cache implementations or eviction policies.
Add the following dependency to your pom.xml
:
<dependency>
<groupId>com.example</groupId>
<artifactId>cache-framework</artifactId>
<version>1.0.0</version>
</dependency>
If you have the JAR file, add it to your project's libs directory and include it in your build configuration:
dependencies {
implementation files('libs/cache-framework-1.0.0.jar')
}
- Initialize the Cache Manager
The CacheManager is a singleton class that manages all caches:
CacheManager cacheManager = CacheManager.getInstance();
- Create a Cache
Create a new cache with a specified capacity:
Cache<String, String> cache = cacheManager.createCache("myCache", 100);
- Put Data into the Cache
Add data to the cache:
cache.put("key1", "value1"); cache.put("key2", "value2");
- Retrieve Data from the Cache
Retrieve data from the cache:
CacheEntity<String, String> entity = cache.get("key1"); if (entity != null) { System.out.println("Retrieved value: " + entity.getValue()); // Outputs: value1 } else { System.out.println("Key not found in cache."); }
- Remove Data from the Cache
Remove a specific entry from the cache:
cache.remove("key1");
- Clear the Cache
Clear all entries from the cache:
cache.clear();
- Multi-Threaded Access
The cache framework is thread-safe, so you can safely access it from multiple threads:
Runnable cacheTask = () -> { cache.put("threadKey", "threadValue"); CacheEntity<String, String> entity = cache.get("threadKey"); if (entity != null) { System.out.println(Thread.currentThread().getName() + " retrieved: " + entity.getValue()); } }; Thread thread1 = new Thread(cacheTask); Thread thread2 = new Thread(cacheTask); thread1.start(); thread2.start(); thread1.join(); thread2.join();
The CacheManager class is implemented as a singleton, ensuring that only one instance of the cache manager exists throughout the application.
public static CacheManager getInstance() {
return Holder.INSTANCE;
}
The CacheManager class uses the Factory Method pattern to create cache instances. The createCache method encapsulates the object creation process.
public <K, V> Cache<K, V> createCache(String cacheName, int size) {
Cache<K, V> cache = new LRUCache<>(size);
caches.put(cacheName, cache);
return cache;
}
Contributions are welcome! Please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License - see the LICENSE file for details.