154dec91be9f0176769c9baddf329dd0f82426aa
[m6w6/libmemcached] / libmemcached / examples.html
1
2
3 <!DOCTYPE html>
4 <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
5 <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
6 <head>
7 <meta charset="utf-8">
8
9 <meta name="viewport" content="width=device-width, initial-scale=1.0">
10
11 <title>libmemcached Examples &mdash; libmemcached 1.0.99 documentation</title>
12
13
14
15
16
17
18
19
20 <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
21
22
23 <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
24 <script src="../_static/jquery.js"></script>
25 <script src="../_static/underscore.js"></script>
26 <script src="../_static/doctools.js"></script>
27 <script src="../_static/language_data.js"></script>
28
29 <script type="text/javascript" src="../_static/js/theme.js"></script>
30
31
32
33
34 <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
35 <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
36 <link rel="index" title="Index" href="../genindex.html" />
37 <link rel="search" title="Search" href="../search.html" />
38 <link rel="copyright" title="Copyright" href="../copyright.html" />
39 <link rel="next" title="libmemcached Versioning" href="versioning.html" />
40 <link rel="prev" title="libmemcached Constants and Defaults" href="constants.html" />
41 </head>
42
43 <body class="wy-body-for-nav">
44
45
46 <div class="wy-grid-for-nav">
47
48 <nav data-toggle="wy-nav-shift" class="wy-nav-side">
49 <div class="wy-side-scroll">
50 <div class="wy-side-nav-search" >
51
52
53
54 <a href="../index.html" class="icon icon-home"> libmemcached
55
56
57
58 </a>
59
60
61
62
63 <div class="version">
64 1.0
65 </div>
66
67
68
69
70 <div role="search">
71 <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
72 <input type="text" name="q" placeholder="Search docs" />
73 <input type="hidden" name="check_keywords" value="yes" />
74 <input type="hidden" name="area" value="default" />
75 </form>
76 </div>
77
78
79 </div>
80
81 <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
82
83
84
85
86
87
88 <p class="caption"><span class="caption-text">libmemcached</span></p>
89 <ul class="current">
90 <li class="toctree-l1"><a class="reference internal" href="../libmemcached.html">Introduction</a></li>
91 <li class="toctree-l1"><a class="reference internal" href="index.html">libmemcached API</a><ul>
92 <li class="toctree-l2"><a class="reference internal" href="index_basics.html">Basics</a></li>
93 <li class="toctree-l2"><a class="reference internal" href="index_data.html">Working with Data</a></li>
94 <li class="toctree-l2"><a class="reference internal" href="index_errors.html">Messages and Errors</a></li>
95 <li class="toctree-l2"><a class="reference internal" href="index_advanced.html">Advanced Topics</a></li>
96 <li class="toctree-l2"><a class="reference internal" href="index_deprecated.html">Deprecated Functionality</a></li>
97 </ul>
98 </li>
99 <li class="toctree-l1 current"><a class="reference internal" href="index_misc.html">Misc</a><ul class="current">
100 <li class="toctree-l2"><a class="reference internal" href="configuration.html">Configuration</a></li>
101 <li class="toctree-l2"><a class="reference internal" href="constants.html">Constants</a></li>
102 <li class="toctree-l2 current"><a class="current reference internal" href="#">Examples</a></li>
103 <li class="toctree-l2"><a class="reference internal" href="versioning.html">Versioning</a></li>
104 </ul>
105 </li>
106 </ul>
107 <p class="caption"><span class="caption-text">libmemcachedutil</span></p>
108 <ul>
109 <li class="toctree-l1"><a class="reference internal" href="../libmemcachedutil.html">Introduction</a></li>
110 <li class="toctree-l1"><a class="reference internal" href="../libmemcachedutil/index.html">libmemcachedutil API</a><ul>
111 <li class="toctree-l2"><a class="reference internal" href="../libmemcachedutil/memcached_pool.html">Working with memcached pools</a></li>
112 </ul>
113 </li>
114 </ul>
115 <p class="caption"><span class="caption-text">libhashkit</span></p>
116 <ul>
117 <li class="toctree-l1"><a class="reference internal" href="../libhashkit.html">Introduction</a></li>
118 <li class="toctree-l1"><a class="reference internal" href="../libhashkit/index.html">libhashkit API</a><ul>
119 <li class="toctree-l2"><a class="reference internal" href="../libhashkit/hashkit_create.html">Creating a hashkit structure</a></li>
120 <li class="toctree-l2"><a class="reference internal" href="../libhashkit/hashkit_function.html">Set Hash Function</a></li>
121 <li class="toctree-l2"><a class="reference internal" href="../libhashkit/hashkit_functions.html">Available Hashes</a></li>
122 <li class="toctree-l2"><a class="reference internal" href="../libhashkit/hashkit_value.html">Generate hash value</a></li>
123 </ul>
124 </li>
125 </ul>
126 <p class="caption"><span class="caption-text">Client Applications</span></p>
127 <ul>
128 <li class="toctree-l1"><a class="reference internal" href="../bin/index.html">Client Applications</a><ul>
129 <li class="toctree-l2"><a class="reference internal" href="../bin/memcapable.html">memcapable - Checking a Memcached server capabilities and compatibility</a></li>
130 <li class="toctree-l2"><a class="reference internal" href="../bin/memcat.html">memcat - “cat” data from a server</a></li>
131 <li class="toctree-l2"><a class="reference internal" href="../bin/memcp.html">memcp - Copy data to a server</a></li>
132 <li class="toctree-l2"><a class="reference internal" href="../bin/memdump.html">memdump - Dumping your server</a></li>
133 <li class="toctree-l2"><a class="reference internal" href="../bin/memerror.html">memerror - translate an error code to a string</a></li>
134 <li class="toctree-l2"><a class="reference internal" href="../bin/memflush.html">memflush - flush all data from a server</a></li>
135 <li class="toctree-l2"><a class="reference internal" href="../bin/memrm.html">memrm - Remove data from a server</a></li>
136 <li class="toctree-l2"><a class="reference internal" href="../bin/memslap.html">memslap - Load testing and benchmarking a server</a></li>
137 <li class="toctree-l2"><a class="reference internal" href="../bin/memaslap.html">memaslap - Load testing and benchmarking a server</a></li>
138 <li class="toctree-l2"><a class="reference internal" href="../bin/memstat.html">memstat - Gather statistics from a server</a></li>
139 <li class="toctree-l2"><a class="reference internal" href="../bin/memexist.html">memexist - Check for the existence of a key</a></li>
140 <li class="toctree-l2"><a class="reference internal" href="../bin/memparse.html">memparse - Parse an option string</a></li>
141 <li class="toctree-l2"><a class="reference internal" href="../bin/memping.html">memping - Test to see if a server is available.</a></li>
142 <li class="toctree-l2"><a class="reference internal" href="../bin/memtouch.html">memtouch - Touches a key.</a></li>
143 </ul>
144 </li>
145 </ul>
146
147
148
149 </div>
150 </div>
151 </nav>
152
153 <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
154
155
156 <nav class="wy-nav-top" aria-label="top navigation">
157
158 <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
159 <a href="../index.html">libmemcached</a>
160
161 </nav>
162
163
164 <div class="wy-nav-content">
165
166 <div class="rst-content">
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 <div role="navigation" aria-label="breadcrumbs navigation">
185
186 <ul class="wy-breadcrumbs">
187
188 <li><a href="../index.html">Docs</a> &raquo;</li>
189
190 <li><a href="index_misc.html">Misc</a> &raquo;</li>
191
192 <li>libmemcached Examples</li>
193
194
195 <li class="wy-breadcrumbs-aside">
196
197
198
199 </li>
200
201 </ul>
202
203
204 <hr/>
205 </div>
206 <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
207 <div itemprop="articleBody">
208
209 <div class="section" id="libmemcached-examples">
210 <h1>libmemcached Examples<a class="headerlink" href="#libmemcached-examples" title="Permalink to this headline"></a></h1>
211 <p>Examples for libmemcached</p>
212 <div class="section" id="description">
213 <h2>DESCRIPTION<a class="headerlink" href="#description" title="Permalink to this headline"></a></h2>
214 <p>For full examples, test cases are found in tests/*.c in the main
215 distribution. These are always up to date, and are used for each test run of
216 the library.</p>
217 </div>
218 <div class="section" id="connecting-to-servers">
219 <h2>Connecting to servers<a class="headerlink" href="#connecting-to-servers" title="Permalink to this headline"></a></h2>
220 <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">config_string</span> <span class="o">=</span>
221 <span class="s">&quot;--SERVER=host10.example.com &quot;</span>
222 <span class="s">&quot;--SERVER=host11.example.com &quot;</span>
223 <span class="s">&quot;--SERVER=host10.example.com&quot;</span><span class="p">;</span>
224 <span class="n">memcached_st</span> <span class="o">*</span><span class="n">memc</span><span class="o">=</span> <span class="n">memcached</span><span class="p">(</span><span class="n">config_string</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">config_string</span><span class="p">);</span>
225 <span class="p">{</span>
226 <span class="c1">// ...</span>
227 <span class="p">}</span>
228 <span class="n">memcached_free</span><span class="p">(</span><span class="n">memc</span><span class="p">);</span>
229 </pre></div>
230 </div>
231 <p>In the above code you create a <a class="reference internal" href="memcached_create.html#_CPPv412memcached_st" title="memcached_st"><code class="xref cpp cpp-type docutils literal notranslate"><span class="pre">memcached_st</span></code></a> object with three server
232 by making use of <a class="reference internal" href="memcached_create.html#_CPPv416memcached_createP12memcached_st" title="memcached_create"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">memcached_create()</span></code></a>.</p>
233 </div>
234 <div class="section" id="creating-a-pool-of-servers">
235 <h2>Creating a pool of servers<a class="headerlink" href="#creating-a-pool-of-servers" title="Permalink to this headline"></a></h2>
236 <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">config_string</span><span class="o">=</span> <span class="s">&quot;--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com&quot;</span><span class="p">;</span>
237
238 <span class="n">memcached_pool_st</span><span class="o">*</span> <span class="n">pool</span><span class="o">=</span> <span class="n">memcached_pool</span><span class="p">(</span><span class="n">config_string</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">config_string</span><span class="p">));</span>
239
240 <span class="n">memcached_return_t</span> <span class="n">rc</span><span class="p">;</span>
241
242 <span class="n">memcached_st</span> <span class="o">*</span><span class="n">memc</span><span class="o">=</span> <span class="n">memcached_pool_pop</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="nb">false</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">rc</span><span class="p">);</span>
243
244 <span class="c1">// .... do work</span>
245
246 <span class="cm">/*</span>
247 <span class="cm"> Release the memc_ptr that was pulled from the pool</span>
248 <span class="cm">*/</span>
249 <span class="n">memcached_pool_push</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="n">memc</span><span class="p">);</span>
250
251 <span class="cm">/*</span>
252 <span class="cm"> Destroy the pool.</span>
253 <span class="cm">*/</span>
254 <span class="n">memcached_pool_destroy</span><span class="p">(</span><span class="n">pool</span><span class="p">);</span>
255 </pre></div>
256 </div>
257 <p>In the above code you create a <a class="reference internal" href="../libmemcachedutil/memcached_pool.html#_CPPv417memcached_pool_st" title="memcached_pool_st"><code class="xref cpp cpp-type docutils literal notranslate"><span class="pre">memcached_pool_st</span></code></a> object with three
258 server by making use of <a class="reference internal" href="../libmemcachedutil/memcached_pool.html#_CPPv414memcached_poolPKc6size_t" title="memcached_pool"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">memcached_pool()</span></code></a>.</p>
259 <p>When <a class="reference internal" href="../libmemcachedutil/memcached_pool.html#_CPPv422memcached_pool_destroyP17memcached_pool_st" title="memcached_pool_destroy"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">memcached_pool_destroy()</span></code></a> all memory will be released that is associated
260 with the pool.</p>
261 </div>
262 <div class="section" id="adding-a-value-to-the-server">
263 <h2>Adding a value to the server<a class="headerlink" href="#adding-a-value-to-the-server" title="Permalink to this headline"></a></h2>
264 <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">char</span> <span class="o">*</span><span class="n">key</span><span class="o">=</span> <span class="s">&quot;foo&quot;</span><span class="p">;</span>
265 <span class="kt">char</span> <span class="o">*</span><span class="n">value</span><span class="o">=</span> <span class="s">&quot;value&quot;</span><span class="p">;</span>
266 <span class="kt">time_t</span> <span class="n">expires</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
267 <span class="kt">uint32_t</span> <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
268
269 <span class="n">memcached_return_t</span> <span class="n">rc</span> <span class="o">=</span> <span class="n">memcached_set</span><span class="p">(</span><span class="n">memc</span><span class="p">,</span>
270 <span class="n">key</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">key</span><span class="p">),</span>
271 <span class="n">value</span><span class="p">,</span> <span class="n">value_length</span><span class="p">,</span>
272 <span class="n">expires</span><span class="p">,</span> <span class="n">flags</span><span class="p">);</span>
273
274 <span class="k">if</span> <span class="p">(</span><span class="n">rc</span> <span class="o">!=</span> <span class="n">MEMCACHED_SUCCESS</span><span class="p">)</span>
275 <span class="p">{</span>
276 <span class="c1">// handle failure</span>
277 <span class="p">}</span>
278 </pre></div>
279 </div>
280 <p>It is best practice to always look at the return value of any operation.</p>
281 </div>
282 <div class="section" id="fetching-multiple-values">
283 <h2>Fetching multiple values<a class="headerlink" href="#fetching-multiple-values" title="Permalink to this headline"></a></h2>
284 <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">memcached_return_t</span> <span class="n">rc</span><span class="p">;</span>
285 <span class="kt">char</span> <span class="o">*</span><span class="n">keys</span><span class="p">[]</span><span class="o">=</span> <span class="p">{</span><span class="s">&quot;fudge&quot;</span><span class="p">,</span> <span class="s">&quot;son&quot;</span><span class="p">,</span> <span class="s">&quot;food&quot;</span><span class="p">};</span>
286 <span class="kt">size_t</span> <span class="n">key_length</span><span class="p">[]</span><span class="o">=</span> <span class="p">{</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">};</span>
287 <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
288 <span class="kt">uint32_t</span> <span class="n">flags</span><span class="p">;</span>
289
290 <span class="kt">char</span> <span class="n">return_key</span><span class="p">[</span><span class="n">MEMCACHED_MAX_KEY</span><span class="p">];</span>
291 <span class="kt">size_t</span> <span class="n">return_key_length</span><span class="p">;</span>
292 <span class="kt">char</span> <span class="o">*</span><span class="n">return_value</span><span class="p">;</span>
293 <span class="kt">size_t</span> <span class="n">return_value_length</span><span class="p">;</span>
294
295 <span class="n">rc</span><span class="o">=</span> <span class="n">memcached_mget</span><span class="p">(</span><span class="n">memc</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="n">key_length</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
296
297 <span class="n">x</span><span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
298 <span class="k">while</span> <span class="p">((</span><span class="n">return_value</span><span class="o">=</span> <span class="n">memcached_fetch</span><span class="p">(</span><span class="n">memc</span><span class="p">,</span> <span class="n">return_key</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">return_key_length</span><span class="p">,</span>
299 <span class="o">&amp;</span><span class="n">return_value_length</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">flags</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">rc</span><span class="p">)))</span>
300 <span class="p">{</span>
301 <span class="n">free</span><span class="p">(</span><span class="n">return_value</span><span class="p">);</span>
302 <span class="n">x</span><span class="o">++</span><span class="p">;</span>
303 <span class="p">}</span>
304 </pre></div>
305 </div>
306 <p>Notice that you freed values returned from memcached_fetch(). The define
307 <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">MEMCACHED_MAX_KEY</span></code> is provided for usage.</p>
308 </div>
309 <div class="section" id="see-also">
310 <h2>SEE ALSO<a class="headerlink" href="#see-also" title="Permalink to this headline"></a></h2>
311 <blockquote>
312 <div><p><em class="manpage"><a class="manpage reference external" href="http://man7.org/linux/man-pages/man1/memcached.1.html">memcached(1)</a></em></p>
313 </div></blockquote>
314 </div>
315 </div>
316
317
318 </div>
319
320 </div>
321 <footer>
322
323 <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
324
325 <a href="versioning.html" class="btn btn-neutral float-right" title="libmemcached Versioning" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
326
327
328 <a href="constants.html" class="btn btn-neutral float-left" title="libmemcached Constants and Defaults" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
329
330 </div>
331
332
333 <hr/>
334
335 <div role="contentinfo">
336 <p>
337 &copy; <a href="../copyright.html">Copyright</a>
338
339 </p>
340 </div>
341 Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
342
343 </footer>
344
345 </div>
346 </div>
347
348 </section>
349
350 </div>
351
352
353
354 <script type="text/javascript">
355 jQuery(function () {
356 SphinxRtdTheme.Navigation.enable(true);
357 });
358 </script>
359
360
361
362
363
364
365 </body>
366 </html>