This is the base class of the MidCOM Cache backend infrastructure.

It provides a general interface for the caching services by encapsulating the calls specific to the data storage interface.

Each cache database in use is encapsulated by its own instance of this service, identified by their name and the handler which controls it. The name must be unique throughout the entire server. See Namespacing below for details.

A handlers type is identified by its class name, so midcom_sercices_cache_backend_dba is the DBA handler from the original pre 2.4 MidCOM.

Inter-Process synchronization:

Backend drivers have to ensure they can be accessed concurrently to ensure Database integrity.

Resource handles:

Resource handles (for example for DBA access) should be closed if necessary if they would block other processes. It is unknown to me if such handles would be safe to use over several requests.

Namespacing:

Each cache database in use has a name, which must consist only of characters valid for file names on the current system. You may create any file or directory within the midcom cache directory as long as you use your name as a prefix.

If you want to stay on the safe side, only cache names using the characters matching the regex class [a-zA-Z0-9._-] should be used.

General configuration directives:

  • string directory: The subdirectory in the cache's base directory to use by this backend. This is automatically concatenated with the systemwide cache base directory.
  • string driver: The concrete class instance to create. It must match the name of a file within the backend directory. Note, that the class must also be named accordingly, the driver "dba" is searched in the file "backend/dba.php" and must be of the type "midcom_services_cache_backend_dba".
  • boolean auto_serialize: Set this to true to enable automatic serialization on storage and/or retrieval. Disabled by default.
package midcom.services

 Methods

_close ()

Close the database that has been opened previously with _open().

_exists (string $key)

Checks, whether the given key exists in the Database.

The data store is opened either read-only or read-write when this function executes.

Parameters

$key

stringThe key to check for.

Returns

booleanIndicating existence.

_get (string $key)

Get the data associated with the given key.

The data store is opened either read-only or read-write when this function executes.

Parameters

$key

stringKey to look up.

Returns

string$data The data associated with the key.

_on_initialize ()

Backend initialization

Add any custom startup code here. The configuration variables are all initialized when this handler is called.

_on_shutdown ()

Backend shutdown

Called, if the backend is no longer used.

_open (boolean $write)

Open the database for usage.

If $write is set to true, it must be opened in read/write access, otherwise read-only access is sufficient.

If the database cannot be opened, midcom_error should be thrown.

The concrete subclass must track any resource handles internally, of course.

Parameters

$write

booleanTrue, if read/write access is required.

_put (string $key, string $data)

Store the given key/value pair, any existing entry with the same key has to be silently overwritten.

The data store is opened in read-write mode when this function executes.

Any error condition should throw midcom_error and must close the data store before doing so.

Parameters

$key

stringThe key to store at.

$data

stringThe data to store.

_remove (string $key)

Delete the data with the given key from the database.

The data store is opened in read-write mode when this function executes.

Deleting non existent keys should fail silently. All other error conditions should throw midcom_error and must close the data store before doing so.

Parameters

$key

stringThe key to delete.

_remove_all ()

Drops the entire database, preferably with some kind of truncate operation.

The data store will not be opened in either read-only or read-write mode when this function executes, to allow for open/truncate operations.

Any error condition should throw midcom_error

close ()

Close the database that has been opened previously with open().

If the database is already closed, the request is ignored silently.

exists (string $key)

Checks, whether the given key exists in the Database.

If the data store has not yet been opened for reading, it will be opened automatically prior to the call, and closed automatically again afterwards.

Parameters

$key

stringThe key to check for.

Returns

booleanIndicating existence.

flush_unsynced ()

flush all unsynced changes to backend

get (string $key)

Get the data associated with the given key.

If the data store has not yet been opened for reading, it will be opened automatically prior to the call, and closed automatically again afterwards.

Parameters

$key

stringKey to look up.

Returns

string$data The data associated with the key.

initialize (string $name, array $config)

Initializes the backend by acquiring all necessary information required for runtime.

After base class initialization, the event handler _on_initialize is called, in which all backend specific stuff should be done.

Parameters

$name

stringThe name ("identifier") of the handler instance.

$config

arrayThe configuration to use.

open (boolean $write)

Open the database for usage.

If $write is set to true, it must be opened in read/write access, otherwise read-only access is sufficient.

If the database is reopened with different access permissions then currently specified (e.g. if going from read-only to read-write), the database is closed prior to opening it again. If the permissions match the current state, nothing is done.

Parameters

$write

booleanTrue, if read/write access is required.

put (string $key, string $data)

Store the given key/value pair, any existing entry with the same key has to be silently overwritten.

If the data store has not yet been opened for writing, it will be opened automatically prior to the call, and closed automatically again afterwards.

Parameters

$key

stringThe key to store at.

$data

stringThe data to store.

remove (string $key)

Delete the data with the given key from the database.

Deleting non existent keys should fail silently. If the data store has not yet been opened for writing, it will be opened automatically prior to the call, and closed automatically again afterwards.

Parameters

$key

stringThe key to delete.

remove_all ()

Drops the entire database and creates an empty one.

The database must not be opened by this process when this is called. If it is, it will be automatically closed prior to executing this call.

Any error condition should throw midcom_error.

shutdown ()

Shutdown the backend.

This calls the corresponding event.

_check_cache_dir ()

This helper will ensure that the cache base directory is created and usable by checking it is actually a directory.

If it does not exist, it will be created automatically. Errors will be handled by midcom_error.

 Properties

 

boolean $_auto_serialize

Set this to true if you plan to store PHP data structures rather then strings, the interface will automatically serialize/unserialize the data you store/retrieve from the database.
 

string $_cache_dir

The base directory in which we may add files and directories within our namespace.
 

Array $_config

Configuration key array.

This is populated during initialize().

 

string $_name

The backend instance name.

This variable my not be written to after the instance has been created.

 

array $_local_cache

cache of objects, which were already requested from backend.
 

boolean $_open_for_reading

True, if the database has been opened for reading previously.

This is also true, if we are in read-write mode, naturally.

Therefore, this flag is also used for checking whether the database is open in general.

 

boolean $_open_for_writing

True, if the database has been opened for writing previously.
 

array $_unsynced_keys

array of ('key' => true) pairs, listing keys, which have to be synced with backend-implementation