Cache/Backend/File.php
Zend Framework
LICENSE
This source file is subject to the new BSD license that is bundled with this package in the file LICENSE.txt. It is also available through the world-wide-web at this URL: http://framework.zend.com/license/new-bsd If you did not receive a copy of the license and are unable to obtain it through the world-wide-web, please send an email to [email protected] so we can send you a copy immediately.
- Category
- Zend
- Copyright
- Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- License
- New BSD License
- Package
- Zend_Cache
- Subpackage
- Zend_Cache_Backend
- Version
- $Id: File.php 24844 2012-05-31 19:01:36Z rob $
\Zend_Cache_Backend_File
- Implements
- \Zend_Cache_Backend_ExtendedInterface
- Parent(s)
- \Zend_Cache_Backend
- Copyright
- Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- License
- New BSD License
Properties

array $_directives = array('lifetime' => 3600, 'logging' => false, 'logger' => null)Frontend or Core directives
<p>directives</p>Inherited from: \Zend_Cache_Backend::$$_directives=====> (int) lifetime : - Cache lifetime (in seconds) - If null, the cache is valid forever
=====> (int) logging : - if set to true, a logging is activated throw Zend_Log
array('lifetime' => 3600, 'logging' => false, 'logger' => null)Details- Type
- array
- Inherited_from
- \Zend_Cache_Backend::$$_directives

array $_metadatasArray = array()Array of metadatas (each item is an associative array)
array()Details- Type
- array

