Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Cache_Backend_File Class Reference
Inheritance diagram for Zend_Cache_Backend_File:
Zend_Cache_Backend Zend_Cache_Backend_ExtendedInterface Zend_Cache_Backend_Interface

Public Member Functions

 __construct (array $options=array())
 
 setCacheDir ($value, $trailingSeparator=true)
 
 load ($id, $doNotTestCacheValidity=false)
 
 test ($id)
 
 save ($data, $id, $tags=array(), $specificLifetime=false)
 
 remove ($id)
 
 clean ($mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
 
 getIds ()
 
 getTags ()
 
 getIdsMatchingTags ($tags=array())
 
 getIdsNotMatchingTags ($tags=array())
 
 getIdsMatchingAnyTags ($tags=array())
 
 getFillingPercentage ()
 
 getMetadatas ($id)
 
 touch ($id, $extraLifetime)
 
 getCapabilities ()
 
 ___expire ($id)
 
- Public Member Functions inherited from Zend_Cache_Backend
 __construct (array $options=array())
 
 setDirectives ($directives)
 
 setOption ($name, $value)
 
 getOption ($name)
 
 getLifetime ($specificLifetime)
 
 isAutomaticCleaningAvailable ()
 
 getTmpDir ()
 
- Public Member Functions inherited from Zend_Cache_Backend_Interface
 setDirectives ($directives)
 

Protected Member Functions

 _getMetadatas ($id)
 
 _setMetadatas ($id, $metadatas, $save=true)
 
 _delMetadatas ($id)
 
 _cleanMetadatas ()
 
 _loadMetadatas ($id)
 
 _saveMetadatas ($id, $metadatas)
 
 _metadatasFile ($id)
 
 _isMetadatasFile ($fileName)
 
 _remove ($file)
 
 _clean ($dir, $mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
 
 _get ($dir, $mode, $tags=array())
 
 _expireTime ($lifetime)
 
 _hash ($data, $controlType)
 
 _idToFileName ($id)
 
 _file ($id)
 
 _path ($id, $parts=false)
 
 _recursiveMkdirAndChmod ($id)
 
 _test ($id, $doNotTestCacheValidity)
 
 _fileGetContents ($file)
 
 _filePutContents ($file, $string)
 
 _fileNameToId ($fileName)
 
- Protected Member Functions inherited from Zend_Cache_Backend
 _isGoodTmpDir ($dir)
 
 _loggerSanity ()
 
 _log ($message, $priority=4)
 

Protected Attributes

 $_options
 
 $_metadatasArray = array()
 
- Protected Attributes inherited from Zend_Cache_Backend
 $_directives
 
 $_options = array()
 

Detailed Description

Definition at line 40 of file File.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( array  $options = array())

Constructor

Parameters
array$optionsassociative array of options
Exceptions
Zend_Cache_Exception

Definition at line 124 of file File.php.

125  {
126  parent::__construct($options);
127  if ($this->_options['cache_dir'] !== null) { // particular case for this option
128  $this->setCacheDir($this->_options['cache_dir']);
129  } else {
130  $this->setCacheDir(self::getTmpDir() . DIRECTORY_SEPARATOR, false);
131  }
132  if (isset($this->_options['file_name_prefix'])) { // particular case for this option
133  if (!preg_match('~^[a-zA-Z0-9_]+$~D', $this->_options['file_name_prefix'])) {
134  Zend_Cache::throwException('Invalid file_name_prefix : must use only [a-zA-Z0-9_]');
135  }
136  }
137  if ($this->_options['metadatas_array_max_size'] < 10) {
138  Zend_Cache::throwException('Invalid metadatas_array_max_size, must be > 10');
139  }
140 
141  if (isset($options['hashed_directory_umask'])) {
142  // See #ZF-12047
143  trigger_error("'hashed_directory_umask' is deprecated -> please use 'hashed_directory_perm' instead", E_USER_NOTICE);
144  if (!isset($options['hashed_directory_perm'])) {
145  $options['hashed_directory_perm'] = $options['hashed_directory_umask'];
146  }
147  }
148  if (isset($options['hashed_directory_perm']) && is_string($options['hashed_directory_perm'])) {
149  // See #ZF-4422
150  $this->_options['hashed_directory_perm'] = octdec($this->_options['hashed_directory_perm']);
151  }
152 
153  if (isset($options['cache_file_umask'])) {
154  // See #ZF-12047
155  trigger_error("'cache_file_umask' is deprecated -> please use 'cache_file_perm' instead", E_USER_NOTICE);
156  if (!isset($options['cache_file_perm'])) {
157  $options['cache_file_perm'] = $options['cache_file_umask'];
158  }
159  }
160  if (isset($options['cache_file_perm']) && is_string($options['cache_file_perm'])) {
161  // See #ZF-4422
162  $this->_options['cache_file_perm'] = octdec($this->_options['cache_file_perm']);
163  }
164  }
setCacheDir($value, $trailingSeparator=true)
Definition: File.php:174
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

Member Function Documentation

◆ ___expire()

___expire (   $id)

PUBLIC METHOD FOR UNIT TESTING ONLY !

Force a cache record to expire

Parameters
string$idcache id

Definition at line 483 of file File.php.

484  {
485  $metadatas = $this->_getMetadatas($id);
486  if ($metadatas) {
487  $metadatas['expire'] = 1;
488  $this->_setMetadatas($id, $metadatas);
489  }
490  }
$id
Definition: fieldset.phtml:14
_setMetadatas($id, $metadatas, $save=true)
Definition: File.php:520

◆ _clean()

_clean (   $dir,
  $mode = Zend_Cache::CLEANING_MODE_ALL,
  $tags = array() 
)
protected

Clean 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)

Parameters
string$dirDirectory to clean
string$modeClean mode
array$tagsArray of tags
Exceptions
Zend_Cache_Exception
Returns
boolean True if no problem

Definition at line 665 of file File.php.

666  {
667  if (!is_dir($dir)) {
668  return false;
669  }
670  $result = true;
671  $prefix = $this->_options['file_name_prefix'];
672  $glob = @glob($dir . $prefix . '--*');
673  if ($glob === false) {
674  // On some systems it is impossible to distinguish between empty match and an error.
675  return true;
676  }
677  $metadataFiles = array();
678  foreach ($glob as $file) {
679  if (is_file($file)) {
680  $fileName = basename($file);
681  if ($this->_isMetadatasFile($fileName)) {
682  // In CLEANING_MODE_ALL, we drop anything, even remainings old metadatas files.
683  // To do that, we need to save the list of the metadata files first.
685  $metadataFiles[] = $file;
686  }
687  continue;
688  }
689  $id = $this->_fileNameToId($fileName);
690  $metadatas = $this->_getMetadatas($id);
691  if ($metadatas === FALSE) {
692  $metadatas = array('expire' => 1, 'tags' => array());
693  }
694  switch ($mode) {
696  $result = $result && $this->remove($id);
697  break;
699  if (time() > $metadatas['expire']) {
700  $result = $this->remove($id) && $result;
701  }
702  break;
704  $matching = true;
705  foreach ($tags as $tag) {
706  if (!in_array($tag, $metadatas['tags'])) {
707  $matching = false;
708  break;
709  }
710  }
711  if ($matching) {
712  $result = $this->remove($id) && $result;
713  }
714  break;
716  $matching = false;
717  foreach ($tags as $tag) {
718  if (in_array($tag, $metadatas['tags'])) {
719  $matching = true;
720  break;
721  }
722  }
723  if (!$matching) {
724  $result = $this->remove($id) && $result;
725  }
726  break;
728  $matching = false;
729  foreach ($tags as $tag) {
730  if (in_array($tag, $metadatas['tags'])) {
731  $matching = true;
732  break;
733  }
734  }
735  if ($matching) {
736  $result = $this->remove($id) && $result;
737  }
738  break;
739  default:
740  Zend_Cache::throwException('Invalid mode for clean() method');
741  break;
742  }
743  }
744  if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
745  // Recursive call
746  $result = $this->_clean($file . DIRECTORY_SEPARATOR, $mode, $tags) && $result;
748  // we try to drop the structure too
749  @rmdir($file);
750  }
751  }
752  }
753 
754  // cycle through metadataFiles and delete orphaned ones
755  foreach ($metadataFiles as $file) {
756  if (file_exists($file)) {
757  $result = $this->_remove($file) && $result;
758  }
759  }
760 
761  return $result;
762  }
$id
Definition: fieldset.phtml:14
_clean($dir, $mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
Definition: File.php:665
const CLEANING_MODE_OLD
Definition: Cache.php:73
$fileName
Definition: translate.phtml:15
$prefix
Definition: name.phtml:25
_isMetadatasFile($fileName)
Definition: File.php:614
_fileNameToId($fileName)
Definition: File.php:1031
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74

◆ _cleanMetadatas()

_cleanMetadatas ( )
protected

Clear the metadatas array

Returns
void

Definition at line 556 of file File.php.

557  {
558  $this->_metadatasArray = array();
559  }

◆ _delMetadatas()

_delMetadatas (   $id)
protected

Drop a metadata record

Parameters
string$idCache id
Returns
boolean True if no problem

Definition at line 542 of file File.php.

543  {
544  if (isset($this->_metadatasArray[$id])) {
545  unset($this->_metadatasArray[$id]);
546  }
547  $file = $this->_metadatasFile($id);
548  return $this->_remove($file);
549  }
$id
Definition: fieldset.phtml:14

◆ _expireTime()

_expireTime (   $lifetime)
protected

Compute & return the expire time

Parameters
int$lifetime
Returns
int expire time (unix timestamp)

Definition at line 854 of file File.php.

855  {
856  if ($lifetime === null) {
857  return 9999999999;
858  }
859  return time() + $lifetime;
860  }

◆ _file()

_file (   $id)
protected

Make and return a file name (with path)

Parameters
string$idCache id
Returns
string File name (with path)

Definition at line 905 of file File.php.

906  {
907  $path = $this->_path($id);
908  $fileName = $this->_idToFileName($id);
909  return $path . $fileName;
910  }
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
_path($id, $parts=false)
Definition: File.php:919

◆ _fileGetContents()

_fileGetContents (   $file)
protected

Return the file content of the given file

Parameters
string$fileFile complete path
Returns
string File content (or false if problem)

Definition at line 984 of file File.php.

985  {
986  $result = false;
987  if (!is_file($file)) {
988  return false;
989  }
990  $f = @fopen($file, 'rb');
991  if ($f) {
992  if ($this->_options['file_locking']) @flock($f, LOCK_SH);
993  $result = stream_get_contents($f);
994  if ($this->_options['file_locking']) @flock($f, LOCK_UN);
995  @fclose($f);
996  }
997  return $result;
998  }

◆ _fileNameToId()

_fileNameToId (   $fileName)
protected

Transform a file name into cache id and return it

Parameters
string$fileNameFile name
Returns
string Cache id

Definition at line 1031 of file File.php.

1032  {
1033  $prefix = $this->_options['file_name_prefix'];
1034  return preg_replace('~^' . $prefix . '---(.*)$~', '$1', $fileName);
1035  }
$fileName
Definition: translate.phtml:15
$prefix
Definition: name.phtml:25

◆ _filePutContents()

_filePutContents (   $file,
  $string 
)
protected

Put the given string into the given file

Parameters
string$fileFile complete path
string$stringString to put in file
Returns
boolean true if no problem

Definition at line 1007 of file File.php.

1008  {
1009  $result = false;
1010  $f = @fopen($file, 'ab+');
1011  if ($f) {
1012  if ($this->_options['file_locking']) @flock($f, LOCK_EX);
1013  fseek($f, 0);
1014  ftruncate($f, 0);
1015  $tmp = @fwrite($f, $string);
1016  if (!($tmp === FALSE)) {
1017  $result = true;
1018  }
1019  @fclose($f);
1020  }
1021  @chmod($file, $this->_options['cache_file_perm']);
1022  return $result;
1023  }

◆ _get()

_get (   $dir,
  $mode,
  $tags = array() 
)
protected

Definition at line 764 of file File.php.

765  {
766  if (!is_dir($dir)) {
767  return false;
768  }
769  $result = array();
770  $prefix = $this->_options['file_name_prefix'];
771  $glob = @glob($dir . $prefix . '--*');
772  if ($glob === false) {
773  // On some systems it is impossible to distinguish between empty match and an error.
774  return array();
775  }
776  foreach ($glob as $file) {
777  if (is_file($file)) {
778  $fileName = basename($file);
779  $id = $this->_fileNameToId($fileName);
780  $metadatas = $this->_getMetadatas($id);
781  if ($metadatas === FALSE) {
782  continue;
783  }
784  if (time() > $metadatas['expire']) {
785  continue;
786  }
787  switch ($mode) {
788  case 'ids':
789  $result[] = $id;
790  break;
791  case 'tags':
792  $result = array_unique(array_merge($result, $metadatas['tags']));
793  break;
794  case 'matching':
795  $matching = true;
796  foreach ($tags as $tag) {
797  if (!in_array($tag, $metadatas['tags'])) {
798  $matching = false;
799  break;
800  }
801  }
802  if ($matching) {
803  $result[] = $id;
804  }
805  break;
806  case 'notMatching':
807  $matching = false;
808  foreach ($tags as $tag) {
809  if (in_array($tag, $metadatas['tags'])) {
810  $matching = true;
811  break;
812  }
813  }
814  if (!$matching) {
815  $result[] = $id;
816  }
817  break;
818  case 'matchingAny':
819  $matching = false;
820  foreach ($tags as $tag) {
821  if (in_array($tag, $metadatas['tags'])) {
822  $matching = true;
823  break;
824  }
825  }
826  if ($matching) {
827  $result[] = $id;
828  }
829  break;
830  default:
831  Zend_Cache::throwException('Invalid mode for _get() method');
832  break;
833  }
834  }
835  if ((is_dir($file)) and ($this->_options['hashed_directory_level']>0)) {
836  // Recursive call
837  $recursiveRs = $this->_get($file . DIRECTORY_SEPARATOR, $mode, $tags);
838  if ($recursiveRs === false) {
839  $this->_log('Zend_Cache_Backend_File::_get() / recursive call : can\'t list entries of "'.$file.'"');
840  } else {
841  $result = array_unique(array_merge($result, $recursiveRs));
842  }
843  }
844  }
845  return array_unique($result);
846  }
_get($dir, $mode, $tags=array())
Definition: File.php:764
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273
$fileName
Definition: translate.phtml:15
$prefix
Definition: name.phtml:25
_fileNameToId($fileName)
Definition: File.php:1031
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ _getMetadatas()

_getMetadatas (   $id)
protected

Get a metadatas record

Parameters
string$idCache id
Returns
array|false Associative array of metadatas

Definition at line 498 of file File.php.

499  {
500  if (isset($this->_metadatasArray[$id])) {
501  return $this->_metadatasArray[$id];
502  } else {
503  $metadatas = $this->_loadMetadatas($id);
504  if (!$metadatas) {
505  return false;
506  }
507  $this->_setMetadatas($id, $metadatas, false);
508  return $metadatas;
509  }
510  }
$id
Definition: fieldset.phtml:14
_setMetadatas($id, $metadatas, $save=true)
Definition: File.php:520

◆ _hash()

_hash (   $data,
  $controlType 
)
protected

Make a control key with the string containing datas

Parameters
string$dataData
string$controlTypeType of control 'md5', 'crc32' or 'strlen'
Exceptions
Zend_Cache_Exception
Returns
string Control key

Definition at line 870 of file File.php.

871  {
872  switch ($controlType) {
873  case 'md5':
874  return md5($data);
875  case 'crc32':
876  return crc32($data);
877  case 'strlen':
878  return strlen($data);
879  case 'adler32':
880  return hash('adler32', $data);
881  default:
882  Zend_Cache::throwException("Incorrect hash function : $controlType");
883  }
884  }
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ _idToFileName()

_idToFileName (   $id)
protected

Transform a cache id into a file name and return it

Parameters
string$idCache id
Returns
string File name

Definition at line 892 of file File.php.

893  {
894  $prefix = $this->_options['file_name_prefix'];
895  $result = $prefix . '---' . $id;
896  return $result;
897  }
$id
Definition: fieldset.phtml:14
$prefix
Definition: name.phtml:25

◆ _isMetadatasFile()

_isMetadatasFile (   $fileName)
protected

Check if the given filename is a metadatas one

Parameters
string$fileNameFile name
Returns
boolean True if it's a metadatas one

Definition at line 614 of file File.php.

615  {
616  $id = $this->_fileNameToId($fileName);
617  if (substr($id, 0, 21) == 'internal-metadatas---') {
618  return true;
619  } else {
620  return false;
621  }
622  }
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
_fileNameToId($fileName)
Definition: File.php:1031

◆ _loadMetadatas()

_loadMetadatas (   $id)
protected

Load metadatas from disk

Parameters
string$idCache id
Returns
array|false Metadatas associative array

Definition at line 567 of file File.php.

568  {
569  $file = $this->_metadatasFile($id);
570  $result = $this->_fileGetContents($file);
571  if (!$result) {
572  return false;
573  }
574  $tmp = @unserialize($result);
575  return $tmp;
576  }
_fileGetContents($file)
Definition: File.php:984
$id
Definition: fieldset.phtml:14

◆ _metadatasFile()

_metadatasFile (   $id)
protected

Make and return a file name (with path) for metadatas

Parameters
string$idCache id
Returns
string Metadatas file name (with path)

Definition at line 601 of file File.php.

602  {
603  $path = $this->_path($id);
604  $fileName = $this->_idToFileName('internal-metadatas---' . $id);
605  return $path . $fileName;
606  }
$id
Definition: fieldset.phtml:14
$fileName
Definition: translate.phtml:15
_path($id, $parts=false)
Definition: File.php:919

◆ _path()

_path (   $id,
  $parts = false 
)
protected

Return the complete directory path of a filename (including hashedDirectoryStructure)

Parameters
string$idCache id
boolean$partsif true, returns array of directory parts instead of single string
Returns
string Complete directory path

Definition at line 919 of file File.php.

920  {
921  $partsArray = array();
922  $root = $this->_options['cache_dir'];
923  $prefix = $this->_options['file_name_prefix'];
924  if ($this->_options['hashed_directory_level']>0) {
925  $hash = hash('adler32', $id);
926  for ($i=0 ; $i < $this->_options['hashed_directory_level'] ; $i++) {
927  $root = $root . $prefix . '--' . substr($hash, 0, $i + 1) . DIRECTORY_SEPARATOR;
928  $partsArray[] = $root;
929  }
930  }
931  if ($parts) {
932  return $partsArray;
933  } else {
934  return $root;
935  }
936  }
$id
Definition: fieldset.phtml:14
$prefix
Definition: name.phtml:25
$i
Definition: gallery.phtml:31

◆ _recursiveMkdirAndChmod()

_recursiveMkdirAndChmod (   $id)
protected

Make the directory strucuture for the given id

Parameters
string$idcache id
Returns
boolean true

Definition at line 944 of file File.php.

945  {
946  if ($this->_options['hashed_directory_level'] <=0) {
947  return true;
948  }
949  $partsArray = $this->_path($id, true);
950  foreach ($partsArray as $part) {
951  if (!is_dir($part)) {
952  @mkdir($part, $this->_options['hashed_directory_perm']);
953  @chmod($part, $this->_options['hashed_directory_perm']); // see #ZF-320 (this line is required in some configurations)
954  }
955  }
956  return true;
957  }
$id
Definition: fieldset.phtml:14
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
_path($id, $parts=false)
Definition: File.php:919

◆ _remove()

_remove (   $file)
protected

Remove a file

If we can't remove the file (because of locks or any problem), we will touch the file to invalidate it

Parameters
string$fileComplete file path
Returns
boolean True if ok

Definition at line 633 of file File.php.

634  {
635  if (!is_file($file)) {
636  return false;
637  }
638  if (!@unlink($file)) {
639  # we can't remove the file (because of locks or any problem)
640  $this->_log("Zend_Cache_Backend_File::_remove() : we can't remove $file");
641  return false;
642  }
643  return true;
644  }
_log($message, $priority=4)
Definition: Backend.php:273

◆ _saveMetadatas()

_saveMetadatas (   $id,
  $metadatas 
)
protected

Save metadatas to disk

Parameters
string$idCache id
array$metadatasAssociative array
Returns
boolean True if no problem

Definition at line 585 of file File.php.

586  {
587  $file = $this->_metadatasFile($id);
588  $result = $this->_filePutContents($file, serialize($metadatas));
589  if (!$result) {
590  return false;
591  }
592  return true;
593  }
$id
Definition: fieldset.phtml:14
_filePutContents($file, $string)
Definition: File.php:1007

◆ _setMetadatas()

_setMetadatas (   $id,
  $metadatas,
  $save = true 
)
protected

Set a metadatas record

Parameters
string$idCache id
array$metadatasAssociative array of metadatas
boolean$saveoptional pass false to disable saving to file
Returns
boolean True if no problem

Definition at line 520 of file File.php.

521  {
522  if (count($this->_metadatasArray) >= $this->_options['metadatas_array_max_size']) {
523  $n = (int) ($this->_options['metadatas_array_max_size'] / 10);
524  $this->_metadatasArray = array_slice($this->_metadatasArray, $n);
525  }
526  if ($save) {
527  $result = $this->_saveMetadatas($id, $metadatas);
528  if (!$result) {
529  return false;
530  }
531  }
532  $this->_metadatasArray[$id] = $metadatas;
533  return true;
534  }
$id
Definition: fieldset.phtml:14
_saveMetadatas($id, $metadatas)
Definition: File.php:585

◆ _test()

_test (   $id,
  $doNotTestCacheValidity 
)
protected

Test if the given cache id is available (and still valid as a cache record)

Parameters
string$idCache id
boolean$doNotTestCacheValidityIf set to true, the cache validity won't be tested
Returns
boolean|mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Definition at line 966 of file File.php.

967  {
968  $metadatas = $this->_getMetadatas($id);
969  if (!$metadatas) {
970  return false;
971  }
972  if ($doNotTestCacheValidity || (time() <= $metadatas['expire'])) {
973  return $metadatas['mtime'];
974  }
975  return false;
976  }
$id
Definition: fieldset.phtml:14

◆ clean()

clean (   $mode = Zend_Cache::CLEANING_MODE_ALL,
  $tags = array() 
)

Clean 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)

