ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:memcached

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v5:userguide:memcached [2016/01/17 03:39] – ↷ Page moved from v5:reference:memcached to v5:userguide:memcached mnewnhamv5:userguide:memcached [2022/03/29 18:29] (current) – [Usage] compress also works with memcached now dregad
Line 1: Line 1:
 ====== Memcached Setup ====== ====== Memcached Setup ======
-<WRAP tip> +
-There are 2 pear modules for connection to a Memcached server, the older [[https://pecl.php.net/package/memcache|PEAR Memcache library]] and the newer [[https://pecl.php.net/package/memcached|PEAR Memcached library]].  +
-  * Windows support is only available for the older Memcache library. +
-  * ADOdb has only been tested against the older library +
-</WRAP>+
 ===== Description ===== ===== Description =====
-This section describes connection to an already configured Memcache server. Information about installation and configuration of Memcached servers can be found [[https://memcached.org/|here]].+This section describes connecting to an already configured Memcache server. Information about installation and configuration of Memcached servers can be found [[https://memcached.org/|here]]. 
 ===== The Memcache Library ===== ===== The Memcache Library =====
 +There are 2 distinct PEAR modules for connecting to a [[wp>Memcached]] server: the [[https://pecl.php.net/package/memcache|Memcache]] library and the [[https://pecl.php.net/package/memcached|Memcached]] library.
 +<WRAP tip>
 +  * Windows support is only available for the //Memcache// library.
 +  * //Memcache// should be used in PHP 5.x environments on UNIX platforms.
 +  * //Memcached// should be used in PHP 7.x environments on UNIX platforms.
 +  * If both libraries are available, //Memcache// will be used.
 +</WRAP>
 +===== Why can't I use the memcached library under Windows? =====
 +The memcached library is much more heavily featured, more modern and better maintained. The problem with it is not a problem with ADOdb, nor a problem with PHP. The PHP module is dependent on the 3rd party library **libmemcache**, which has no Windows support. Any Windows DLLs you might find floating around the web will __always__ be built on the memcache library.
  
  
Line 14: Line 20:
  
 $db = NewADOConnection($driver='mysqli'); $db = NewADOConnection($driver='mysqli');
 +
 /* /*
-* Enable memcache + * Enable memcache 
-*/+ */
 $db->memCache = true; $db->memCache = true;
 +
 /* /*
-* Add one or more servers, for use in distributed systems + * Add one or more servers, for use in distributed systems 
-*/ + */ 
-$db->memCacheHost = array('192.168.0.78', '192.168.0.79', '192.168.0.80'); +$db->memCacheHost = array('192.168.0.78', '192.168.0.79', '192.168.0.80'); 
 /* /*
-* Optionally add the server port if it differs from the default + * Optionally add the server port if it differs from the default 
-*/+ */
 $db->memCachePort = 11211; $db->memCachePort = 11211;
 +
 /* /*
-* Use 'true' to store the item compressed (uses zlib) + * Use 'true' to store the item compressed (uses zlib) 
-*/ + */ 
-$db->memCacheCompress = false; +$db->memCacheCompress = false;
  
 /* /*
-* We connect to the database, not the memcache server + * We connect to the database, not the memcache server 
-*/+ */
 $db->Connect('localhost','mysql-user','mysql-password','employees'); $db->Connect('localhost','mysql-user','mysql-password','employees');
  
 /* /*
-* look for a cached query with a life of 2400 seconds + * look for a cached query with a life of 2400 seconds 
-*/+ */
 $sql = 'SELECT code,description FROM xref_table'; $sql = 'SELECT code,description FROM xref_table';
 $db->cacheExecute(2400,$sql); $db->cacheExecute(2400,$sql);
  
 </code> </code>
 +===== Adding Weighted Server Groups =====
 +
 +From ADOdb version 5.22, the **memcached** library supports the use of **//Weighted server groups//**, in which a specific server can be forced to return a result a specific percentage of the time. 
 +
 +If the feature is used, the total weighting of the server group must equal 100%.  To achieve this, the configuration of the servers must be defined in a different way to the default. Each host is defined as an array:
 +
 +<code php>
 +array(
 + 'host'=> The host name,
 + 'port'=> The port, defaults to 11211,
 + 'weight'=> A percentage, up to 100,
 +);
 +</code>
 +
 +The host arrays are then combined together. In this example 86.91 serves 70% of requests and 86.92 serves 30%
 +<code php>
 +
 +$hosts = array(
 +    array('host'=>'192.168.86.91','port'=>'11211','weight'=>70),
 +    array('host'=>'192.168.86.92','port'=>'11211','weight'=>30),
 +    );
 +
 +$db = NewADOConnection($driver='mysqli');
 +
 +/*
 + * Enable memcache
 + */
 +$db->memCache = true;
 +
 +/*
 + * Add the weighted servers in $memCacheHosts. All other parameters are ignored.
 + */
 +$db->memCacheHost = $hosts
 + 
 +</code>
 +
 +===== Adding options to the memcached server =====
 +From ADOdb version 5.22, [[https://www.php.net/manual/en/memcached.setoption.php|server options]] can be set on the **Memcached** connection by use of the **memCacheOptions** array
 +
 +<code php>
 +
 +$hosts = array(
 +    array('host'=>'192.168.86.91','port'=>'11211','weight'=>70)
 +    array('host'=>'192.168.86.92','port'=>'11211','weight'=>30)
 +    );
 +
 +$db = NewADOConnection($driver='mysqli');
 +
 +/*
 + * Enable memcache
 + */
 +$db->memCache = true;
 +
 +/*
 + * Add the weighted servers in $memCacheHosts
 + */
 +$db->memCacheHost = $hosts;
 +
 +/*
 +* Set some options
 +*/
 +$db->memCacheOptions = array(Memcached::OPT_HASH=>Memcached::HASH_MURMUR);
 +</code>
 +
 ===== Using Debug Mode  ===== ===== Using Debug Mode  =====
-The size of query results that can be cached by the memcached server is limited by server configuration. The default is quite small. If the query is too large, and [[v5:reference:debug|debugging]] is enabled, then an error is displayed. 
  
 +The size of query results that can be cached by the memcached server is limited by server configuration. The default is quite small. If the query is too large, and [[v5:userguide:debug|debugging]] is enabled, then an error is displayed.
 +
 +<code>
 +3849805e4e026b9e7c485999a24a95c6 cache failure: Item with such key doesn't exists on the memcached server.
 +(this is a notice and not an error)
 +-----<hr>
 +
 +(mysqli): SELECT * FROM employees
 +
 +------<hr>
 +
 +Notice: MemcachePool::set(): Server 192.168.0.78 (tcp 11211, udp 0) failed with:
 +SERVER_ERROR object too large for cache
 +(3) in C:\dev\github\mssqlnative-fixes\adodb-memcache.lib.inc.php on line 94
 +Failed to save data at the memcached server!
 +
 +Cache write error
 +
 +PHP Notice:  MemcachePool::set(): Server 192.168.0.78 (tcp 11211, udp 0) failed with:
 +SERVER_ERROR object too large for cache (3)
 +in C:\dev\github\mssqlnative-fixes\adodb-memcache.lib.inc.php on line 94
 +</code>
  
-  3849805e4e026b9e7c485999a24a95c6 cache failure: Item with such key doesn't exists on the memcached server. 
-  (this is a notice and not an error) 
-  -----<hr> 
-   
-  (mysqli): SELECT * FROM employees  
-   
-  -----<hr> 
-  Notice: MemcachePool::set(): Server 192.168.0.78 (tcp 11211, udp 0) failed with: 
-  SERVER_ERROR object too large for cache 
-  (3) in C:\dev\github\mssqlnative-fixes\adodb-memcache.lib.inc.php on line 94 
-  Failed to save data at the memcached server! 
-   
-  Cache write error 
-   
-  PHP Notice:  MemcachePool::set(): Server 192.168.0.78 (tcp 11211, udp 0) failed with: 
-  SERVER_ERROR object too large for cache (3)  
-  in C:\dev\github\mssqlnative-fixes\adodb-memcache.lib.inc.php on line 94 
-   
 ADOdb currently has no mechanism for splitting large queries into smaller chunks. ADOdb currently has no mechanism for splitting large queries into smaller chunks.
  
  
 {{tag>[memcached cache]}} {{tag>[memcached cache]}}
 +
v5/userguide/memcached.1452998350.txt.gz · Last modified: 2017/04/21 11:39 (external edit)