phpBB

Development Wiki

PhpBB3.1/RFC/Cache Refactoring

From phpBB Development Wiki

< PhpBB3.1‎ | RFC

Description

Refactoring the cache system to be more flexible, testable and cleaner.

Motivation

The current cache system is a mess. The cache modules declare a single 'acm' class, which is then extended by the 'cache' class, adding methods to retrieve and cache certain data. 'acm' is a non-descriptive name which does not seem to be defined.

By refactoring we can make it possible to test different cache implementations, and in theory use different ones for different things. We can make code that depends on cache cleaner, by allowing a driver to be injected.

Usage

We encourage passing the cache driver into new classes that perform caching instead of using the global cache service ($cache).

Service

The cache service acts as a container and wrapper for the cache driver and adds methods for retrieving specific data, such as smilies. The main purpose is to provide backward-compatibility. In most cases you should use the driver directly instead, you can do that by calling:

$driver $cache->get_driver();

Driver

A driver must implement the phpbb_cache_driver_interface. The only methods you will ever need are:

Get a value from the cache

$value $driver->get('key');

Store something in the cache for one hour

$driver->put('key''value'3600);

Purge the cache entirely

$driver->purge();

Implementation

Each driver has it's own class name and extends a common base class. 'cache' is renamed to 'phpbb_cache_service' and no longer extends the driver. Instead it takes a driver as an argument and uses __call to remain backward-compatible with old code.

It makes use of autoloading.

Notes

The cache drivers still do way more than they should (database-related things), but it's a move in the right direction.