<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Adds a variable in user cache and overwrites a variable if it already exists in the cache</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.wincache-ucache-meminfo.html">wincache_ucache_meminfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.wincache-unlock.html">wincache_unlock</a></div> <div class="up"><a href="ref.wincache.html">WinCache Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.wincache-ucache-set" class="refentry"> <div class="refnamediv"> <h1 class="refname">wincache_ucache_set</h1> <p class="verinfo">(PECL wincache >= 1.1.0)</p><p class="refpurpose"><span class="refname">wincache_ucache_set</span> — <span class="dc-title"> Adds a variable in user cache and overwrites a variable if it already exists in the cache </span></p> </div> <div class="refsect1 description" id="refsect1-function.wincache-ucache-set-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>wincache_ucache_set</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$ttl</code><span class="initializer"> = 0</span></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>wincache_ucache_set</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$unused</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$ttl</code><span class="initializer"> = 0</span></span> ]] )</div> <p class="para rdfs-comment"> Adds a variable in user cache. Overwrites a variable if it already exists in the cache. The added or updated variable remains in the user cache unless its time to live expires or it is deleted by using <span class="function"><a href="function.wincache-ucache-delete.html" class="function">wincache_ucache_delete()</a></span> or <span class="function"><a href="function.wincache-ucache-clear.html" class="function">wincache_ucache_clear()</a></span> functions. </p> </div> <div class="refsect1 parameters" id="refsect1-function.wincache-ucache-set-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">key</code></em></span> <dd> <p class="para"> Store the variable using this <em><code class="parameter">key</code></em> name. If a variable with same <em><code class="parameter">key</code></em> is already present the function will overwrite the previous value with the new one. <em><code class="parameter">key</code></em> is case sensitive. <em><code class="parameter">key</code></em> can also take array of name => value pairs where names will be used as keys. This can be used to add multiple values in the cache in one operation, thus avoiding race condition. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value</code></em></span> <dd> <p class="para"> Value of a variable to store. <em><code class="parameter">Value</code></em> supports all data types except resources, such as file handles. This paramter is ignored if first argument is an array. A general guidance is to pass <strong><code>NULL</code></strong> as <em><code class="parameter">value</code></em> while using array as <em><code class="parameter">key</code></em>. If <em><code class="parameter">value</code></em> is an object, or an array containing objects, then the objects will be serialized. See <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> for details on serializing objects. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">values</code></em></span> <dd> <p class="para"> Associative array of keys and values. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">ttl</code></em></span> <dd> <p class="para"> Time for the variable to live in the cache in seconds. After the value specified in <em><code class="parameter">ttl</code></em> has passed the stored variable will be deleted from the cache. This parameter takes a default value of <em>0</em> which means the variable will stay in the cache unless explicitly deleted by using <span class="function"><a href="function.wincache-ucache-delete.html" class="function">wincache_ucache_delete()</a></span> or <span class="function"><a href="function.wincache-ucache-clear.html" class="function">wincache_ucache_clear()</a></span> functions. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.wincache-ucache-set-returnvalues"> <h3 class="title">Return Values</h3> <p class="simpara"> If <em><code class="parameter">key</code></em> is string, the function returns <strong><code>TRUE</code></strong> on success and <strong><code>FALSE</code></strong> on failure. </p> <p class="para"> If <em><code class="parameter">key</code></em> is an array, the function returns: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> If all the name => value pairs in the array can be set, function returns an empty array; </span> </li> <li class="listitem"> <span class="simpara"> If all the name => value pairs in the array cannot be set, function returns <strong><code>FALSE</code></strong>; </span> </li> <li class="listitem"> <span class="simpara"> If some can be set while others cannot, function returns an array with name=>value pair for which the addition failed in the user cache. </span> </li> </ul> </p> </div> <div class="refsect1 examples" id="refsect1-function.wincache-ucache-set-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-583"> <p><strong>Example #1 <span class="function"><strong>wincache_ucache_set()</strong></span> with <em><code class="parameter">key</code></em> as a string</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">'BAR'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #0000BB">$bar</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$bar1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'BAR1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #0000BB">$bar1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(true) string(3) "BAR" bool(true) string(3) "BAR1" </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-584"> <p><strong>Example #2 <span class="function"><strong>wincache_ucache_set()</strong></span> with <em><code class="parameter">key</code></em> as an array</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$colors_array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'green' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'5'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Blue' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'6'</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'7'</span><span style="color: #007700">, </span><span style="color: #DD0000">'cyan' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'8'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$colors_array</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$colors_array</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">wincache_ucache_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'Blue'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(0) {} array(0) {} string(1) "6" </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.wincache-ucache-set-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.wincache-ucache-add.html" class="function" rel="rdfs-seeAlso">wincache_ucache_add()</a> - Adds a variable in user cache only if variable does not already exist in the cache</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-get.html" class="function" rel="rdfs-seeAlso">wincache_ucache_get()</a> - Gets a variable stored in the user cache</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-delete.html" class="function" rel="rdfs-seeAlso">wincache_ucache_delete()</a> - Deletes variables from the user cache</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-clear.html" class="function" rel="rdfs-seeAlso">wincache_ucache_clear()</a> - Deletes entire content of the user cache</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-exists.html" class="function" rel="rdfs-seeAlso">wincache_ucache_exists()</a> - Checks if a variable exists in the user cache</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-meminfo.html" class="function" rel="rdfs-seeAlso">wincache_ucache_meminfo()</a> - Retrieves information about user cache memory usage</span></li> <li class="member"> <span class="function"><a href="function.wincache-ucache-info.html" class="function" rel="rdfs-seeAlso">wincache_ucache_info()</a> - Retrieves information about data stored in the user cache</span></li> <li class="member"><a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.wincache-ucache-meminfo.html">wincache_ucache_meminfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.wincache-unlock.html">wincache_unlock</a></div> <div class="up"><a href="ref.wincache.html">WinCache Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>