An interactive web application that visualizes the Least Recently Used (LRU) Cache data structure with a beautiful, modern UI. This project provides both a visual learning tool and a production-ready LRU Cache implementation in TypeScript.
- Interactive Visualization: Watch the LRU Cache in action with real-time visual feedback
- Production-Ready Implementation: Efficient O(1) time complexity for both get and put operations
- Modern UI: Beautiful interface with dark mode support
- Educational: Perfect for learning how LRU Cache works
- TypeScript: Fully typed for better developer experience
- Comprehensive Testing: Well-tested with high code coverage
A Least Recently Used (LRU) Cache is a data structure that stores a limited number of items and automatically evicts the least recently used item when the cache reaches its capacity. It's widely used in:
- Operating Systems: Page replacement algorithms
- Web Browsers: Browser cache management
- Databases: Query result caching
- CDNs: Content delivery optimization
- APIs: Response caching
- Get Operation: Retrieve a value by key and mark it as recently used
- Put Operation: Insert or update a key-value pair
- Eviction: When capacity is exceeded, remove the least recently used item
| Operation | Time Complexity | Space Complexity |
|---|---|---|
get(key) |
O(1) | O(capacity) |
put(key, value) |
O(1) | O(capacity) |
This implementation uses:
- Circular Doubly-Linked List: For maintaining access order
- HashMap: For O(1) key lookups
- Head Pointer: Points to the most recently used item
- Node.js 18+ and npm/yarn/pnpm
# Clone the repository
git clone https://github.com/Akshayy67/LRU-Cache.git
cd LRU-Cache
# Install dependencies
npm install
# Run the development server
npm run devVisit http://localhost:5173 to see the visualizer in action!
npm run build
npm run preview- Set Capacity: Choose the maximum number of items (1-10)
- Put Operation: Enter a key and value, then click "Put"
- Get Operation: Enter a key and click "Get"
- Clear Cache: Reset the cache to start fresh
- View Logs: See a timestamped history of all operations
import { LRUCache } from './src/utils/LRUCache';
// Create a cache with capacity 3
const cache = new LRUCache(3);
// Add items
cache.put(1, 100);
cache.put(2, 200);
cache.put(3, 300);
// Get an item (returns the value and marks it as recently used)
const value = cache.get(1); // Returns 100
// Add another item (will evict the least recently used)
cache.put(4, 400); // Evicts key 2 (least recently used)
// Try to get evicted item
cache.get(2); // Returns -1 (not found)
// Get current state
const state = cache.getState();
console.log(state); // [{ key: 4, value: 400 }, { key: 1, value: 100 }, { key: 3, value: 300 }]// Create a cache for storing user sessions
const sessionCache = new LRUCache(100);
// Store session data
sessionCache.put(userId, sessionData);
// Retrieve session (marks as recently used)
const session = sessionCache.get(userId);
if (session === -1) {
// Session expired or not found
createNewSession(userId);
} else {
// Session found and refreshed
continueSession(session);
}constructor(capacity: number)Creates a new LRU Cache with the specified capacity.
Parameters:
capacity(number): Maximum number of items the cache can hold. Must be a positive integer.
Throws:
Error: If capacity is not a positive integer.
Example:
const cache = new LRUCache(5);Retrieves the value associated with the key. If the key exists, it's marked as recently used.
Parameters:
key(number): The key to look up
Returns:
number: The value associated with the key, or -1 if not found
Time Complexity: O(1)
Example:
const value = cache.get(1); // Returns value or -1Inserts or updates a key-value pair. If the cache is at capacity, the least recently used item is evicted.
Parameters:
key(number): The key to insert/updatevalue(number): The value to store
Returns: void
Time Complexity: O(1)
Example:
cache.put(1, 100);
cache.put(1, 200); // Updates existing keyReturns the current state of the cache as an array, ordered from most recently used to least recently used.
Returns:
Array<{ key: number; value: number }>: Array of key-value pairs
Time Complexity: O(n) where n is the number of items in cache
Example:
const state = cache.getState();
// [{ key: 3, value: 300 }, { key: 1, value: 100 }]Run the test suite:
npm testRun tests with coverage:
npm run test:coverageLRU-Cache/
βββ src/
β βββ components/
β β βββ Description.tsx # Algorithm explanation component
β β βββ Visualizer.tsx # Interactive visualizer component
β βββ utils/
β β βββ LRUCache.ts # Core LRU Cache implementation
β βββ App.tsx # Main application component
β βββ main.tsx # Application entry point
β βββ index.css # Global styles
βββ examples/
β βββ basic-usage.ts # Basic usage examples
β βββ node-example.js # Node.js usage example
β βββ performance.ts # Performance benchmarks
βββ tests/
β βββ LRUCache.test.ts # Unit tests
βββ public/ # Static assets
βββ package.json
βββ tsconfig.json
βββ vite.config.ts
βββ README.md
- TypeScript: Type-safe JavaScript
- React: UI library
- Vite: Build tool and dev server
- TailwindCSS: Utility-first CSS framework
- Lucide React: Beautiful icons
- Vitest: Unit testing framework
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Akshay Juluri
- GitHub: @Akshayy67
- Email: akshayjuluri6704@gmail.com
- Inspired by the need for better data structure visualization tools
- Built with modern web technologies
- Thanks to the open-source community
None at the moment. Please report any issues you find!
- Add support for generic key-value types
- Implement TTL (Time To Live) for cache entries
- Add export/import cache state functionality
- Create mobile-responsive design improvements
- Add animation speed controls
- Support for different eviction policies (LFU, FIFO, etc.)
Made with β€οΈ by Akshay Juluri
