ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v6:caching

V6 Caching Service

The caching service requires PHP 7.4 or higher

Overview

The caching service provides access to various services that provide results caching, to reduce the overhead of making repetitive database queries for data. The most obvious examples of this are static cross-reference code → description pairs. Caching services allow the storing of the results of these queries in shared, direct access memory locations. Optionally, the query results can be time limited to allow for expiration and re-reading of the results. In its most simplest form, the process is as follows.

  • Prepare a query statement, and pass to caching function
  • Caching service checks to see if the result set is already cached
  • If not, execute the query on the database
  • Store the result set in the specified memory location
  • Set the timeout for result expiry

Supported Caching Methods

  • Disk Based caching [V5 Compatible]. This client benefits substantially by being located on a SSD disk if available.
  • Memcache server using the memcached client. This client attaches to a Memcache server and is enhanced to support weighted groups, server keys and client options such prefix keys and hashing options. This client cannot be used on windows client machines.
  • Memcache server using the memcache client. This client is enhanced to support weighted groups, compression thresholds as well as the full range of connection options such as failure callbacks.This client can be used on Windows systems.
  • The Redis client. This version of the client includes support for a single server connection using Auth methods, database selection and persistent connections.
  • The Redis Cluster caching client. This version allows connections to a pre-configured Redis Cluster
  • The APCu client
  • The WinCache client. This is a Windows only caching client
  • The Yac client

Prerequisits

The feature requires a PSR compliant auto-loader. The feature exist in the ADOdb namespace.

Incompatibilities

The feature is incompatible with any custom coding that uses the globals $ADODB_CACHE,$ADODB_CACHE_DIR or $ADODB_CACHE_CLASS,

Upgrading from Version 5

Access to new run time parameters is achieved by appending the Cache Data Object to the parameter list when calling one of the cacheExecute type methods. The object is described below.

The cache data Object

The object is the same across all commands that accept the use of cached data selection. If passed to cache methods that cannot use it, it is ignored. In its simplest form, the object can look like this:

$obj = new stdClass;
$obj->cache = true;

This provides a shortcut to a cached connection, using the default TTL and connection options. This format works across all configured servers. The default TTL is defined in the ** ADOCachingDefinitions ** class.

Expanding The Object

In its expanded usage, the cache data object holds the configuration items available to the Caching service; The cache expiry time and, if using a memcached service, an optional grouping key.

$obj = new stdClass;
$obj->cache = array('ttl' => 2400,serverKey => 'adodb');
 

Order Of Importance

TTL can be defined in multiple ways. The order of importance, if multiple methods are available at the same time, is as follows, from most to least:

  1. Cache seconds when passed as the first argument to the cache* function
  2. The ADOConnection::secs2cache class variable
  3. The cacheDataObject::ttl class variable

Supported Methods

Each of the services have different options available. You should view each page to see them.

Service Description
The Filesystem based Caching Plugin Filesystem based caching
The Memcache Caching Plugin Attaching to the Memcache server using the memcache library
The Memcached Caching Plugin Attaching to the Memcache server using the memcached library
The Redis Caching Plugin Attaching to an individual Redis server
The Redis Cluster Caching Plugin Attaching to a Redis Cluster
The APCu Caching Plugin APCu
The WinCache Caching Plugin WinCache
The Yac Caching Plugin Yac

The ADOCache Definitions File

To use this, copy it or instantiate the file that matches the defined caching server before setting the required entry. Pass the resulting class into the ** ADOCacheMethods ** class. Each caching method has a different set of named parameters


'' \ADOdb\addins\cache\plugins\<cachemethod>\ADOCacheDefinitions.php''


Example Using Memcached

/*
* Create an ADOdb connection
*/
$db = NewADOConnection('mssqlnative');
$db->connect($host,$user,$password,$database);
 
 
/*
* creating a logging group for the caching service
*/
$loggingDefinition = new ADOdb\addins\logger\builtin\ADOloggingDefinitions;
$loggingDefinition->textFile = '/home/adodb/cache.log';
$loggingDefinition->loggingTag = 'ADODB-CACHE';
 
$loggingObject = new ADOdb\logger\ADOlogger($loggingDefinition);
 
/*
* Create a weighted group of memcache servers
*/
$servers = array(array('host'=>'192.68.0.85','port'=>'11261','weight'=>50),
                array('host'=>'192.68.0.86','port'=>'11161','weight'=>30),
                array('host'=>'192.68.0.75','port'=>'11261','weight'=>20));
 
$memcacheServers = new \ADOdb\addins\cache\plugins\memcached\ADOCacheDefinitions;
 
$memcacheServers->memCacheControllers = $servers;
 
 
$memcacheServers->loggingObject = $loggingObject;
 
/*
* Create an object that links the database connection and the caching object. 
*/
$obj = new ADOdb\addins\cache\ADOCacheMethods($db,$memcacheServers);
 
/*
* Now force ADOdb version 5 to use the V6 Caching services instead of the built in options
*/
$ADODB_CACHE = $obj->cachingObject;
 
/*
* Executing a cached query using the named server key
*/
 
$cacheObject = new \stdClass;
$cacheObject->cache = array(
    'cachesecs'=>3600,
    'serverkey'=>'LOOKUPSERVER'
    );
 
$lookups = $db->cacheGetAssoc($SQL,null,false,false,$cacheObject);

The cacheInfo() method

The cacheInfo() method returns an associative array of service specific statistics associated with the driver used

/*
* Example from the memcached driver
*/
 
$servers = array(array('host'=>'192.68.86.91','port'=>'11211','weight'=>50));
 
$memcacheServers = new \ADOdb\addins\cache\plugins\memcached\ADOCacheDefinitions;
 
$memcacheServers->memCacheControllers = $servers;
 
 
/*
* Create an object that links the database connection and the caching object. 
*/
$obj = new ADOdb\addins\cache\ADOCacheMethods($db,$memcacheServers);
 
/*
* Gets the Stats
*/
 
print_r($obj=>cacheInfo())
 
/*
* Prints
Array
(
    [192.168.86.91:11211] => Array
        (
            [pid] => 1127
            [uptime] => 25095
            [time] => 1615442807
            [version] => 1.4.4
            [pointer_size] => 64
            [rusage_user] => 1.3098
            [rusage_system] => 0.52292
            [curr_connections] => 10
            [total_connections] => 21
            [connection_structures] => 11
            [cmd_get] => 22
            [cmd_set] => 1
            [cmd_flush] => 0
            [get_hits] => 21
            [get_misses] => 1
            [delete_misses] => 0
            [delete_hits] => 0
            [incr_misses] => 0
            [incr_hits] => 0
            [decr_misses] => 0
            [decr_hits] => 0
            [cas_misses] => 0
            [cas_hits] => 0
            [cas_badval] => 0
            [auth_cmds] => 0
            [auth_errors] => 0
            [bytes_read] => 1862
            [bytes_written] => 21316
            [limit_maxbytes] => 67108864
            [accepting_conns] => 1
            [listen_disabled_num] => 0
            [threads] => 4
            [conn_yields] => 0
            [bytes] => 946
            [curr_items] => 1
            [total_items] => 1
            [evictions] => 0
        )
 */
v6/caching.txt · Last modified: by 127.0.0.1