| 1 | <?xml version="1.0" encoding="iso-8859-1"?> | 
|---|
| 2 | <!DOCTYPE chapter PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc"> | 
|---|
| 3 | <chapter id="gencache"> | 
|---|
| 4 | <chapterinfo> | 
|---|
| 5 | <author> | 
|---|
| 6 | <firstname>Rafal</firstname><surname>Szczesniak</surname> | 
|---|
| 7 | </author> | 
|---|
| 8 | <pubdate>April 2003</pubdate> | 
|---|
| 9 | </chapterinfo> | 
|---|
| 10 |  | 
|---|
| 11 | <title>General cache mechanism and API</title> | 
|---|
| 12 |  | 
|---|
| 13 | <sect1> | 
|---|
| 14 | <title>Abstract</title> | 
|---|
| 15 | <para> | 
|---|
| 16 | General cache (gencache) was designed to combine various kinds of caching | 
|---|
| 17 | mechanisms into one, defined by a simple API. This way, anyone can use it | 
|---|
| 18 | to create their own caching layer on top of gencache. An example of | 
|---|
| 19 | such approach is the netbios name cache. | 
|---|
| 20 | </para> | 
|---|
| 21 | </sect1> | 
|---|
| 22 |  | 
|---|
| 23 | <sect1> | 
|---|
| 24 | <title>The mechanism</title> | 
|---|
| 25 | <para> | 
|---|
| 26 | Gencache utilises <emphasise>tdb</emphasise> database, like many other | 
|---|
| 27 | parts of Samba. As its origins are in Berkeley DB implementation, it | 
|---|
| 28 | uses key/value pairs stored in binary file. The values gencache | 
|---|
| 29 | operates on are string-based, however. This makes very easy to use it | 
|---|
| 30 | in command line environment eg. to quickly take a look at what's in | 
|---|
| 31 | the cache or set some value. | 
|---|
| 32 | </para> | 
|---|
| 33 |  | 
|---|
| 34 | <para> | 
|---|
| 35 | All the data is stored in <filename>gencache.tdb</filename> | 
|---|
| 36 | file. Records put there are in key/value format as mentioned below, | 
|---|
| 37 | but as it's a cache, the timeout plays also important role and has a | 
|---|
| 38 | special place in the key/value pair, as well as API. | 
|---|
| 39 | </para> | 
|---|
| 40 | </sect1> | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | <sect1> | 
|---|
| 44 | <title>The data structure</title> | 
|---|
| 45 | <para> | 
|---|
| 46 | The record stored in <filename>gencache.tdb</filename> file consists | 
|---|
| 47 | of the key, the value and the expiration timeout. While the first part | 
|---|
| 48 | is stored completely independent from the others, the last two are | 
|---|
| 49 | kept together. The form the record has is: | 
|---|
| 50 | </para> | 
|---|
| 51 |  | 
|---|
| 52 | <para><programlisting> | 
|---|
| 53 | key:     <string> | 
|---|
| 54 | value:   <12-digit timeout>/<string> | 
|---|
| 55 | </programlisting></para> | 
|---|
| 56 |  | 
|---|
| 57 | <para>The timeout part is the ASCII representation of | 
|---|
| 58 | <emphasis>time_t</emphasis> value of the time when the cache entry | 
|---|
| 59 | expires. Obviously the API, the programmer is provided with, hides this detail, | 
|---|
| 60 | so that you don't have to care about checking it. Simply watch | 
|---|
| 61 | carefully the return status of the function. | 
|---|
| 62 | </para> | 
|---|
| 63 | </sect1> | 
|---|
| 64 |  | 
|---|
| 65 | <sect1> | 
|---|
| 66 | <title>The API</title> | 
|---|
| 67 |  | 
|---|
| 68 | <para><programlisting> | 
|---|
| 69 | BOOL gencache_init() | 
|---|
| 70 | </programlisting></para> | 
|---|
| 71 |  | 
|---|
| 72 | <para>This is used to initialise to whole caching mechanism. It means | 
|---|
| 73 | opening the file or creating it if non-existing. If it's already been | 
|---|
| 74 | opened earlier, then the routine just does nothing and returns | 
|---|
| 75 | <constant>true</constant>. If something goes wrong, say the user | 
|---|
| 76 | doesn't have necessary rights, the function returns | 
|---|
| 77 | <constant>false</constant>.</para> | 
|---|
| 78 |  | 
|---|
| 79 | <para><programlisting> | 
|---|
| 80 | BOOL gencache_shutdown() | 
|---|
| 81 | </programlisting></para> | 
|---|
| 82 |  | 
|---|
| 83 | <para>This is the proper way to close the cache file. It simply | 
|---|
| 84 | returns <constant>true</constant> after successful closing file and | 
|---|
| 85 | <constant>false</constant> upon a failure.</para> | 
|---|
| 86 |  | 
|---|
| 87 | <para><programlisting> | 
|---|
| 88 | BOOL gencache_set(const char* keystr, const char* value, time_t timeout) | 
|---|
| 89 | </programlisting></para> | 
|---|
| 90 |  | 
|---|
| 91 | <para>This is one of the most basic functions. What it allows you to | 
|---|
| 92 | do is to set some particular cache entry. If the entry haven't | 
|---|
| 93 | existed yet, the function will act just as it was "gencache_add" | 
|---|
| 94 | function. If it's already been in the cache, the entry will be set to | 
|---|
| 95 | the new value. In either case, the cache entry will be set with given | 
|---|
| 96 | key, value and timeout. Thus it is comfortable way to just set the | 
|---|
| 97 | entry and not care about the details.</para> | 
|---|
| 98 |  | 
|---|
| 99 | <para><programlisting> | 
|---|
| 100 | BOOL gencache_set_only(const char* keystr, const char* value, time_t timeout) | 
|---|
| 101 | </programlisting></para> | 
|---|
| 102 |  | 
|---|
| 103 | <para><programlisting> | 
|---|
| 104 | BOOL gencache_del(const char* keystr) | 
|---|
| 105 | </programlisting></para> | 
|---|
| 106 |  | 
|---|
| 107 | <para><programlisting> | 
|---|
| 108 | BOOL gencache_get(const char* keystr, char** valstr, time_t* timeout) | 
|---|
| 109 | </programlisting></para> | 
|---|
| 110 |  | 
|---|
| 111 | <para><programlisting> | 
|---|
| 112 | void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr), | 
|---|
| 113 | void* data, const char* keystr_pattern) | 
|---|
| 114 |  | 
|---|
| 115 | </programlisting></para> | 
|---|
| 116 |  | 
|---|
| 117 | </sect1> | 
|---|
| 118 |  | 
|---|
| 119 | </chapter> | 
|---|