AnewtDatabaseConnectionMemcache Class Reference

Database connection wrapper with Memcache result set caching. More...

List of all members.

Public Member Functions

 __construct ($settings)
 Create a new connection instance (internal use only).
 is_connected ()
 Check whether the connection is currently open.
 last_insert_id ($options=null)
 Get the last insert id for this connection.
 real_execute_sql ($sql)
 Execute SQL and create an AnewtDatabaseResultSet for a query.
 prepare_executev_fetch_one ($sql, $values=null)
 Execute a query using the values passed as a single parameter, and fetch the first row.
 prepare_executev_fetch_all ($sql, $values=null)
 Execute a query using the values passed as a single parameter, and fetch all rows.
 flush_cache ()
 Flush the contents of the query cache.
 escape_boolean ($value)
 Escape a boolean for use in SQL queries.
 escape_string ($value)
 Escape a string for use in SQL queries.
 escape_table_name ($value)
 Escape a table name for use in SQL queries.
 escape_column_name ($value)
 Escape a column name for use in SQL queries.
 escape_date ($value)
 Escape a date for use in SQL queries.
 escape_time ($value)
 Escape a time for use in SQL queries.
 escape_datetime ($value)
 Escape a datetime for use in SQL queries.

Public Attributes

 $n_cache_hits = 0
 The number of Memcache hits.
 $n_cache_misses = 0
 The number of Memcache misses.

Protected Member Functions

 real_connect ()
 Establish a database connection.
 real_disconnect ()
 Disconnect the database connection.

Private Member Functions

 build_key ($sql, $values, $all_rows)
 Construct a key to be used as a memcache key.

Private Attributes

 $memcache
 The Memcache instance.
 $memcache_connected
 Whether we are Memcache server is connected.


Detailed Description

Database connection wrapper with Memcache result set caching.

AnewtDatabaseConnectionMemcache is a wrapper around another database connection, providing a simple caching solution for SELECT queries. It is not supposed to be a fully featured database caching solution, but designed to work transparently in simple cases. Using AnewtDatabaseConnectionMemcache does not require any code changes, except for the database connection step. Just invoke AnewtDatabase::setup_connection() twice: one for the real connection, one for the cached connection.

Note that connect(), disconnect() and is_connected() only apply to the Memcache server connection, and do not propagate to the underlying database connection. Since connections to the Memcache server are only established if required, it usually does not make sense to connect() and disconnect() the connection manually.

How does it work? The rows returned from a SELECT query executed against a real database connection are temporarily stored in a Memcache cache. If the same query is executed again within a certain timespan (see expiry below), the exact same result is returned without hitting the database again. Note that this means that changes to the database while the data is in the cache are not immediately visible.

Result row caching only works if you execute your SELECT queries through one of these convenience functions:

Note that preparing and executing a query manually, i.e. by calling AnewtDatabaseConnection::prepare(), executing the returned AnewtDatabasePreparedQuery and then fetching rows with AnewtDatabaseResultSet::fetch_one() (or other fetch functions) will not result in caching!

Important note: if you change the contents of the database and want to see the results of e.g. INSERT queries reflected immediately if you perform a SELECT query right after the INSERT, you should not rely on AnewtDatabaseConnectionMemcache, but interact directly with the real AnewtDatabaseConnection, e.g. AnewtDatabaseConnectionMySQL. You can use both the AnewtDatabaseConnectionMemcache connection and the underlying AnewtDatabaseConnection at the same time. This way, you can opt to use the cache for some of your SELECT queries (when delays are not an issue), while you deliberately avoid it for other SELECT queries (if you want changes to be immediately visible).

Additionally, you can also delete all cached result sets by invoking AnewtDatabaseConnectionMemcache::flush_cache() directly.

The settings accepted by this backend are:

See also:
AnewtDatabase

AnewtDatabaseConnection

Definition at line 90 of file backend-memcache.lib.php.


Constructor & Destructor Documentation

AnewtDatabaseConnectionMemcache::__construct ( settings  ) 

Create a new connection instance (internal use only).

Do not call this method directly; it is for internal use only. See the AnewtDatabase documentation on how to setup and obtain AnewtDatabaseConnection instances.

Parameters:
$settings Associative array with connection settings.

Reimplemented from AnewtDatabaseConnection.

Definition at line 104 of file backend-memcache.lib.php.


Member Function Documentation

AnewtDatabaseConnectionMemcache::is_connected (  ) 

Check whether the connection is currently open.

Returns:
True if the connection is open, false otherwise.

Reimplemented from AnewtDatabaseConnection.

Definition at line 153 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::last_insert_id ( options = null  ) 

Get the last insert id for this connection.

The $options parameter is database backend specific. For most backends this value is not needed. At least PostgreSQL needs a sequence name. See the backend documentation for more information.

Parameters:
$options Backend-specific value.

Reimplemented from AnewtDatabaseConnection.

Definition at line 158 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::real_execute_sql ( sql  ) 

Execute SQL and create an AnewtDatabaseResultSet for a query.

This method is for internal use only and is backend-specific.

Parameters:
$sql The SQL query to execute.

Reimplemented from AnewtDatabaseConnection.

Definition at line 163 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::build_key ( sql,
values,
all_rows 
) [private]

Construct a key to be used as a memcache key.

Parameters:
$sql The SQL query
$values The values for the SQL query
$all_rows Whether this key is intended for single row or multiple row caching (boolean)
Returns:
A key that can be used as a memcache key.

Definition at line 181 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::prepare_executev_fetch_one ( sql,
values = null 
)

Execute a query using the values passed as a single parameter, and fetch the first row.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
A single row, or NULL
See also:
AnewtDatabaseConnection::prepare_execute_fetch_one

AnewtDatabaseConnection::prepare_executev

AnewtDatabaseConnection::prepare_executev_fetch_all

Reimplemented from AnewtDatabaseConnection.

Definition at line 191 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::prepare_executev_fetch_all ( sql,
values = null 
)

Execute a query using the values passed as a single parameter, and fetch all rows.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
Array of all rows (may be empty)
See also:
AnewtDatabaseConnection::prepare_execute_fetch_all

AnewtDatabaseConnection::prepare_executev

AnewtDatabaseConnection::prepare_executev_fetch_one

Reimplemented from AnewtDatabaseConnection.

Definition at line 226 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::flush_cache (  ) 

Flush the contents of the query cache.

This method can be used to flush the contents of the query cache. Note that this will delete all cached data from the Memcache server, i.e. including any data that has not been set from this connection!

Definition at line 269 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_boolean ( value  ) 

Escape a boolean for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 277 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_string ( value  ) 

Escape a string for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 282 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_table_name ( value  ) 

Escape a table name for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 287 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_column_name ( value  ) 

Escape a column name for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 292 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_date ( value  ) 

Escape a date for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 297 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_time ( value  ) 

Escape a time for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 302 of file backend-memcache.lib.php.

AnewtDatabaseConnectionMemcache::escape_datetime ( value  ) 

Escape a datetime for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented from AnewtDatabaseConnection.

Definition at line 307 of file backend-memcache.lib.php.


The documentation for this class was generated from the following file:

Generated on Sun Aug 2 22:54:37 2009 for Anewt by  doxygen 1.5.9