Working with Data Cache

In this Topic Hide

Disk/Shared-Memory Caching

'namespace' Support

Setting the cached 'namespace':

Cache Folder Depth Configuration

 

The Data Cache API is used the same way as any other API: By inserting the API functions into your PHP code. The Data Cache component uses an API to cache partial PHP outputs using memory or disk.

 

The Data Cache API includes the following functionality:

Disk/Shared-Memory Caching

This feature provides options to determine where to store cached variables. Memory caching improves server responsiveness and increases performance - primarily in environments that run high-traffic applications that can benefit from off loading activity directed toward their hard disk. Disk caching is more suitable for smaller applications and ensures the cached content is available after the machine is restarted.

 

SHM/disk storage is implemented by using the appropriate API functions and configuring the Data Cache directives.

Note:

Memory option error messages have been created to notify you if the store operation fails or you run out of allocated memory.

The following example shows the different storage options:

Usage Example

Example:

A simple key with no namespace stored on disk

if (zend_disk_cache_store("hello1", 1) === false){

    echo "error2\n";    exit();

}

Shared memory:

if (zend_shm_cache_store("hello1", 1) === false){

    echo "error2\n";    exit();

}

Store with namespace on disk

if (zend_disk_cache_store("ns1::hello1", 1) === false){

    echo "error2\n";    exit();

}

Shared memory:

if (zend_shm_cache_store("ns1::hello1", 1) === false){

    echo "error2\n";    exit();

}

 

Store with namespace on disk with limited lifetime (3)

if (zend_disk_cache_store("ns3::test_ttl", 2, 3) === false){

    echo "error12\n";    exit();

}

Shared memory:

if (zend_shm_cache_store("ns3::test_ttl", 2, 3) === false){

    echo "error12\n";    exit();

}

 

 

'namespace' Support

Using namespaces for caching provides the ability to define a key that can serve as an identifier to delete select items from the cache, rather than unnecessarily removing shared instances. 'namespace' support is intended for environments that run large applications that are separated into modules. Applying a 'namespace' to each module provides the identification necessary to pinpoint all the cached items that belong to a given module and remove only those specific items.

 

This does not mean that you must use the 'namespaces' to clear the cache: The entire cache can be cleared by using the 'output_cache_remove' function.

Setting the cached 'namespace':

The cache 'namespace' is set by adding it as a prefix to the cache with '::' as the separator.

Usage Example

Example:

This example shows how to manipulate variable caching using a 'namespace'

zend_disk_cache_store("my_namespace::my_key",$data) is fetched with zend_disk_cache_fetch("my_namespace::my_key");

zend_shm_cache_clear("my_namespace") clears all the keys that start with "my_namespace::"

 

Cache Folder Depth Configuration

Defining the Cache folder depth is intended for environments that use a large number of keys. By definition, cached content is separated into different directories by key, to prevent performance degradation caused by accessing files that contain large amounts of content. This option is only available with disk caching. Increase the cache folder depth according to the quantity of content that requires caching (small amount = 0, large quantities = 2).

Note:

A single directory may include several keys, depending on the quantity of cached content.

The cache folder depth is defined by the directive zend_cache.disk.dir_levels. The value of the directive configures how the cached files are stored. The accepted values for this directive are 0, 1 or 2, where:

0 = one directory containing all the cache files

1 = a separate directory under the cache directory

2 = an additional sub directory for cached content under the cache directory

 

 

Related Links

Related Links:

Working with Components

Data Cache

Working with Page Caching