This example shows how to create and use a simple cache with
ezcCacheManager:
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $options = array(
6. 'ttl' => 30,
7. );
8.
9. ezcCacheManager::createCache( 'simple', '/tmp/cache/plain', 'ezcCacheStorageFilePlain', $options );
10.
11. $myId = 'unique_id_1';
12. $mySecondId = 'id_2';
13.
14. $cache = ezcCacheManager::getCache( 'simple' );
15.
16. if ( ( $dataOfFirstItem = $cache->restore( $myId ) ) === false )
17. {
18. $dataOfFirstItem = "Plain cache stored on " . date( 'Y-m-d, H:i:s' );
19. $cache->store( $myId, $dataOfFirstItem );
20. }
21.
22. if ( ( $dataOfSecondItem = $cache->restore( $mySecondId ) ) === false )
23. {
24. $dataOfSecondItem = "Plain cache 2 stored on " . date( 'Y-m-d, H:i:s' );
25. $cache->store( $mySecondId, $dataOfSecondItem );
26. }
27.
28. var_dump( $dataOfFirstItem, $dataOfSecondItem );
29.
30. ?>
Time-to-live is defined as 30 seconds in this case; if left
out, the cache will have a lifespan of 24 hours. On line 9, the cache
configuration is stored in the cache
manager. The created cache uses the cache identifier "simple" and will reside in the directory
/tmp/cache/plain. (Note: This directory must exist and must be writable!) To store a cache item, the storage
class ezcCacheStorageFilePlain will be used.
Line 11 defines a cache key for a cache item. The next line defines a
second unique cache key for a second cache item. After that (line 14), the
newly-created cache object is retrieved from ezcCacheManager. Lines 16 and
22 show how to check for cached data: ezcCacheStorage::restore()
will return bool false if no valid cache data is found for the given ID.
If no valid data is found, the data will be generated
and stored in the cache later (lines 17-18 and 24-25). The last line outputs the
data, so you can follow how it's cached for 30 seconds by running the
example multiple times in a short time frame. After 30 seconds, the cache data
will become invalid and will be regenerated.
Instead of calling the ezcCacheManager::getCache() method yourself it is also
possible to use delayed initialization. When using this it is not required to
configure all the caches first by calling the ezcCacheManager::createCache()
method. Instead it would allow you to configure/create caches on demand, this
is called lazy, or delayed initialization. You can find a description how you
can use it for your own components and how it works in the ezcBase
tutorial. The keyword for the cache component is
ezcInitConfigurationManager.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. class customLazyCacheConfiguration implements ezcBaseConfigurationInitializer
6. {
7. public static function configureObject( $id )
8. {
9. $options = array( 'ttl' => 30 );
10.
11. switch ( $id )
12. {
13. case 'simple':
14. ezcCacheManager::createCache( 'simple', '/tmp/cache/plain', 'ezcCacheStorageFilePlain', $options );
15. break;
16. }
17. }
18. }
19.
20. ezcBaseInit::setCallback(
21. 'ezcInitCacheManager',
22. 'customLazyCacheConfiguration'
23. );
24.
25. $myId = 'unique_id_1';
26. $mySecondId = 'id_2';
27.
28. $cache = ezcCacheManager::getCache( 'simple' );
29.
30. if ( ( $dataOfFirstItem = $cache->restore( $myId ) ) === false )
31. {
32. $dataOfFirstItem = "Plain cache stored on " . date( 'Y-m-d, H:i:s' );
33. $cache->store( $myId, $dataOfFirstItem );
34. }
35.
36. if ( ( $dataOfSecondItem = $cache->restore( $mySecondId ) ) === false )
37. {
38. $dataOfSecondItem = "Plain cache 2 stored on " . date( 'Y-m-d, H:i:s' );
39. $cache->store( $mySecondId, $dataOfSecondItem );
40. }
41.
42. var_dump( $dataOfFirstItem, $dataOfSecondItem );
43.
44. ?>
Differences with the previous example can be seen in lines 5 to 23. In lines 20
to 23 you tell the delayed initialization mechanism to use the
customLazyCacheConfiguration as lazy initialization provider for the
ezcInitConfigurationManager context. The customLazyCacheConfiguration class
implements the configureObject() method that will automatically be called when
ezcCacheManager::getCache() is called with a cache identifier that has not been
created yet through ezcCacheManager::createCache(), as you can see in line 14.
The following example shows how the cache manager deals with multiple caches:
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $optionsPlain = array(
6. 'ttl' => 30,
7. );
8. $optionsArray = array(
9. 'ttl' => 45,
10. );
11.
12. ezcCacheManager::createCache( 'plain', '/tmp/cache/plain', 'ezcCacheStorageFilePlain', $optionsPlain );
13. ezcCacheManager::createCache( 'array', '/tmp/cache/array', 'ezcCacheStorageFileArray', $optionsArray );
14.
15. $myId = 'unique_id_2';
16.
17. $cache = ezcCacheManager::getCache( 'plain' );
18.
19. if ( ( $plainData = $cache->restore( $myId ) ) === false )
20. {
21. $plainData = "Plain cache stored on " . date( 'Y-m-d, H:m:s' );
22. $cache->store( $myId, $plainData );
23.
24. sleep( 2 );
25. }
26.
27. echo "Plain cache data:\n";
28. var_dump( $plainData );
29.
30. $cache = ezcCacheManager::getCache( 'array' );
31.
32. if ( ( $arrayData = $cache->restore( $myId ) ) === false )
33. {
34. $arrayData = array(
35. $plainData,
36. "Array cache stored on " . date( 'Y-m-d, H:m:s'),
37. true,
38. 23
39. );
40. $cache->store( $myId, $arrayData );
41. }
42.
43. echo "Array cache data:\n";
44. var_dump( $arrayData );
45.
46. ?>
In lines 12 and 13, two caches are created. Each cache must
reside in its own location and must have a different cache identifier. We
use two different options for the lifetime of the caches to show how they act
independently.
Since the first cache reuses the location already used in example 1,
we use a different cache key here.
Lines 15 to 25 are almost identical to the code from example 1,
except that the program will pause for two seconds when generating
the plain cache, in order to show different generation times for the two
caches.
On line 30, the second cache object is retrieved, which is capable of storing
arrays. Therefore, we store the data from the plain cache here and
generate some additional data to be stored in an array. Running this
example multiple times will give you different results
since the second cache has a longer lifetime and will therefore hold its data
longer than the first one.
As the next example shows, the ezcCacheStorage class is capable of more advanced
features. This example uses extra attributes in addition to the cache key:
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $options = array(
6. 'ttl' => 30,
7. );
8.
9. ezcCacheManager::createCache( 'array', '/tmp/cache/array', 'ezcCacheStorageFileArray', $options );
10.
11. $exampleData = array(
12. 'unique_id_3_a' => array( 'language' => 'en', 'section' => 'articles' ),
13. 'unique_id_3_b' => array( 'language' => 'de', 'section' => 'articles' ),
14. 'unique_id_3_c' => array( 'language' => 'no', 'section' => 'articles' ),
15. 'unique_id_3_d' => array( 'language' => 'de', 'section' => 'tutorials' ),
16. );
17.
18. $cache = ezcCacheManager::getCache( 'array' );
19.
20. foreach ( $exampleData as $myId => $exampleDataArr )
21. {
22. if ( ( $data = $cache->restore( $myId, $exampleDataArr ) ) === false )
23. {
24. $cache->store( $myId, $exampleDataArr, $exampleDataArr );
25. }
26. }
27.
28. echo "Data items with attribute <section> set to <articles>: " .
29. $cache->countDataItems( null, array( 'section' => 'articles' ) ) .
30. "\n";
31.
32. echo "Data items with attribute <language> set to <de>: " .
33. $cache->countDataItems( null, array( 'language' => 'de' ) ) .
34. "\n\n";
35.
36. $cache->delete( null, array( 'language' => 'de' ) );
37.
38. echo "Data items with attribute <section> set to <articles>: " .
39. $cache->countDataItems( null, array( 'section' => 'articles' ) ) .
40. "\n";
41.
42. echo "Data items with attribute <language> set to <de>: " .
43. $cache->countDataItems( null, array( 'language' => 'de' ) ) .
44. "\n\n";
45.
46. ?>
After the creation of an array cache, some sample data is created (lines
11-16). Data is identified by cache key s, which are associated with
arrays. Each array will be used to store the content and the attributes
together. Attribute s describe a cache item s in further detail.
In line 20, a foreach loop starts, which stores all example data in the cache.
After that, the method ezcCacheStorageFile::countDataItems() is used to count
cache items that meet certain criteria. The first parameter here would be a
cache key.
When this is set, the method should always return 1 or 0, because only one cache
item per cache key may exist. In this example, the cache items with the specified attribute are
counted. The attributes to match are supplied as the second parameter. The
first method call will return 3 (line 28), since we have three cache items that
have the attribute "section" set to "articles". The second call (line 32)
should return 2, because two data items have the attribute "language" set to the
value "de".
On line 36 the storage object is told to delete all cache items that have
the attribute "language" set to "de". Therefore, the next calls to
ezcCacheStorageFile::countDataItems() will return 2 and 0.
If either of Memcache or APC PHP extensions is installed, then the caching
performance can be improved considerably by storing the cache data in memory
between requests.
There are 2 implementations of APC provided: ezcCacheStorageApcPlain for
storing cache objects in memory, and ezcCacheStorageFileApcArray for storing
arrays in memory with file fall-back. The ezcCacheStorageFileApcArray basically
replaces the ezcCacheStorageFileArray if the APC PHP extension is installed.
The following example shows how to create and use a plain APC storage. It is
the same as the simple example above, but using the ezcCacheStorageApcPlain
class instead of ezcCacheStorageFilePlain. The second parameter of the
createCache() method is null, because an existing path is not needed.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $options = array(
6. 'ttl' => 30,
7. );
8.
9. ezcCacheManager::createCache( 'simple', null, 'ezcCacheStorageApcPlain', $options );
10.
11. $myId = 'unique_id_1';
12. $mySecondId = 'id_2';
13.
14. $cache = ezcCacheManager::getCache( 'simple' );
15.
16. if ( ( $dataOfFirstItem = $cache->restore( $myId ) ) === false )
17. {
18. $dataOfFirstItem = "Plain cache stored on " . date( 'Y-m-d, H:i:s' );
19. $cache->store( $myId, $dataOfFirstItem );
20. }
21.
22. if ( ( $dataOfSecondItem = $cache->restore( $mySecondId ) ) === false )
23. {
24. $dataOfSecondItem = "Plain cache 2 stored on " . date( 'Y-m-d, H:i:s' );
25. $cache->store( $mySecondId, $dataOfSecondItem );
26. }
27.
28. var_dump( $dataOfFirstItem, $dataOfSecondItem );
29.
30. ?>
The following example shows how to create and use an APC array storage. It is
the same as the second example above, but using the ezcCacheStorageFileApcArray
class instead of ezcCacheStorageFileArray. The second parameter of the
createCache() method must be an existing writeable path.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $options = array(
6. 'ttl' => 45,
7. );
8.
9. ezcCacheManager::createCache( 'array', '/tmp/cache/array', 'ezcCacheStorageFileApcArray', $optionsArray );
10.
11. $cache = ezcCacheManager::getCache( 'array' );
12.
13. if ( ( $arrayData = $cache->restore( $myId ) ) === false )
14. {
15. $arrayData = array(
16. $plainData,
17. "Array cache stored on " . date( 'Y-m-d, H:m:s'),
18. true,
19. 23
20. );
21. $cache->store( $myId, $arrayData );
22. }
23.
24. echo "Array cache data:\n";
25. var_dump( $arrayData );
26.
27. ?>
Certain options must be set before creating a Memcache cache storage. See
ezcCacheStorageMemcacheOptions for more information.
- host
- The name of the host running Memcache. Normally this is localhost.
- port
- The port on which to connect to the Memcache host. Normally it is 11211.
- persistent
- If creating a persistent connection to the Memcache server or not. A
persistent connection does not close when the script ends. By default this is
false.
- compressed
- If the data is to be compressed in the cache. The zlib extension is required.
The following example shows how to create and use a plain Memcache storage. It
is the same as the simple example above, but using the
ezcCacheStorageMemcachePlain class instead of ezcCacheStorageFilePlain, and
setting the required options for the storage. The second parameter of the
createCache() method is null, because an existing path is not needed.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $options = array(
6. 'ttl' => 30,
7. 'host' => 'localhost',
8. 'port' => 11211
9. );
10.
11. ezcCacheManager::createCache( 'simple', null, 'ezcCacheStorageMemcachePlain', $options );
12.
13. $myId = 'unique_id_1';
14. $mySecondId = 'id_2';
15.
16. $cache = ezcCacheManager::getCache( 'simple' );
17.
18. if ( ( $dataOfFirstItem = $cache->restore( $myId ) ) === false )
19. {
20. $dataOfFirstItem = "Plain cache stored on " . date( 'Y-m-d, H:i:s' );
21. $cache->store( $myId, $dataOfFirstItem );
22. }
23.
24. if ( ( $dataOfSecondItem = $cache->restore( $mySecondId ) ) === false )
25. {
26. $dataOfSecondItem = "Plain cache 2 stored on " . date( 'Y-m-d, H:i:s' );
27. $cache->store( $mySecondId, $dataOfSecondItem );
28. }
29.
30. var_dump( $dataOfFirstItem, $dataOfSecondItem );
31.
32. ?>