Parameters
string$modeclean mode
array$tagsarray of tags
Returns
boolean true if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 308 of file File.php.

309  {
310  // We use this protected method to hide the recursive stuff
311  clearstatcache();
312  return $this->_clean($this->_options['cache_dir'], $mode, $tags);
313  }
_clean($dir, $mode=Zend_Cache::CLEANING_MODE_ALL, $tags=array())
Definition: File.php:665
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ getCapabilities()

getCapabilities ( )

Return 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)
Returns
array associative of with capabilities

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 464 of file File.php.

465  {
466  return array(
467  'automatic_cleaning' => true,
468  'tags' => true,
469  'expired_read' => true,
470  'priority' => false,
471  'infinite_lifetime' => true,
472  'get_list' => true
473  );
474  }

◆ getFillingPercentage()

getFillingPercentage ( )

Return the filling percentage of the backend storage

Exceptions
Zend_Cache_Exception
Returns
int integer between 0 and 100

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 380 of file File.php.

381  {
382  $free = disk_free_space($this->_options['cache_dir']);
383  $total = disk_total_space($this->_options['cache_dir']);
384  if ($total == 0) {
385  Zend_Cache::throwException('can\'t get disk_total_space');
386  } else {
387  if ($free >= $total) {
388  return 100;
389  }
390  return ((int) (100. * ($total - $free) / $total));
391  }
392  }
disk_free_space($path)
Definition: io.php:36
static throwException($msg, Exception $e=null)
Definition: Cache.php:205

