---
name: "netsnmp_cache_handler"
section: "3snmp"
platform: "linux"
package: "libsnmp-dev"
version: "5.4.3~dfsg-2.8+deb7u1"
release: "Debian 7 (Wheezy)"
platforms: ["linux"]
url: "https://new.carta.tech/netsnmp_cache_handler"
---

# netsnmp\_cache\_handler(3snmp)

Maintains a cache of data for use by lower level handlers.

## NAME

cache\_handler -

Maintains a cache of data for use by lower level handlers.

### Functions

**netsnmp\_cache** \* **netsnmp\_cache\_get\_head** (void)\
 *get cache head*  **netsnmp\_cache** \* **netsnmp\_cache\_find\_by\_oid** (oid \*rootoid, int rootoid\_len)\
 *find existing cache*  **netsnmp\_cache** \* **netsnmp\_cache\_create** (int timeout, NetsnmpCacheLoad \*load\_hook, NetsnmpCacheFree \*free\_hook, oid \*rootoid, int rootoid\_len)\
 *returns a cache*  unsigned int **netsnmp\_cache\_timer\_start** (**netsnmp\_cache** \*cache)\
 *starts the recurring cache\_load callback*  void **netsnmp\_cache\_timer\_stop** (**netsnmp\_cache** \*cache)\
 *stops the recurring cache\_load callback*  **netsnmp\_mib\_handler** \* **netsnmp\_cache\_handler\_get** (**netsnmp\_cache** \*cache)\
 *returns a cache handler that can be injected into a given handler chain.*  **netsnmp\_mib\_handler** \* **netsnmp\_get\_cache\_handler** (int timeout, NetsnmpCacheLoad \*load\_hook, NetsnmpCacheFree \*free\_hook, oid \*rootoid, int rootoid\_len)\
 *returns a cache handler that can be injected into a given handler chain.*  int **netsnmp\_cache\_handler\_register** (**netsnmp\_handler\_registration** \*reginfo, **netsnmp\_cache** \*cache)\
 *functionally the same as calling* **netsnmp\_register\_handler()** *but also injects a cache handler at the same time for you.*  int **netsnmp\_register\_cache\_handler** (**netsnmp\_handler\_registration** \*reginfo, int timeout, NetsnmpCacheLoad \*load\_hook, NetsnmpCacheFree \*free\_hook)\
 *functionally the same as calling* **netsnmp\_register\_handler()** *but also injects a cache handler at the same time for you.*  NETSNMP\_STATIC\_INLINE char \* **\_build\_cache\_name** (const char \*name)\
 void **netsnmp\_cache\_reqinfo\_insert** (**netsnmp\_cache** \*cache, **netsnmp\_agent\_request\_info** \*reqinfo, const char \*name)\
 *Insert the cache information for a given request (PDU).*  **netsnmp\_cache** \* **netsnmp\_cache\_reqinfo\_extract** (**netsnmp\_agent\_request\_info** \*reqinfo, const char \*name)\
 *Extract the cache information for a given request (PDU).*  **netsnmp\_cache** \* **netsnmp\_extract\_cache\_info** (**netsnmp\_agent\_request\_info** \*reqinfo)\
 *Extract the cache information for a given request (PDU).*  int **netsnmp\_cache\_check\_expired** (**netsnmp\_cache** \*cache)\
 *Check if the cache timeout has passed.*  int **netsnmp\_cache\_check\_and\_reload** (**netsnmp\_cache** \*cache)\
 *Reload the cache if required.*  int **netsnmp\_cache\_is\_valid** (**netsnmp\_agent\_request\_info** \*reqinfo, const char \*name)\
 *Is the cache valid for a given request?*  int **netsnmp\_is\_cache\_valid** (**netsnmp\_agent\_request\_info** \*reqinfo)\
 *Is the cache valid for a given request? for backwards compatability.*  int **netsnmp\_cache\_helper\_handler** (**netsnmp\_mib\_handler** \*handler, **netsnmp\_handler\_registration** \*reginfo, **netsnmp\_agent\_request\_info** \*reqinfo, **netsnmp\_request\_info** \*requests)\
 *Implements the cache handler.*  void **release\_cached\_resources** (unsigned int regNo, void \*clientargs)\
 *run regularly to automatically release cached resources.*

## Detailed Description

Maintains a cache of data for use by lower level handlers.

This helper checks to see whether the data has been loaded 'recently' (according to the timeout for that particular cache) and calls the registered 'load\_cache' routine if necessary. The lower handlers can then work with this local cached data.

A timeout value of -1 will cause **netsnmp\_cache\_check\_expired()** to always return true, and thus the cache will be reloaded for every request.

To minimze resource use by the agent, a periodic callback checks for expired caches, and will call the free\_cache function for any expired cache.

The load\_cache route should return a negative number if the cache was not successfully loaded. 0 or any positive number indicates successs.

Several flags can be set to affect the operations on the cache.

If NETSNMP\_CACHE\_DONT\_INVALIDATE\_ON\_SET is set, the free\_cache method will not be called after a set request has processed. It is assumed that the lower mib handler using the cache has maintained cache consistency.

If NETSNMP\_CACHE\_DONT\_FREE\_BEFORE\_LOAD is set, the free\_cache method will not be called before the load\_cache method is called. It is assumed that the load\_cache routine will properly deal with being called with a valid cache.

If NETSNMP\_CACHE\_DONT\_FREE\_EXPIRED is set, the free\_cache method will not be called with the cache expires. The expired flag will be set, but the valid flag will not be cleared. It is assumed that the load\_cache routine will properly deal with being called with a valid cache.

If NETSNMP\_CACHE\_PRELOAD is set when a the cache handler is created, the cache load routine will be called immediately.

If NETSNMP\_CACHE\_DONT\_AUTO\_RELEASE is set, the periodic callback that checks for expired caches will skip the cache. The cache will only be checked for expiration when a request triggers the cache handler. This is useful if the cache has it's own periodic callback to keep the cache fresh.

If NETSNMP\_CACHE\_AUTO\_RELOAD is set, a timer will be set up to reload the cache when it expires. This is useful for keeping the cache fresh, even in the absence of incoming snmp requests.

Here are some suggestions for some common situations.

Cached File: If your table is based on a file that may periodically change, you can test the modification date to see if the file has changed since the last cache load. To get the cache helper to call the load function for every request, set the timeout to -1, which will cause the cache to always report that it is expired. This means that you will want to prevent the agent from flushing the cache when it has expired, and you will have to flush it manually if you detect that the file has changed. To accomplish this, set the following flags:

NETSNMP\_CACHE\_DONT\_FREE\_EXPIRED NETSNMP\_CACHE\_DONT\_AUTO\_RELEASE

Constant (periodic) reload: If you want the cache kept up to date regularly, even if no requests for the table are received, you can have your cache load routine called periodically. This is very useful if you need to monitor the data for changes (eg a *LastChanged* object). You will need to prevent the agent from flushing the cache when it expires. Set the cache timeout to the frequency, in seconds, that you wish to reload your cache, and set the following flags:

NETSNMP\_CACHE\_DONT\_FREE\_EXPIRED NETSNMP\_CACHE\_DONT\_AUTO\_RELEASE NETSNMP\_CACHE\_AUTO\_RELOAD

## Function Documentation

### int netsnmp\_cache\_check\_and\_reload (netsnmp\_cache \* cache)

Reload the cache if required.

Definition at line 385 of file cache\_handler.c.

### int netsnmp\_cache\_check\_expired (netsnmp\_cache \* cache)

Check if the cache timeout has passed. Sets and return the expired flag.

Definition at line 370 of file cache\_handler.c.

### netsnmp\_cache\* netsnmp\_cache\_create (int timeout, NetsnmpCacheLoad \* load\_hook, NetsnmpCacheFree \* free\_hook, oid \* rootoid, int rootoid\_len)

returns a cache

Definition at line 136 of file cache\_handler.c.

### netsnmp\_cache\* netsnmp\_cache\_find\_by\_oid (oid \* rootoid, int rootoid\_len)

find existing cache

Definition at line 120 of file cache\_handler.c.

### netsnmp\_cache\* netsnmp\_cache\_get\_head (void)

get cache head

Definition at line 112 of file cache\_handler.c.

### netsnmp\_mib\_handler\* netsnmp\_cache\_handler\_get (netsnmp\_cache \* cache)

returns a cache handler that can be injected into a given handler chain.

Definition at line 247 of file cache\_handler.c.

### int netsnmp\_cache\_handler\_register (netsnmp\_handler\_registration \* reginfo, netsnmp\_cache \* cache)

functionally the same as calling **netsnmp\_register\_handler()** but also injects a cache handler at the same time for you.

Definition at line 295 of file cache\_handler.c.

### int netsnmp\_cache\_helper\_handler (netsnmp\_mib\_handler \* handler, netsnmp\_handler\_registration \* reginfo, netsnmp\_agent\_request\_info \* reqinfo, netsnmp\_request\_info \* requests)

Implements the cache handler.

next handler called automatically - 'AUTO\_NEXT'

next handler called automatically - 'AUTO\_NEXT'

next handler called automatically - 'AUTO\_NEXT'

Definition at line 420 of file cache\_handler.c.

### int netsnmp\_cache\_is\_valid (netsnmp\_agent\_request\_info \* reqinfo, const char \* name)

Is the cache valid for a given request?

Definition at line 402 of file cache\_handler.c.

### netsnmp\_cache\* netsnmp\_cache\_reqinfo\_extract (netsnmp\_agent\_request\_info \* reqinfo, const char \* name)

Extract the cache information for a given request (PDU).

Definition at line 350 of file cache\_handler.c.

### void netsnmp\_cache\_reqinfo\_insert (netsnmp\_cache \* cache, netsnmp\_agent\_request\_info \* reqinfo, const char \* name)

Insert the cache information for a given request (PDU).

Definition at line 333 of file cache\_handler.c.

### unsigned int netsnmp\_cache\_timer\_start (netsnmp\_cache \* cache)

starts the recurring cache\_load callback

Definition at line 191 of file cache\_handler.c.

### void netsnmp\_cache\_timer\_stop (netsnmp\_cache \* cache)

stops the recurring cache\_load callback

Definition at line 226 of file cache\_handler.c.

### netsnmp\_cache\* netsnmp\_extract\_cache\_info (netsnmp\_agent\_request\_info \* reqinfo)

Extract the cache information for a given request (PDU).

Definition at line 362 of file cache\_handler.c.

### netsnmp\_mib\_handler\* netsnmp\_get\_cache\_handler (int timeout, NetsnmpCacheLoad \* load\_hook, NetsnmpCacheFree \* free\_hook, oid \* rootoid, int rootoid\_len)

returns a cache handler that can be injected into a given handler chain.

Definition at line 276 of file cache\_handler.c.

### int netsnmp\_is\_cache\_valid (netsnmp\_agent\_request\_info \* reqinfo)

Is the cache valid for a given request? for backwards compatability. for backwards compat

**netsnmp\_cache\_is\_valid()** is preferred.

Definition at line 413 of file cache\_handler.c.

### int netsnmp\_register\_cache\_handler (netsnmp\_handler\_registration \* reginfo, int timeout, NetsnmpCacheLoad \* load\_hook, NetsnmpCacheFree \* free\_hook)

functionally the same as calling **netsnmp\_register\_handler()** but also injects a cache handler at the same time for you.

Definition at line 308 of file cache\_handler.c.

### void release\_cached\_resources (unsigned int regNo, void \* clientargs)

run regularly to automatically release cached resources. xxx - method to prevent cache from expiring while a request is being processed (e.g. delegated request). proposal: set a flag, which would be cleared when request finished (which could be acomplished by a dummy data list item in agent req info & custom free function).

Definition at line 575 of file cache\_handler.c.

## Author

Generated automatically by Doxygen for net-snmp from the source code.
