Skip to content

abhijithshetty12/Smart-Cache-Analyzer

Repository files navigation

⚡Smart Cache Analyzer

An interactive web-based cache algorithm simulator and visualizer that demonstrates the performance differences between LRU (Least Recently Used) and LFU (Least Frequently Used) cache replacement policies.

Cache Python Flask

📸 Screenshots

Dashboard Overview Interactive dashboard showing real-time cache simulation with live statistics

Algorithm Comparison Side-by-side comparison of LRU and LFU algorithm performance

🚀 Features

Interactive Dashboard

  • Real-time Cache Simulation: Watch cache operations unfold step by step
  • Live Statistics: Monitor hit ratio, bandwidth savings, and load times in real-time
  • Visual Cache State: See which pages are cached with color-coded indicators
  • Access Log Timeline: Track request history with hit/miss highlighting

Algorithm Comparison

  • Side-by-side Analysis: Compare LRU vs LFU performance metrics
  • Performance Charts: Interactive graphs showing hit ratio trends
  • Detailed Statistics: Comprehensive performance breakdown for each algorithm

User Experience

  • Modern UI: Dark theme with responsive design
  • Step-by-step Control: Manual stepping or auto-play mode
  • Configurable Cache Size: Adjustable cache capacity (1-10 MB)
  • Keyboard Shortcuts: Spacebar for step forward

🎯 What This Project Does

This project simulates a web browser cache system that stores frequently accessed pages to reduce bandwidth usage and improve load times. It demonstrates how different cache replacement algorithms perform under the same access patterns.

Cache Algorithms Implemented

  1. LRU (Least Recently Used)

    • Evicts the page that hasn't been accessed for the longest time
    • Better for scenarios with temporal locality
    • Uses OrderedDict for efficient tracking
  2. LFU (Least Frequently Used)

    • Evicts the page with the lowest access frequency
    • Better for scenarios with frequently accessed content
    • Tracks access counts for each cached page

📊 Performance Metrics

The simulator tracks and visualizes:

  • Cache Hit Ratio: Percentage of requests served from cache
  • Bandwidth Saved: Total data (KB) not re-downloaded due to caching
  • Average Load Time: Mean page load time with caching benefits
  • Request Progress: Real-time tracking of simulation steps

🛠️ Technology Stack

Backend

  • Python 3.8+: Core programming language
  • Flask 3.0+: Web framework for API and serving the dashboard
  • Flask-CORS: Cross-origin resource sharing support

Frontend

  • HTML5: Semantic markup
  • CSS3: Modern styling with CSS Grid, Flexbox, and custom properties
  • JavaScript (ES6+): Interactive functionality
  • Chart.js: Data visualization library

🚦 Getting Started

Prerequisites

  • Python 3.8 or higher
  • pip (Python package manager)
  • Modern web browser (Chrome, Firefox, Edge, Safari)

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/SmartCacheOptimizer.git
    cd SmartCacheOptimizer
  2. Install dependencies

    pip install -r requirements.txt
  3. Run the application

    python app.py
  4. Open in browser

    http://127.0.0.1:5000
    

📖 Usage Guide

Running a Simulation

  1. Select Algorithm: Choose LRU or LFU from the dropdown
  2. Set Cache Size: Use the slider to adjust cache capacity (1-10 MB)
  3. Run Simulation: Click the "Run Simulation" button
  4. Step Through: Use "Step Forward" or "Auto Play" to watch the simulation
  5. View Statistics: Watch the real-time metrics update

Comparing Algorithms

  1. Click the "Compare LRU vs LFU" button
  2. View the side-by-side comparison modal
  3. Analyze the performance charts
  4. Close the modal to continue

Keyboard Shortcuts

  • Spacebar: Step forward in simulation (when active)
  • Escape: Close modal dialogs

📁 Project Structure

SmartCacheOptimizer/
├── app.py                    # Flask web application
├── lru_cache.py              # LRU cache implementation
├── lfu_cache.py              # LFU cache implementation
├── requirements.txt          # Python dependencies
├── README.md                 # This file
├── templates/
│   └── index.html           # Main dashboard HTML
├── static/
│   ├── style.css            # CSS styling
│   └── app.js               # JavaScript functionality
└── data/
    └── sample_access_log.csv # Sample access log data

🔬 Understanding the Algorithms

LRU (Least Recently Used)

How it works:

  • Tracks the order of page accesses
  • When cache is full, removes the least recently accessed page
  • Uses an ordered dictionary to maintain access order

Best for:

  • Workloads with temporal locality
  • Scenarios where recent access patterns predict future access
  • Web browsing with sequential page navigation

Example:

Cache: [A, B, C] (capacity = 3)
Access D → Evict A (least recent) → [B, C, D]
Access B → Move B to end → [C, D, B]

LFU (Least Frequently Used)

How it works:

  • Counts how many times each page is accessed
  • When cache is full, removes the page with lowest access count
  • Maintains a frequency dictionary alongside the cache

Best for:

  • Workloads with popular/frequently accessed content
  • Scenarios where some content is accessed much more than others
  • Content delivery with "hot" items

Example:

Cache: [A(freq:1), B(freq:3), C(freq:2)]
Access D → Evict A (lowest frequency) → [B(freq:3), C(freq:2), D(freq:1)]

📊 Sample Data

The project includes a sample access log (data/sample_access_log.csv) with realistic web page access patterns:

  • Pages: HTML pages, images, videos
  • Sizes: Range from 100 KB to 2000 KB
  • Load Times: 60-800 ms
  • Access Patterns: Mix of frequently and rarely accessed content

🎨 Features in Detail

Cache State Visualization

  • Green Border: Recently accessed page (cache hit)
  • Orange Border: New page being added (cache miss)
  • Frequency Display: Shows access count for LFU algorithm

Real-time Charts

  • Hit Ratio Over Time: Line chart showing how hit ratio evolves
  • Performance Comparison: Bar chart comparing hits vs misses

Responsive Design

  • Works on desktop, tablet, and mobile devices
  • Adapts layout based on screen size
  • Touch-friendly controls

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

🙏 Acknowledgments

  • Chart.js for the excellent charting library
  • Flask for the lightweight web framework
  • Inter Font for beautiful typography

Made with ❤️ using Python and Flask

⭐ Star this repo if you find it helpful!

About

A performance‑focused tool for analyzing caching strategies and memory usage. SmartCacheAnalyzer helps evaluate cache efficiency, detect bottlenecks, and optimize resource management with clear insights. Built using modern development practices to support learning and experimentation in system optimization.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors