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