Difference between revisions of "Cache handler"

From Net-SNMP Wiki
Jump to: navigation, search
(Initialization: spacing)
(Initialization)
Line 38: Line 38:
 
     /* now create our cache handler: */
 
     /* now create our cache handler: */
 
     cache_handler = netsnmp_get_cache_handler(MY_CACHE_TIMEOUT,              /* how long a cache is valid for */
 
     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_cache_Sload,                /* a pointer to the cache loading function */
 
                                               my_cahe_free,                  /* a pointer to the cache freeing function */
 
                                               my_cahe_free,                  /* a pointer to the cache freeing function */
      my_oid, OID_LENGTH(my_oid)));  /* the OID of the registration point */
+
      my_oid, OID_LENGTH(my_oid)));  /* the OID of the registration point */
 
   
 
   
 
     /* Inject the handler into the handler chain for our registration: */
 
     /* Inject the handler into the handler chain for our registration: */

Revision as of 16:30, 14 July 2011

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