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]-->
9 <meta name=
"viewport" content=
"width=device-width, initial-scale=1.0">
11 <title>libmemcached Examples
— libmemcached
1.1.0 documentation
</title>
20 <script type=
"text/javascript" src=
"../_static/js/modernizr.min.js"></script>
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>
29 <script type=
"text/javascript" src=
"../_static/js/theme.js"></script>
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" />
43 <body class=
"wy-body-for-nav">
46 <div class=
"wy-grid-for-nav">
48 <nav data-toggle=
"wy-nav-shift" class=
"wy-nav-side">
49 <div class=
"wy-side-scroll">
50 <div class=
"wy-side-nav-search" >
54 <a href=
"../index.html" class=
"icon icon-home"> libmemcached
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" />
81 <div class=
"wy-menu wy-menu-vertical" data-spy=
"affix" role=
"navigation" aria-label=
"main navigation">
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>
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>
113 <section data-toggle=
"wy-nav-shift" class=
"wy-nav-content-wrap">
116 <nav class=
"wy-nav-top" aria-label=
"top navigation">
118 <i data-toggle=
"wy-nav-top" class=
"fa fa-bars"></i>
119 <a href=
"../index.html">libmemcached
</a>
124 <div class=
"wy-nav-content">
126 <div class=
"rst-content">
144 <div role=
"navigation" aria-label=
"breadcrumbs navigation">
146 <ul class=
"wy-breadcrumbs">
148 <li><a href=
"../index.html">Docs
</a> »</li>
150 <li><a href=
"index_misc.html">Miscellaneous
</a> »</li>
152 <li>libmemcached Examples
</li>
155 <li class=
"wy-breadcrumbs-aside">
159 <a href=
"https://github.com/m6w6/libmemcached/blob/v1.x/docs/source/libmemcached/examples.rst" class=
"fa fa-github"> Edit on GitHub
</a>
170 <div role=
"main" class=
"document" itemscope=
"itemscope" itemtype=
"http://schema.org/Article">
171 <div itemprop=
"articleBody">
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>
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">"--SERVER=host10.example.com
"</span>
185 <span class=
"s">"--SERVER=host11.example.com
"</span>
186 <span class=
"s">"--SERVER=host10.example.com
"</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>
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>
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">"--SERVER=host10.example.com
"</span>
201 <span class=
"s">"--SERVER=host11.example.com
"</span>
202 <span class=
"s">"--SERVER=host10.example.com
"</span><span class=
"p">;
</span>
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>
206 <span class=
"n">memcached_return_t
</span> <span class=
"n">rc
</span><span class=
"p">;
</span>
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">&</span><span class=
"n">rc
</span><span class=
"p">);
</span>
210 <span class=
"c1">// .... do work
</span>
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>
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>
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
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">"foo
"</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">"value
"</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>
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>
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>
246 <p>It is best practice to always look at the return value of any operation.
</p>
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">"fudge
"</span><span class=
"p">,
</span> <span class=
"s">"son
"</span><span class=
"p">,
</span> <span class=
"s">"food
"</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>
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>
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>
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">&</span><span class=
"n">return_key_length
</span><span class=
"p">,
</span>
265 <span class=
"o">&</span><span class=
"n">return_value_length
</span><span class=
"p">,
</span> <span class=
"o">&</span><span class=
"n">flags
</span><span class=
"p">,
</span> <span class=
"o">&</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>
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>
275 <div class=
"section" id=
"see-also">
276 <h2>SEE ALSO
<a class=
"headerlink" href=
"#see-also" title=
"Permalink to this headline">¶
</a></h2>
278 <div><em class=
"manpage"><a class=
"manpage reference external" href=
"https://linux.die.net/man/1/memcached">memcached(
1)
</a></em></div></blockquote>
288 <div class=
"rst-footer-buttons" role=
"navigation" aria-label=
"footer navigation">
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>
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>
300 <div role=
"contentinfo">
302 © <a href=
"../copyright.html">Copyright
</a>
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>.
319 <script type=
"text/javascript">
321 SphinxRtdTheme.Navigation.enable(true);