array $_options = array('cache_dir' => null, 'file_locking' => true, 'read_control' => true, 'read_control_type' => 'crc32', 'hashed_directory_level' => 0, 'hashed_directory_perm' => 448, 'file_name_prefix' => 'zend_cache', 'cache_file_perm' => 384, 'metadatas_array_max_size' => 100)Available options
<p>available options</p>=====> (string) cache_dir : - Directory where to put the cache files
=====> (boolean) file_locking : - Enable / disable file_locking - Can avoid cache corruption under bad circumstances but it doesn't work on multithread webservers and on NFS filesystems for example
=====> (boolean) read_control : - Enable / disable read control - If enabled, a control key is embeded in cache file and this key is compared with the one calculated after the reading.
=====> (string) read_control_type : - Type of read control (only if read control is enabled). Available values are : 'md5' for a md5 hash control (best but slowest) 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) 'adler32' for an adler32 hash control (excellent choice too, faster than crc32) 'strlen' for a length only test (fastest)
=====> (int) hashed_directory_level : - Hashed directory level - Set the hashed directory structure level. 0 means "no hashed directory structure", 1 means "one level of directory", 2 means "two levels"... This option can speed up the cache only when you have many thousands of cache file. Only specific benchs can help you to choose the perfect value for you. Maybe, 1 or 2 is a good start.
=====> (int) hashed_directory_umask : - deprecated - Permissions for hashed directory structure
=====> (int) hashed_directory_perm : - Permissions for hashed directory structure
=====> (string) file_name_prefix : - prefix for cache files - be really carefull with this option because a too generic value in a system cache dir (like /tmp) can cause disasters when cleaning the cache
=====> (int) cache_file_umask : - deprecated - Permissions for cache files
=====> (int) cache_file_perm : - Permissions for cache files
=====> (int) metatadatas_array_max_size : - max size for the metadatas array (don't change this value unless you know what you are doing)
array('cache_dir' => null, 'file_locking' => true, 'read_control' => true, 'read_control_type' => 'crc32', 'hashed_directory_level' => 0, 'hashed_directory_perm' => 448, 'file_name_prefix' => 'zend_cache', 'cache_file_perm' => 384, 'metadatas_array_max_size' => 100)Details- Type
- array
Methods

___expire(string $id) : voidPUBLIC METHOD FOR UNIT TESTING ONLY !
Force a cache record to expire
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |

__construct(array $options = array()) : voidConstructor
| Name | Type | Description |
|---|---|---|
| $options | array | associative array of options |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

_clean(string $dir, string $mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = array()) : booleanClean some cache records (protected method used for recursive stuff)
Available modes are : Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags ($tags can be an array of strings or a single string)
| Name | Type | Description |
|---|---|---|
| $dir | string | Directory to clean |
| $mode | string | Clean mode |
| $tags | array | Array of tags |
| Type | Description |
|---|---|
| boolean | True if no problem |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

_delMetadatas(string $id) : booleanDrop a metadata record
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| boolean | True if no problem |

_expireTime( $lifetime) : intCompute & return the expire time
| Name | Type | Description |
|---|---|---|
| $lifetime |
| Type | Description |
|---|---|
| int | expire time (unix timestamp) |

_file(string $id) : stringMake and return a file name (with path)
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| string | File name (with path) |

_fileGetContents(string $file) : stringReturn the file content of the given file
| Name | Type | Description |
|---|---|---|
| $file | string | File complete path |
| Type | Description |
|---|---|
| string | File content (or false if problem) |

_fileNameToId(string $fileName) : stringTransform a file name into cache id and return it
| Name | Type | Description |
|---|---|---|
| $fileName | string | File name |
| Type | Description |
|---|---|
| string | Cache id |

_filePutContents(string $file, string $string) : booleanPut the given string into the given file
| Name | Type | Description |
|---|---|---|
| $file | string | File complete path |
| $string | string | String to put in file |
| Type | Description |
|---|---|
| boolean | true if no problem |

_getMetadatas(string $id) : array | falseGet a metadatas record
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| array | false | Associative array of metadatas |

_hash(string $data, string $controlType) : stringMake a control key with the string containing datas
| Name | Type | Description |
|---|---|---|
| $data | string | Data |
| $controlType | string | Type of control 'md5', 'crc32' or 'strlen' |
| Type | Description |
|---|---|
| string | Control key |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

_idToFileName(string $id) : stringTransform a cache id into a file name and return it
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| string | File name |

_isGoodTmpDir(string $dir) : booleanVerify if the given temporary directory is readable and writable
Inherited from: \Zend_Cache_Backend::_isGoodTmpDir()| Name | Type | Description |
|---|---|---|
| $dir | string | temporary directory |
| Type | Description |
|---|---|
| boolean | true if the directory is ok |

_isMetadatasFile(string $fileName) : booleanCheck if the given filename is a metadatas one
| Name | Type | Description |
|---|---|---|
| $fileName | string | File name |
| Type | Description |
|---|---|
| boolean | True if it's a metadatas one |

_loadMetadatas(string $id) : array | falseLoad metadatas from disk
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| array | false | Metadatas associative array |

_log(string $message, $priority = 4) : void
| Name | Type | Description |
|---|---|---|
| $message | string | |
| $priority |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

_loggerSanity() : voidMake sure if we enable logging that the Zend_Log class is available.
Inherited from: \Zend_Cache_Backend::_loggerSanity()Create a default log object if none is set.
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

_metadatasFile(string $id) : stringMake and return a file name (with path) for metadatas
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| Type | Description |
|---|---|
| string | Metadatas file name (with path) |

_path(string $id, boolean $parts = false) : stringReturn the complete directory path of a filename (including hashedDirectoryStructure)
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| $parts | boolean | if true, returns array of directory parts instead of single string |
| Type | Description |
|---|---|
| string | Complete directory path |

_recursiveMkdirAndChmod(string $id) : booleanMake the directory strucuture for the given id
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |
| Type | Description |
|---|---|
| boolean | true |

_remove(string $file) : booleanRemove a file
If we can't remove the file (because of locks or any problem), we will touch the file to invalidate it
| Name | Type | Description |
|---|---|---|
| $file | string | Complete file path |
| Type | Description |
|---|---|
| boolean | True if ok |

_saveMetadatas(string $id, array $metadatas) : booleanSave metadatas to disk
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| $metadatas | array | Associative array |
| Type | Description |
|---|---|
| boolean | True if no problem |

_setMetadatas(string $id, array $metadatas, boolean $save = true) : booleanSet a metadatas record
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| $metadatas | array | Associative array of metadatas |
| $save | boolean | optional pass false to disable saving to file |
| Type | Description |
|---|---|
| boolean | True if no problem |

_test(string $id, boolean $doNotTestCacheValidity) : boolean | mixedTest if the given cache id is available (and still valid as a cache record)
| Name | Type | Description |
|---|---|---|
| $id | string | Cache id |
| $doNotTestCacheValidity | boolean | If set to true, the cache validity won't be tested |
| Type | Description |
|---|---|
| boolean | mixed | false (a cache is not available) or "last modified" timestamp (int) of the available cache record |

clean(string $mode = \Zend_Cache::CLEANING_MODE_ALL, \tags $tags = array()) : booleanClean some cache records
Available modes are :
Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} ($tags can be an array of strings or a single string) Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags ($tags can be an array of strings or a single string)
| Name | Type | Description |
|---|---|---|
| $mode | string | clean mode |
| $tags | \tags | array $tags array of tags |
| Type | Description |
|---|---|
| boolean | true if no problem |

getCapabilities() : arrayReturn an associative array of capabilities (booleans) of the backend
The array must include these keys : - automatic_cleaning (is automating cleaning necessary) - tags (are tags supported) - expired_read (is it possible to read expired cache records (for doNotTestCacheValidity option for example)) - priority does the backend deal with priority when saving - infinite_lifetime (is infinite lifetime can work with this backend) - get_list (is it possible to get the list of cache ids and the complete list of tags)
| Type | Description |
|---|---|
| array | associative of with capabilities |

