web/lib/Zend/Cache/Backend/File.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Cache
    16  * @package    Zend_Cache
    17  * @subpackage Zend_Cache_Backend
    17  * @subpackage Zend_Cache_Backend
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: File.php 21636 2010-03-24 17:10:23Z mabe $
    20  * @version    $Id: File.php 24844 2012-05-31 19:01:36Z rob $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Cache_Backend_Interface
    24  * @see Zend_Cache_Backend_Interface
    25  */
    25  */
    32 
    32 
    33 
    33 
    34 /**
    34 /**
    35  * @package    Zend_Cache
    35  * @package    Zend_Cache
    36  * @subpackage Zend_Cache_Backend
    36  * @subpackage Zend_Cache_Backend
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    39  */
    40 class Zend_Cache_Backend_File extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
    40 class Zend_Cache_Backend_File extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
    41 {
    41 {
    42     /**
    42     /**
    69      * This option can speed up the cache only when you have many thousands of
    69      * This option can speed up the cache only when you have many thousands of
    70      * cache file. Only specific benchs can help you to choose the perfect value
    70      * cache file. Only specific benchs can help you to choose the perfect value
    71      * for you. Maybe, 1 or 2 is a good start.
    71      * for you. Maybe, 1 or 2 is a good start.
    72      *
    72      *
    73      * =====> (int) hashed_directory_umask :
    73      * =====> (int) hashed_directory_umask :
    74      * - Umask for hashed directory structure
    74      * - deprecated
       
    75      * - Permissions for hashed directory structure
       
    76      *
       
    77      * =====> (int) hashed_directory_perm :
       
    78      * - Permissions for hashed directory structure
    75      *
    79      *
    76      * =====> (string) file_name_prefix :
    80      * =====> (string) file_name_prefix :
    77      * - prefix for cache files
    81      * - prefix for cache files
    78      * - be really carefull with this option because a too generic value in a system cache dir
    82      * - be really carefull with this option because a too generic value in a system cache dir
    79      *   (like /tmp) can cause disasters when cleaning the cache
    83      *   (like /tmp) can cause disasters when cleaning the cache
    80      *
    84      *
    81      * =====> (int) cache_file_umask :
    85      * =====> (int) cache_file_umask :
    82      * - Umask for cache files
    86      * - deprecated
       
    87      * - Permissions for cache files
       
    88      *
       
    89      * =====> (int) cache_file_perm :
       
    90      * - Permissions for cache files
    83      *
    91      *
    84      * =====> (int) metatadatas_array_max_size :
    92      * =====> (int) metatadatas_array_max_size :
    85      * - max size for the metadatas array (don't change this value unless you
    93      * - max size for the metadatas array (don't change this value unless you
    86      *   know what you are doing)
    94      *   know what you are doing)
    87      *
    95      *
    91         'cache_dir' => null,
    99         'cache_dir' => null,
    92         'file_locking' => true,
   100         'file_locking' => true,
    93         'read_control' => true,
   101         'read_control' => true,
    94         'read_control_type' => 'crc32',
   102         'read_control_type' => 'crc32',
    95         'hashed_directory_level' => 0,
   103         'hashed_directory_level' => 0,
    96         'hashed_directory_umask' => 0700,
   104         'hashed_directory_perm' => 0700,
    97         'file_name_prefix' => 'zend_cache',
   105         'file_name_prefix' => 'zend_cache',
    98         'cache_file_umask' => 0600,
   106         'cache_file_perm' => 0600,
    99         'metadatas_array_max_size' => 100
   107         'metadatas_array_max_size' => 100
   100     );
   108     );
   101 
   109 
   102     /**
   110     /**
   103      * Array of metadatas (each item is an associative array)
   111      * Array of metadatas (each item is an associative array)
   128             }
   136             }
   129         }
   137         }
   130         if ($this->_options['metadatas_array_max_size'] < 10) {
   138         if ($this->_options['metadatas_array_max_size'] < 10) {
   131             Zend_Cache::throwException('Invalid metadatas_array_max_size, must be > 10');
   139             Zend_Cache::throwException('Invalid metadatas_array_max_size, must be > 10');
   132         }
   140         }
   133         if (isset($options['hashed_directory_umask']) && is_string($options['hashed_directory_umask'])) {
   141 
       
   142         if (isset($options['hashed_directory_umask'])) {
       
   143             // See #ZF-12047
       
   144             trigger_error("'hashed_directory_umask' is deprecated -> please use 'hashed_directory_perm' instead", E_USER_NOTICE);
       
   145             if (!isset($options['hashed_directory_perm'])) {
       
   146                 $options['hashed_directory_perm'] = $options['hashed_directory_umask'];
       
   147             }
       
   148         }
       
   149         if (isset($options['hashed_directory_perm']) && is_string($options['hashed_directory_perm'])) {
   134             // See #ZF-4422
   150             // See #ZF-4422
   135             $this->_options['hashed_directory_umask'] = octdec($this->_options['hashed_directory_umask']);
   151             $this->_options['hashed_directory_perm'] = octdec($this->_options['hashed_directory_perm']);
   136         }
   152         }
   137         if (isset($options['cache_file_umask']) && is_string($options['cache_file_umask'])) {
   153 
       
   154         if (isset($options['cache_file_umask'])) {
       
   155             // See #ZF-12047
       
   156             trigger_error("'cache_file_umask' is deprecated -> please use 'cache_file_perm' instead", E_USER_NOTICE);
       
   157             if (!isset($options['cache_file_perm'])) {
       
   158                 $options['cache_file_perm'] = $options['cache_file_umask'];
       
   159             }
       
   160         }
       
   161         if (isset($options['cache_file_perm']) && is_string($options['cache_file_perm'])) {
   138             // See #ZF-4422
   162             // See #ZF-4422
   139             $this->_options['cache_file_umask'] = octdec($this->_options['cache_file_umask']);
   163             $this->_options['cache_file_perm'] = octdec($this->_options['cache_file_perm']);
   140         }
   164         }
   141     }
   165     }
   142 
   166 
   143     /**
   167     /**
   144      * Set the cache_dir (particular case of setOption() method)
   168      * Set the cache_dir (particular case of setOption() method)
   149      * @return void
   173      * @return void
   150      */
   174      */
   151     public function setCacheDir($value, $trailingSeparator = true)
   175     public function setCacheDir($value, $trailingSeparator = true)
   152     {
   176     {
   153         if (!is_dir($value)) {
   177         if (!is_dir($value)) {
   154             Zend_Cache::throwException('cache_dir must be a directory');
   178             Zend_Cache::throwException(sprintf('cache_dir "%s" must be a directory', $value));
   155         }
   179         }
   156         if (!is_writable($value)) {
   180         if (!is_writable($value)) {
   157             Zend_Cache::throwException('cache_dir is not writable');
   181             Zend_Cache::throwException(sprintf('cache_dir "%s" is not writable', $value));
   158         }
   182         }
   159         if ($trailingSeparator) {
   183         if ($trailingSeparator) {
   160             // add a trailing DIRECTORY_SEPARATOR if necessary
   184             // add a trailing DIRECTORY_SEPARATOR if necessary
   161             $value = rtrim(realpath($value), '\\/') . DIRECTORY_SEPARATOR;
   185             $value = rtrim(realpath($value), '\\/') . DIRECTORY_SEPARATOR;
   162         }
   186         }
   266 
   290 
   267     /**
   291     /**
   268      * Clean some cache records
   292      * Clean some cache records
   269      *
   293      *
   270      * Available modes are :
   294      * Available modes are :
   271      * 'all' (default)  => remove all cache entries ($tags is not used)
   295      *
   272      * 'old'            => remove too old cache entries ($tags is not used)
   296      * Zend_Cache::CLEANING_MODE_ALL (default)    => remove all cache entries ($tags is not used)
   273      * 'matchingTag'    => remove cache entries matching all given tags
   297      * Zend_Cache::CLEANING_MODE_OLD              => remove too old cache entries ($tags is not used)
   274      *                     ($tags can be an array of strings or a single string)
   298      * Zend_Cache::CLEANING_MODE_MATCHING_TAG     => remove cache entries matching all given tags
   275      * 'notMatchingTag' => remove cache entries not matching one of the given tags
   299      *                                               ($tags can be an array of strings or a single string)
   276      *                     ($tags can be an array of strings or a single string)
   300      * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
   277      * 'matchingAnyTag' => remove cache entries matching any given tags
   301      *                                               ($tags can be an array of strings or a single string)
   278      *                     ($tags can be an array of strings or a single string)
   302      * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
       
   303      *                                               ($tags can be an array of strings or a single string)
   279      *
   304      *
   280      * @param string $mode clean mode
   305      * @param string $mode clean mode
   281      * @param tags array $tags array of tags
   306      * @param tags array $tags array of tags
   282      * @return boolean true if no problem
   307      * @return boolean true if no problem
   283      */
   308      */
   720                 }
   745                 }
   721             }
   746             }
   722             if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
   747             if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
   723                 // Recursive call
   748                 // Recursive call
   724                 $result = $this->_clean($file . DIRECTORY_SEPARATOR, $mode, $tags) && $result;
   749                 $result = $this->_clean($file . DIRECTORY_SEPARATOR, $mode, $tags) && $result;
   725                 if ($mode=='all') {
   750                 if ($mode == Zend_Cache::CLEANING_MODE_ALL) {
   726                     // if mode=='all', we try to drop the structure too
   751                     // we try to drop the structure too
   727                     @rmdir($file);
   752                     @rmdir($file);
   728                 }
   753                 }
   729             }
   754             }
   730         }
   755         }
   731         return $result;
   756         return $result;
   916             return true;
   941             return true;
   917         }
   942         }
   918         $partsArray = $this->_path($id, true);
   943         $partsArray = $this->_path($id, true);
   919         foreach ($partsArray as $part) {
   944         foreach ($partsArray as $part) {
   920             if (!is_dir($part)) {
   945             if (!is_dir($part)) {
   921                 @mkdir($part, $this->_options['hashed_directory_umask']);
   946                 @mkdir($part, $this->_options['hashed_directory_perm']);
   922                 @chmod($part, $this->_options['hashed_directory_umask']); // see #ZF-320 (this line is required in some configurations)
   947                 @chmod($part, $this->_options['hashed_directory_perm']); // see #ZF-320 (this line is required in some configurations)
   923             }
   948             }
   924         }
   949         }
   925         return true;
   950         return true;
   926     }
   951     }
   927 
   952 
   985             if (!($tmp === FALSE)) {
  1010             if (!($tmp === FALSE)) {
   986                 $result = true;
  1011                 $result = true;
   987             }
  1012             }
   988             @fclose($f);
  1013             @fclose($f);
   989         }
  1014         }
   990         @chmod($file, $this->_options['cache_file_umask']);
  1015         @chmod($file, $this->_options['cache_file_perm']);
   991         return $result;
  1016         return $result;
   992     }
  1017     }
   993 
  1018 
   994     /**
  1019     /**
   995      * Transform a file name into cache id and return it
  1020      * Transform a file name into cache id and return it