Cache handler

From Net-SNMP Wiki
Revision as of 16:30, 14 July 2011 by Wes (Talk | contribs) (Initialization)

Jump to: navigation, search
Net-SNMP MIB Helper
Cache Handler
Documentation: doxygen API
Code: agent/helpers/cache_helper.c
Other Helpers: Agent Helpers

Overview

The cache_helper is designed to do one thing: help you manage a cache of data for a given mib implementation. It simply calls your load and free hooks based on how long the cache should remain valid. You define the data structures to hold the information and use your cache to respond to requests. The cache handler simply takes care of the timing about when to load it.

It is typically injected during the mib module's initialization routine.

Usage

Using the cache handler involves a few steps:

  1. inject a cache handler into the handler chain for your module
  2. implement the loading routine
  3. implement the free routine
  4. use the cache to load your data

Initialization

In your init_XXX() routine, do something like this:

 #define MY_CACHE_TIMEOUT 30

 NetsnmpCacheLoad *my_cache_load;
 NetsnmpCacheFree *my_cache_free;

 struct my_cache {
   int index;
   char *value;
 };

 void init_XXX() {
   netsnmp_mib_handler *cache_handler;

   /* create and register your "object".  Example using the ... */
   netsnmp_register_table_iterator(reginfo, iterator_info);

   /* now create our cache handler: */
   cache_handler = netsnmp_get_cache_handler(MY_CACHE_TIMEOUT,               /* how long a cache is valid for */
        		   		      my_cache_Sload,                 /* a pointer to the cache loading function */
                                             my_cahe_free,                   /* a pointer to the cache freeing function */
					      my_oid, OID_LENGTH(my_oid)));   /* the OID of the registration point */

   /* Inject the handler into the handler chain for our registration: */
   netsnmp_inject_handler(reginfo, cache_handler);
 }

Loading