getFillingPercentage() : intReturn the filling percentage of the backend storage
| Type | Description |
|---|---|
| int | integer between 0 and 100 |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

getIds() : arrayReturn an array of stored cache ids
| Type | Description |
|---|---|
| array | array of stored cache ids (string) |

getIdsMatchingAnyTags(array $tags = array()) : arrayReturn an array of stored cache ids which match any given tags
In case of multiple tags, a logical AND is made between tags
| Name | Type | Description |
|---|---|---|
| $tags | array | array of tags |
| Type | Description |
|---|---|
| array | array of any matching cache ids (string) |

getIdsMatchingTags(array $tags = array()) : arrayReturn an array of stored cache ids which match given tags
In case of multiple tags, a logical AND is made between tags
| Name | Type | Description |
|---|---|---|
| $tags | array | array of tags |
| Type | Description |
|---|---|
| array | array of matching cache ids (string) |

getIdsNotMatchingTags(array $tags = array()) : arrayReturn an array of stored cache ids which don't match given tags
In case of multiple tags, a logical OR is made between tags
| Name | Type | Description |
|---|---|---|
| $tags | array | array of tags |
| Type | Description |
|---|---|
| array | array of not matching cache ids (string) |

getLifetime(int $specificLifetime) : int
if $specificLifetime is not false, the given specific life time is used else, the global lifetime is used
| Name | Type | Description |
|---|---|---|
| $specificLifetime | int |
| Type | Description |
|---|---|
| int | Cache life time |

getMetadatas(string $id) : arrayReturn an array of metadatas for the given cache id
The array must include these keys : - expire : the expire timestamp - tags : a string array of tags - mtime : timestamp of last modification time
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |
| Type | Description |
|---|---|
| array | array of metadatas (false if the cache id is not found) |

getOption(string $name) : mixed
| Name | Type | Description |
|---|---|---|
| $name | string | Optional, the options name to return |
| Type | Description |
|---|---|
| mixed |
| Exception | Description |
|---|---|
| \Zend_Cache_Exceptions |

getTags() : arrayReturn an array of stored tags
| Type | Description |
|---|---|
| array | array of stored tags (string) |

getTmpDir() : stringDetermine system TMP directory and detect if we have read access
Inherited from: \Zend_Cache_Backend::getTmpDir()inspired from Zend_File_Transfer_Adapter_Abstract
| Type | Description |
|---|---|
| string |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception | if unable to determine directory |

isAutomaticCleaningAvailable() : booleanReturn true if the automatic cleaning is available for the backend
Inherited from: \Zend_Cache_Backend::isAutomaticCleaningAvailable()DEPRECATED : use getCapabilities() instead
| Type | Description |
|---|---|
| boolean |

load(string $id, boolean $doNotTestCacheValidity = false) : string | falseTest if a cache is available for the given id and (if yes) return it (false else)
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |
| $doNotTestCacheValidity | boolean | if set to true, the cache validity won't be tested |
| Type | Description |
|---|---|
| string | false | cached datas |

remove(string $id) : booleanRemove a cache record
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |
| Type | Description |
|---|---|
| boolean | true if no problem |

save(string $data, string $id, array $tags = array(), int $specificLifetime = false) : booleanSave some string datas into a cache record
Note : $data is always "string" (serialization is done by the core not by the backend)
| Name | Type | Description |
|---|---|---|
| $data | string | Datas to cache |
| $id | string | Cache id |
| $tags | array | Array of strings, the cache record will be tagged by each string entry |
| $specificLifetime | int | If != false, set a specific lifetime for this cache record (null => infinite lifetime) |
| Type | Description |
|---|---|
| boolean | true if no problem |

setCacheDir(string $value, boolean $trailingSeparator = true) : voidSet the cache_dir (particular case of setOption() method)
| Name | Type | Description |
|---|---|---|
| $value | string | |
| $trailingSeparator | boolean | If true, add a trailing separator is necessary |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

setDirectives(array $directives) : void
| Name | Type | Description |
|---|---|---|
| $directives | array | Assoc of directives |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

setOption(string $name, mixed $value) : void
| Name | Type | Description |
|---|---|---|
| $name | string | |
| $value | mixed |
| Exception | Description |
|---|---|
| \Zend_Cache_Exception |

test(string $id) : mixedTest if a cache is available or not (for the given id)
| Name | Type | Description |
|---|---|---|
| $id | string | cache id |
| Type | Description |
|---|---|
| mixed | false (a cache is not available) or "last modified" timestamp (int) of the available cache record |