HandlebarsCacheDisk

A flat-file filesystem cache.

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Cache/Disk.php  
  1. class Disk implements Cache 
  2.  
  3. private $_path = ''; 
  4. private $_prefix = ''; 
  5. private $_suffix = ''; 
  6.  
  7. /** 
  8. * Construct the disk cache. 
  9. * @param string $path Filesystem path to the disk cache location 
  10. * @param string $prefix optional file prefix, defaults to empty string 
  11. * @param string $suffix optional file extension, defaults to empty string 
  12. * @throws \RuntimeException 
  13. * @throws \InvalidArgumentException 
  14. */ 
  15. public function __construct($path, $prefix = '', $suffix = '') 
  16. if (empty($path)) { 
  17. throw new \InvalidArgumentException('Must specify disk cache path'); 
  18. } elseif (!is_dir($path)) { 
  19. @mkdir($path, 0777, true); 
  20.  
  21. if (!is_dir($path)) { 
  22. throw new \RuntimeException('Could not create cache file path'); 
  23.  
  24. $this->_path = $path; 
  25. $this->_prefix = $prefix; 
  26. $this->_suffix = $suffix; 
  27.  
  28. /** 
  29. * Gets the full disk path for a given cache item's file,  
  30. * taking into account the cache path, optional prefix,  
  31. * and optional extension. 
  32. * @param string $name Name of the cache item 
  33. * @return string full disk path of cached item 
  34. */ 
  35. private function _getPath($name) 
  36. return $this->_path . DIRECTORY_SEPARATOR . 
  37. $this->_prefix . $name . $this->_suffix; 
  38.  
  39. /** 
  40. * Get cache for $name if it exists. 
  41. * @param string $name Cache id 
  42. * @return mixed data on hit, boolean false on cache not found 
  43. */ 
  44. public function get($name) 
  45. $path = $this->_getPath($name); 
  46.  
  47. return (file_exists($path)) ? 
  48. unserialize(file_get_contents($path)) : false; 
  49.  
  50. /** 
  51. * Set a cache 
  52. * @param string $name cache id 
  53. * @param mixed $value data to store 
  54. * @return void 
  55. */ 
  56. public function set($name, $value) 
  57. $path = $this->_getPath($name); 
  58.  
  59. file_put_contents($path, serialize($value)); 
  60.  
  61. /** 
  62. * Remove cache 
  63. * @param string $name Cache id 
  64. * @return void 
  65. */ 
  66. public function remove($name) 
  67. $path = $this->_getPath($name); 
  68.  
  69. unlink($path); 
  70.  
/vendor/xamin/handlebars.php/src/Handlebars/Cache/Disk.php  
  1. class Disk implements Cache 
  2.  
  3. private $_path = ''; 
  4. private $_prefix = ''; 
  5. private $_suffix = ''; 
  6.  
  7. /** 
  8. * Construct the disk cache. 
  9. * @param string $path Filesystem path to the disk cache location 
  10. * @param string $prefix optional file prefix, defaults to empty string 
  11. * @param string $suffix optional file extension, defaults to empty string 
  12. * @throws \RuntimeException 
  13. * @throws \InvalidArgumentException 
  14. */ 
  15. public function __construct($path, $prefix = '', $suffix = '') 
  16. if (empty($path)) { 
  17. throw new \InvalidArgumentException('Must specify disk cache path'); 
  18. } elseif (!is_dir($path)) { 
  19. @mkdir($path, 0777, true); 
  20.  
  21. if (!is_dir($path)) { 
  22. throw new \RuntimeException('Could not create cache file path'); 
  23.  
  24. $this->_path = $path; 
  25. $this->_prefix = $prefix; 
  26. $this->_suffix = $suffix; 
  27.  
  28. /** 
  29. * Gets the full disk path for a given cache item's file,  
  30. * taking into account the cache path, optional prefix,  
  31. * and optional extension. 
  32. * @param string $name Name of the cache item 
  33. * @return string full disk path of cached item 
  34. */ 
  35. private function _getPath($name) 
  36. return $this->_path . DIRECTORY_SEPARATOR . 
  37. $this->_prefix . $name . $this->_suffix; 
  38.  
  39. /** 
  40. * Get cache for $name if it exists. 
  41. * @param string $name Cache id 
  42. * @return mixed data on hit, boolean false on cache not found 
  43. */ 
  44. public function get($name) 
  45. $path = $this->_getPath($name); 
  46.  
  47. return (file_exists($path)) ? 
  48. unserialize(file_get_contents($path)) : false; 
  49.  
  50. /** 
  51. * Set a cache 
  52. * @param string $name cache id 
  53. * @param mixed $value data to store 
  54. * @return void 
  55. */ 
  56. public function set($name, $value) 
  57. $path = $this->_getPath($name); 
  58.  
  59. file_put_contents($path, serialize($value)); 
  60.  
  61. /** 
  62. * Remove cache 
  63. * @param string $name Cache id 
  64. * @return void 
  65. */ 
  66. public function remove($name) 
  67. $path = $this->_getPath($name); 
  68.  
  69. unlink($path); 
  70.