Deploy to GitHub pages
[awesomized/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.1.0 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 type="text/javascript" src="../_static/jquery.js"></script>
25 <script type="text/javascript" src="../_static/underscore.js"></script>
26 <script type="text/javascript" src="../_static/doctools.js"></script>
27 <script type="text/javascript" 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.1
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 <ul class="current">
89 <li class="toctree-l1"><a class="reference internal" href="../libmemcached.html">Introduction</a></li>
90 <li class="toctree-l1"><a class="reference internal" href="index.html">libmemcached API</a></li>
91 <li class="toctree-l1"><a class="reference internal" href="../libmemcachedutil/index.html">libmemcachedutil API</a></li>
92 <li class="toctree-l1"><a class="reference internal" href="../libhashkit/index.html">libhashkit API</a></li>
93 <li class="toctree-l1"><a class="reference internal" href="../bin/index.html">Client Applications</a></li>
94 <li class="toctree-l1 current"><a class="reference internal" href="index_misc.html">Miscellaneous</a><ul class="current">
95 <li class="toctree-l2"><a class="reference internal" href="configuration.html">Configuration</a></li>
96 <li class="toctree-l2"><a class="reference internal" href="constants.html">Constants</a></li>
97 <li class="toctree-l2 current"><a class="current reference internal" href="#">Examples</a></li>
98 <li class="toctree-l2"><a class="reference internal" href="versioning.html">Versioning</a></li>
99 </ul>
100 </li>
101 </ul>
102 <ul>
103 <li class="toctree-l1"><a class="reference internal" href="../changelogs.html">Change Logs</a></li>
104 <li class="toctree-l1"><a class="reference internal" href="../copyright.html">Copyright</a></li>
105 </ul>
106
107
108
109 </div>
110 </div>
111 </nav>
112
113 <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
114
115
116 <nav class="wy-nav-top" aria-label="top navigation">
117
118 <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
119 <a href="../index.html">libmemcached</a>
120
121 </nav>
122
123
124 <div class="wy-nav-content">
125
126 <div class="rst-content">
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 <div role="navigation" aria-label="breadcrumbs navigation">
145
146 <ul class="wy-breadcrumbs">
147
148 <li><a href="../index.html">Docs</a> &raquo;</li>
149
150 <li><a href="index_misc.html">Miscellaneous</a> &raquo;</li>
151
152 <li>libmemcached Examples</li>
153
154
155 <li class="wy-breadcrumbs-aside">
156
157
158
159 <a href="https://github.com/m6w6/libmemcached/blob/v1.x/docs/source/libmemcached/examples.rst" class="fa fa-github"> Edit on GitHub</a>
160
161
162
163 </li>
164
165 </ul>
166
167
168 <hr/>
169 </div>
170 <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
171 <div itemprop="articleBody">
172
173 <div class="section" id="libmemcached-examples">
174 <h1>libmemcached Examples<a class="headerlink" href="#libmemcached-examples" title="Permalink to this headline"></a></h1>
175 <p>Examples for libmemcached</p>
176 <div class="section" id="description">
177 <h2>DESCRIPTION<a class="headerlink" href="#description" title="Permalink to this headline"></a></h2>
178 <p>For full examples, test cases are found in tests/*.c in the main distribution.
179 These are always up to date, and are used for each test run of the library.</p>
180 </div>
181 <div class="section" id="connecting-to-servers">
182 <h2>Connecting to servers<a class="headerlink" href="#connecting-to-servers" title="Permalink to this headline"></a></h2>
183 <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>
184 <span class="s">&quot;--SERVER=host10.example.com &quot;</span>
185 <span class="s">&quot;--SERVER=host11.example.com &quot;</span>
186 <span class="s">&quot;--SERVER=host10.example.com&quot;</span><span class="p">;</span>
187 <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>
188 <span class="p">{</span>
189 <span class="c1">// ...</span>
190 <span class="p">}</span>
191 <span class="n">memcached_free</span><span class="p">(</span><span class="n">memc</span><span class="p">);</span>
192 </pre></div>
193 </div>
194 <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-any docutils literal notranslate"><span class="pre">memcached_st</span></code></a> object with three server by making
195 use of <a class="reference internal" href="memcached_create.html#_CPPv416memcached_createP12memcached_st" title="memcached_create"><code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">memcached_create()</span></code></a>.</p>
196 </div>
197 <div class="section" id="creating-a-pool-of-servers">
198 <h2>Creating a pool of servers<a class="headerlink" href="#creating-a-pool-of-servers" title="Permalink to this headline"></a></h2>
199 <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>
200 <span class="s">&quot;--SERVER=host10.example.com &quot;</span>
201 <span class="s">&quot;--SERVER=host11.example.com &quot;</span>
202 <span class="s">&quot;--SERVER=host10.example.com&quot;</span><span class="p">;</span>
203
204 <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>
205
206 <span class="n">memcached_return_t</span> <span class="n">rc</span><span class="p">;</span>
207
208 <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>
209
210 <span class="c1">// .... do work</span>
211
212 <span class="cm">/*</span>
213 <span class="cm"> Release the memc_ptr that was pulled from the pool</span>
214 <span class="cm">*/</span>
215 <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>
216
217 <span class="cm">/*</span>
218 <span class="cm"> Destroy the pool.</span>
219 <span class="cm">*/</span>
220 <span class="n">memcached_pool_destroy</span><span class="p">(</span><span class="n">pool</span><span class="p">);</span>
221 </pre></div>
222 </div>
223 <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-any docutils literal notranslate"><span class="pre">memcached_pool_st</span></code></a> object with three server by
224 making use of <a class="reference internal" href="../libmemcachedutil/memcached_pool.html#_CPPv414memcached_poolPKc6size_t" title="memcached_pool"><code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">memcached_pool()</span></code></a>.</p>
225 <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-any docutils literal notranslate"><span class="pre">memcached_pool_destroy()</span></code></a> all memory will be released that is associated
226 with the pool.</p>
227 </div>
228 <div class="section" id="adding-a-value-to-the-server">
229 <h2>Adding a value to the server<a class="headerlink" href="#adding-a-value-to-the-server" title="Permalink to this headline"></a></h2>
230 <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>
231 <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>
232 <span class="kt">time_t</span> <span class="n">expires</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
233 <span class="kt">uint32_t</span> <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
234
235 <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>
236 <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>
237 <span class="n">value</span><span class="p">,</span> <span class="n">value_length</span><span class="p">,</span>
238 <span class="n">expires</span><span class="p">,</span> <span class="n">flags</span><span class="p">);</span>
239
240 <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>
241 <span class="p">{</span>
242 <span class="c1">// handle failure</span>
243 <span class="p">}</span>
244 </pre></div>
245 </div>
246 <p>It is best practice to always look at the return value of any operation.</p>
247 </div>
248 <div class="section" id="fetching-multiple-values">
249 <h2>Fetching multiple values<a class="headerlink" href="#fetching-multiple-values" title="Permalink to this headline"></a></h2>
250 <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>
251 <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>
252 <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>
253 <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
254 <span class="kt">uint32_t</span> <span class="n">flags</span><span class="p">;</span>
255
256 <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>
257 <span class="kt">size_t</span> <span class="n">return_key_length</span><span class="p">;</span>
258 <span class="kt">char</span> <span class="o">*</span><span class="n">return_value</span><span class="p">;</span>
259 <span class="kt">size_t</span> <span class="n">return_value_length</span><span class="p">;</span>
260
261 <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>
262
263 <span class="n">x</span><span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
264 <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>
265 <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>
266 <span class="p">{</span>
267 <span class="n">free</span><span class="p">(</span><span class="n">return_value</span><span class="p">);</span>
268 <span class="n">x</span><span class="o">++</span><span class="p">;</span>
269 <span class="p">}</span>
270 </pre></div>
271 </div>
272 <p>Notice that you freed values returned from memcached_fetch(). The define
273 <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">MEMCACHED_MAX_KEY</span></code> is provided for usage.</p>
274 </div>
275 <div class="section" id="see-also">
276 <h2>SEE ALSO<a class="headerlink" href="#see-also" title="Permalink to this headline"></a></h2>
277 <blockquote>
278 <div><em class="manpage"><a class="manpage reference external" href="https://linux.die.net/man/1/memcached">memcached(1)</a></em></div></blockquote>
279 </div>
280 </div>
281
282
283 </div>
284
285 </div>
286 <footer>
287
288 <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
289
290 <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>
291
292
293 <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>
294
295 </div>
296
297
298 <hr/>
299
300 <div role="contentinfo">
301 <p>
302 &copy; <a href="../copyright.html">Copyright</a>
303
304 </p>
305 </div>
306 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>.
307
308 </footer>
309
310 </div>
311 </div>
312
313 </section>
314
315 </div>
316
317
318
319 <script type="text/javascript">
320 jQuery(function () {
321 SphinxRtdTheme.Navigation.enable(true);
322 });
323 </script>
324
325
326
327
328
329
330 </body>
331 </html>