Skip to content
barryo edited this page Jun 20, 2012 · 2 revisions

The OSS_SNMP always uses some form of caching.

Unless you specify otherwise, \OSS_SNMP\Cache\Basic is used which is just an associative array of requested OIDs and the values returned.

This is very important because of the way OSS_SNMP is designed.

Let's take an example of getting the port name and administrative status for a device:

1. $snmpHost = new \OSS_SNMP\SNMP( $ip, $community );
2. 
3. foreach( $snmpHost->useIface()->names as $portId => $portName )
4.    echo $portName . ": " . $snmpHost->useIface()->adminStates()[$portId] . "\n";

Without caching, line 4 would generate a new SNMP walk everytime it was executed. But, as the result is cached, the above code only performs two SNMP walks.

I have chosen walking for queries like this rather than getting as I believe it is the better use case and the alternative would required more convoluted code and also two SNMP get queries for each port.

Note that the default Basic cache only caches for the duration of execution for that single script / call. The array is destroyed on completion of the script. This is fine for most use cases but we have also created an APC cache (see below) for longer term queries and it should be trivial to add Memcached or other caches.

Enabled and Disabling the Cache

If you need to ensure you have fresh data, you can disable the cache and reenable it as follows:

$snmpHost->disableCache();
$names = $snmpHost->useIface()->names as $portId => $portName );
$snmpHost->enableCache();

Note that the result of the SNMP query is still cached but the cache is not used to get the information - when the cache is disabled, SNMP queries are always performed.

Cache Functions

All caches extend the abstract \OSS_SNMP\Cache which requires them to implement a load() and save() method but also a clear() and a clearAll() method.

APC Cache

If you'd prefer to have a longer term cache, you can use the APC cache as follows:

$snmpHost = new \OSS_SNMP\SNMP( $ip, $community );
$snmpHost->setCache( new \OSS_SNMP\Cache\APC( $ttl, $prefix ) );

where $ttl is the default time to live of cached data (defaults to 300 seconds) and $prefix is the variable name prefix added to stored values in the cache (defaults to OSS_SNMP_).

Clone this wiki locally