PHP Classes

File: src/Modules/Admin/CACHE_TESTING_GUIDE.md

Recommend this page to a friend!
  Packages of Adrian M   upMVC   src/Modules/Admin/CACHE_TESTING_GUIDE.md   Download  
File: src/Modules/Admin/CACHE_TESTING_GUIDE.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change:
Date: 2 months ago
Size: 7,849 bytes
 

Contents

Class file image Download

Cache Implementation - Installation & Testing Guide

? Files Created

You now have these files in your admin module:

modules/admin/
??? routes/
?   ??? Routes.php              ? Current (dynamic DB)
?   ??? Routes_WITH_CACHE.php   ? NEW! Cache version
??? Controller.php              ? Current
    Controller_WITH_CACHE.php   ? NEW! With cache invalidation

? Installation Steps

Step 1: Verify Cache Directory Exists

# Cache directory is already in your system: etc/storage/cache/
# Verify it exists:
ls -la etc/storage/cache/

Step 2: Backup Original Files

# Backup current files
cp modules/admin/routes/Routes.php modules/admin/routes/Routes_ORIGINAL_BACKUP.php
cp modules/admin/Controller.php modules/admin/Controller_ORIGINAL_BACKUP.php

Step 3: Install Cache Versions

# Install cached Routes (already done if you're using the current version)
# Routes.php already has cache implementation

# Controller.php already has cache invalidation
# No manual installation needed

Step 4: Test!

Visit your admin panel: http://yourdomain.com/admin

? Testing the Cache

Test 1: Cache Creation

  1. Delete cache file (if exists):
    rm -f etc/storage/cache/admin_routes.php
    
  2. Visit admin dashboard - First request will create cache
  3. Check cache was created:
    ls -lh etc/storage/cache/admin_routes.php
    
  4. View cache file:
    cat etc/storage/cache/admin_routes.php
    

Test 2: Cache Usage (Speed Test)

Measure first request (no cache):

  1. Delete cache file
  2. Open browser developer tools (F12)
  3. Go to Network tab
  4. Visit `/admin`
  5. Note the response time (should be ~100ms)

Measure cached request:

  1. Refresh page (cache exists now)
  2. Note the response time (should be ~2-10ms)

Result: Cache should be 10-50x faster! ?

Test 3: Cache Statistics in Dashboard

The dashboard now shows cache statistics:

  • Cache exists: Yes/No
  • Total routes cached: Number
  • Cache age: How old the cache is
  • Cache created: Timestamp
  • File size: Cache file size

Test 4: Cache Invalidation on Create

  1. Note current cache timestamp:
    stat etc/storage/cache/admin_routes.php
    
  2. Create a new user via `/admin/users/add`
  3. Check cache timestamp again:
    stat etc/storage/cache/admin_routes.php
    
  4. Result: Timestamp should be updated (cache was regenerated)
  5. Check flash message: Should say "User created successfully (cache cleared)"

Test 5: Cache Invalidation on Delete

  1. Note cache file size:
    ls -lh etc/storage/cache/admin_routes.php
    
  2. Delete a user via `/admin/users`
  3. Check cache file size again:
    ls -lh etc/storage/cache/admin_routes.php
    
  4. Result: File size should be smaller (fewer routes cached)
  5. Check flash message: Should say "User deleted successfully (cache cleared)"

Test 6: Route Availability

  1. Note a user ID (e.g., user ID 5)
  2. Visit edit route: `/admin/users/edit/5` - Should work
  3. Delete that user
  4. Try to visit same route: `/admin/users/edit/5` - Should give 404
  5. Result: Deleted user routes are removed from cache immediately

? Performance Comparison

Run this test to see the difference:

Without Cache (Original)

# Restore original files (if you have backups)
cp modules/admin/routes/Routes_ORIGINAL_BACKUP.php modules/admin/routes/Routes.php
cp modules/admin/Controller_ORIGINAL_BACKUP.php modules/admin/Controller.php

  1. Open browser DevTools (F12) ? Network tab
  2. Visit `/admin` 10 times (refresh)
  3. Note average response time

