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. | |
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:
connection: The existing AnewtDatabaseConnection to wraphostname: The hostname of the memcache server (optional, defaults to localhost)port: The port number of the memcache server (optional, defaults to 11211)socket: Path to a Unix domain socket (optional). Instead of a network connection, you can also provide a path to a Unix socket where memcached listens. If this is specified, hostname and port are ignored.expiry: The number of seconds to cache result sets (optional, defaults to 600)identifier: An application-specific identifier (optional). Use this to avoid collisions if two instances of the same application use the same Memcache server but you do not want them to share their cached values. In this case, you should specify an identifier value that is unique to this application instance.compression: Whether to enable compression (optional, defaults to false)
Definition at line 90 of file backend-memcache.lib.php.
| 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.
| $settings | Associative array with connection settings. |
Reimplemented from AnewtDatabaseConnection.
Definition at line 104 of file backend-memcache.lib.php.
| AnewtDatabaseConnectionMemcache::is_connected | ( | ) |
Check whether the connection is currently open.
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.
| $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.
| $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.
| $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) |
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.
| $sql | The SQL query to be prepared (optionally with placeholders) | |
| $values | Zero or more values to be substituted for the placeholders |
NULL 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.
| $sql | The SQL query to be prepared (optionally with placeholders) | |
| $values | Zero or more values to be substituted for the placeholders |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
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.
| $value | The value to escape |
Reimplemented from AnewtDatabaseConnection.
Definition at line 307 of file backend-memcache.lib.php.
1.5.9