Skip to content

Caching

ZILLEALI edited this page May 20, 2026 · 1 revision

Caching

Reduce RouterOS API load using CachingProxy.


Overview

MikroTik routers have limited CPU. Polling them too frequently causes performance issues. CachingProxy wraps any manager with a TTL cache — subsequent calls return cached data.

Cache invalidation: Write operations (create, update, delete) automatically flush the cache for that manager.


Basic Usage

// Wrap manager with cache — TTL in seconds
$cached = MikroTik::withCache(MikroTik::pppoe(), ttl: 30);

// First call — fetches from router
$sessions = $cached->getActiveSessions();

// Within 30 seconds — returns cached result
$sessions = $cached->getActiveSessions();

// Write operation — auto-invalidates cache
$cached->kickSession('user1'); // cache cleared

// Next read — fetches fresh from router
$sessions = $cached->getActiveSessions();

Recommended TTL Values

Data Type Recommended TTL Reason
Active sessions 30s Changes frequently
System resources 60s CPU/RAM polling
Interface status 60s Rarely changes
PPPoE secrets 300s Config data
Hotspot users 300s Config data
DHCP leases 60s Lease changes
Firewall rules 600s Config data
Router users 600s Rarely changes

Multiple Managers with Cache

$pppoeCache    = MikroTik::withCache(MikroTik::pppoe(), ttl: 30);
$systemCache   = MikroTik::withCache(MikroTik::system(), ttl: 60);
$hotspotCache  = MikroTik::withCache(MikroTik::hotspot(), ttl: 30);
$firewallCache = MikroTik::withCache(MikroTik::firewall(), ttl: 300);

// NOC dashboard — all cached
$sessions  = $pppoeCache->getActiveSessions();
$cpu       = $systemCache->getCpuLoad();
$hosts     = $hotspotCache->getActiveHosts();
$rules     = $firewallCache->getFilterRules();

Service Provider Binding

Register cached managers in AppServiceProvider:

// app/Providers/AppServiceProvider.php
public function register(): void
{
    $this->app->singleton('mikrotik.pppoe', function () {
        return MikroTik::withCache(MikroTik::pppoe(), ttl: 30);
    });

    $this->app->singleton('mikrotik.system', function () {
        return MikroTik::withCache(MikroTik::system(), ttl: 60);
    });
}

// Usage
$pppoe = app('mikrotik.pppoe');
$sessions = $pppoe->getActiveSessions();

SSL Setup | Rate Limiting →


📝 Found an error or missing info?
Edit this page or open an issue to suggest improvements.

Clone this wiki locally