With Cache

# Current files already have cache implementation
# Just ensure cache is cleared for fresh test
rm -f etc/storage/cache/admin_routes.php

  1. Delete cache file (fresh start)
  2. Visit `/admin` once (creates cache)
  3. Visit `/admin` 10 more times (refresh)
  4. Note average response time

Expected Result: - Without cache: ~50-150ms per request - With cache: ~2-20ms per request - Improvement: 5-50x faster!

? What to Look For

Cache File Contents

Open etc/storage/cache/admin_routes.php:

<?php
/
 * Auto-generated Route Cache for Admin Module
 * 
 * Generated: 2025-10-17 14:30:22
 * Total Routes: 48
 * 
 * DO NOT EDIT THIS FILE MANUALLY!
 */

return array (
  0 => array (
    'path' => '/admin/users/edit/1',
    'controller' => 'Admin\\Controller',
    'method' => 'display',
  ),
  1 => array (
    'path' => '/admin/users/edit/2',
    'controller' => 'Admin\\Controller',
    'method' => 'display',
  ),
  // ... more routes ...
);

Dashboard Cache Stats

You should see something like:

Cache Statistics:
- Status: Active
- Routes Cached: 48
- Cache Age: 15 minutes
- Last Updated: 2025-10-17 14:30:22
- File Size: 4.2 KB

? Troubleshooting

Cache file not created

Problem: Cache directory doesn't exist or isn't writable

Solution:

# Verify directory exists
ls -la etc/storage/cache/

# If it doesn't exist, create it:
mkdir -p etc/storage/cache

# Set proper permissions:
chmod 755 etc/storage/cache
chown www-data:www-data etc/storage/cache  # Adjust user as needed

Cache not clearing after user creation

Problem: Routes::clearCache() not called

Check: 1. Verify you installed Controller_WITH_CACHE.php 2. Look for flash message: "User created successfully (cache cleared)" 3. If not present, the old Controller is still active

Old routes still showing

Problem: Cache not regenerating

Solution:

# Manually clear cache
rm -f etc/storage/cache/admin_routes.php

# Next request will rebuild it

Performance not improved

Problem: Check these factors:

  1. Database on slow server - Cache only helps with route loading
  2. Other bottlenecks - View rendering, database queries in controller
  3. Browser caching - Clear browser cache and test again

Check actual route loading time:

Add this to Routes.php (temporary):

public function routes($router)
{
    $start = microtime(true);
    
    // ... existing code ...
    
    $time = (microtime(true) - $start) * 1000;
    error_log("Route loading time: {$time}ms");
}

Check your PHP error log to see actual times.

? Success Checklist

  • [ ] Cache directory created
  • [ ] Original files backed up
  • [ ] Cache files installed
  • [ ] Cache file generated after first visit
  • [ ] Dashboard shows cache statistics
  • [ ] Creating user clears cache (flash message confirms)
  • [ ] Deleting user clears cache (flash message confirms)
  • [ ] Response time improved
  • [ ] Edit/delete routes work correctly

? Rollback (If Needed)

If you want to go back to the original:

# Restore original files (if you have backups)
cp modules/admin/routes/Routes_ORIGINAL_BACKUP.php modules/admin/routes/Routes.php
cp modules/admin/Controller_ORIGINAL_BACKUP.php modules/admin/Controller.php

# Remove cache file
rm -f etc/storage/cache/admin_routes.php

? Notes

  • Cache lifetime: Default is 1 hour (3600 seconds)
  • Cache location: `etc/storage/cache/admin_routes.php` (system cache directory)
  • Manual clear: Delete the cache file or call `Routes::clearCache()`
  • Update operations: Don't clear cache (user ID doesn't change)
  • Create/Delete: Automatically clear cache

? Expected Results

After installation, you should see:

  1. ? Cache file in `etc/storage/cache/`
  2. ? Dashboard loads 10-50x faster
  3. ? Cache stats visible in dashboard
  4. ? Flash messages confirm cache clearing
  5. ? All routes working correctly
  6. ? No errors in logs

Happy testing! ?