◆ getIds()

getIds ( )

Return an array of stored cache ids

Returns
array array of stored cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 320 of file File.php.

321  {
322  return $this->_get($this->_options['cache_dir'], 'ids', array());
323  }
_get($dir, $mode, $tags=array())
Definition: File.php:764

◆ getIdsMatchingAnyTags()

getIdsMatchingAnyTags (   $tags = array())

Return an array of stored cache ids which match any given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of any matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 369 of file File.php.

370  {
371  return $this->_get($this->_options['cache_dir'], 'matchingAny', $tags);
372  }
_get($dir, $mode, $tags=array())
Definition: File.php:764

◆ getIdsMatchingTags()

getIdsMatchingTags (   $tags = array())

Return an array of stored cache ids which match given tags

In case of multiple tags, a logical AND is made between tags

Parameters
array$tagsarray of tags
Returns
array array of matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 343 of file File.php.

344  {
345  return $this->_get($this->_options['cache_dir'], 'matching', $tags);
346  }
_get($dir, $mode, $tags=array())
Definition: File.php:764

◆ getIdsNotMatchingTags()

getIdsNotMatchingTags (   $tags = array())

Return an array of stored cache ids which don't match given tags

In case of multiple tags, a logical OR is made between tags

Parameters
array$tagsarray of tags
Returns
array array of not matching cache ids (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 356 of file File.php.

357  {
358  return $this->_get($this->_options['cache_dir'], 'notMatching', $tags);
359  }
_get($dir, $mode, $tags=array())
Definition: File.php:764

◆ getMetadatas()

getMetadatas (   $id)

Return 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
Parameters
string$idcache id
Returns
array array of metadatas (false if the cache id is not found)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 405 of file File.php.

406  {
407  $metadatas = $this->_getMetadatas($id);
408  if (!$metadatas) {
409  return false;
410  }
411  if (time() > $metadatas['expire']) {
412  return false;
413  }
414  return array(
415  'expire' => $metadatas['expire'],
416  'tags' => $metadatas['tags'],
417  'mtime' => $metadatas['mtime']
418  );
419  }
$id
Definition: fieldset.phtml:14

◆ getTags()

getTags ( )

Return an array of stored tags

Returns
array array of stored tags (string)

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 330 of file File.php.

331  {
332  return $this->_get($this->_options['cache_dir'], 'tags', array());
333  }
_get($dir, $mode, $tags=array())
Definition: File.php:764

◆ load()

load (   $id,
  $doNotTestCacheValidity = false 
)

Test if a cache is available for the given id and (if yes) return it (false else)

Parameters
string$idcache id
boolean$doNotTestCacheValidityif set to true, the cache validity won't be tested
Returns
string|false cached datas

Implements Zend_Cache_Backend_Interface.

Definition at line 196 of file File.php.

197  {
198  if (!($this->_test($id, $doNotTestCacheValidity))) {
199  // The cache is not hit !
200  return false;
201  }
202  $metadatas = $this->_getMetadatas($id);
203  $file = $this->_file($id);
204  $data = $this->_fileGetContents($file);
205  if ($this->_options['read_control']) {
206  $hashData = $this->_hash($data, $this->_options['read_control_type']);
207  $hashControl = $metadatas['hash'];
208  if ($hashData != $hashControl) {
209  // Problem detected by the read control !
210  $this->_log('Zend_Cache_Backend_File::load() / read_control : stored hash and computed hash do not match');
211  $this->remove($id);
212  return false;
213  }
214  }
215  return $data;
216  }
_fileGetContents($file)
Definition: File.php:984
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273
_test($id, $doNotTestCacheValidity)
Definition: File.php:966
_hash($data, $controlType)
Definition: File.php:870

◆ remove()

remove (   $id)

Remove a cache record

Parameters
string$idcache id
Returns
boolean true if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 282 of file File.php.

283  {
284  $file = $this->_file($id);
285  $boolRemove = $this->_remove($file);
286  $boolMetadata = $this->_delMetadatas($id);
287  return $boolMetadata && $boolRemove;
288  }
$id
Definition: fieldset.phtml:14

◆ save()

save (   $data,
  $id,
  $tags = array(),
  $specificLifetime = false 
)

Save some string datas into a cache record

Note : $data is always "string" (serialization is done by the core not by the backend)

Parameters
string$dataDatas to cache
string$idCache id
array$tagsArray of strings, the cache record will be tagged by each string entry
boolean | int$specificLifetimeIf != false, set a specific lifetime for this cache record (null => infinite lifetime)
Returns
boolean true if no problem

Implements Zend_Cache_Backend_Interface.

Definition at line 242 of file File.php.

243  {
244  clearstatcache();
245  $file = $this->_file($id);
246  $path = $this->_path($id);
247  if ($this->_options['hashed_directory_level'] > 0) {
248  if (!is_writable($path)) {
249  // maybe, we just have to build the directory structure
251  }
252  if (!is_writable($path)) {
253  return false;
254  }
255  }
256  if ($this->_options['read_control']) {
257  $hash = $this->_hash($data, $this->_options['read_control_type']);
258  } else {
259  $hash = '';
260  }
261  $metadatas = array(
262  'hash' => $hash,
263  'mtime' => time(),
264  'expire' => $this->_expireTime($this->getLifetime($specificLifetime)),
265  'tags' => $tags
266  );
267  $res = $this->_setMetadatas($id, $metadatas);
268  if (!$res) {
269  $this->_log('Zend_Cache_Backend_File::save() / error on saving metadata');
270  return false;
271  }
272  $res = $this->_filePutContents($file, $data);
273  return $res;
274  }
_expireTime($lifetime)
Definition: File.php:854
$id
Definition: fieldset.phtml:14
_log($message, $priority=4)
Definition: Backend.php:273
_filePutContents($file, $string)
Definition: File.php:1007
is_writable($path)
Definition: io.php:25
_recursiveMkdirAndChmod($id)
Definition: File.php:944
_setMetadatas($id, $metadatas, $save=true)
Definition: File.php:520
getLifetime($specificLifetime)
Definition: Backend.php:143
_path($id, $parts=false)
Definition: File.php:919
_hash($data, $controlType)
Definition: File.php:870

◆ setCacheDir()

setCacheDir (   $value,
  $trailingSeparator = true 
)

Set the cache_dir (particular case of setOption() method)

Parameters
string$value
boolean$trailingSeparatorIf true, add a trailing separator is necessary
Exceptions
Zend_Cache_Exception
Returns
void

Definition at line 174 of file File.php.

175  {
176  if (!is_dir($value)) {
177  Zend_Cache::throwException(sprintf('cache_dir "%s" must be a directory', $value));
178  }
179  if (!is_writable($value)) {
180  Zend_Cache::throwException(sprintf('cache_dir "%s" is not writable', $value));
181  }
182  if ($trailingSeparator) {
183  // add a trailing DIRECTORY_SEPARATOR if necessary
184  $value = rtrim(realpath($value), '\\/') . DIRECTORY_SEPARATOR;
185  }
186  $this->_options['cache_dir'] = $value;
187  }
$value
Definition: gender.phtml:16
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
is_writable($path)
Definition: io.php:25

◆ test()

test (   $id)

Test if a cache is available or not (for the given id)

Parameters
string$idcache id
Returns
mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Implements Zend_Cache_Backend_Interface.

Definition at line 224 of file File.php.

225  {
226  clearstatcache();
227  return $this->_test($id, false);
228  }
$id
Definition: fieldset.phtml:14
_test($id, $doNotTestCacheValidity)
Definition: File.php:966

◆ touch()

touch (   $id,
  $extraLifetime 
)

Give (if possible) an extra lifetime to the given cache id

Parameters
string$idcache id
int$extraLifetime
Returns
boolean true if ok

Implements Zend_Cache_Backend_ExtendedInterface.

Definition at line 428 of file File.php.

429  {
430  $metadatas = $this->_getMetadatas($id);
431  if (!$metadatas) {
432  return false;
433  }
434  if (time() > $metadatas['expire']) {
435  return false;
436  }
437  $newMetadatas = array(
438  'hash' => $metadatas['hash'],
439  'mtime' => time(),
440  'expire' => $metadatas['expire'] + $extraLifetime,
441  'tags' => $metadatas['tags']
442  );
443  $res = $this->_setMetadatas($id, $newMetadatas);
444  if (!$res) {
445  return false;
446  }
447  return true;
448  }
$id
Definition: fieldset.phtml:14
_setMetadatas($id, $metadatas, $save=true)
Definition: File.php:520

Field Documentation

◆ $_metadatasArray

$_metadatasArray = array()
protected

Definition at line 115 of file File.php.

◆ $_options

$_options
protected
Initial value:
= array(
'cache_dir' => null,
'file_locking' => true,
'read_control' => true,
'read_control_type' => 'crc32',
'hashed_directory_level' => 0,
'hashed_directory_perm' => 0700,
'file_name_prefix' => 'zend_cache',
'cache_file_perm' => 0600,
'metadatas_array_max_size' => 100
)

Definition at line 98 of file File.php.


The documentation for this class was generated from the following file: