Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Source for file trifile.php

Documentation is available at trifile.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17. // |          Ian Eure <ieure@php.net>                                    |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: trifile.php 184642 2005-04-18 19:05:01Z dufuz $
  21.  
  22. require_once 'Cache/Container/file.php';
  23.  
  24. /**
  25.  * Tri-file cache.
  26.  *
  27.  * This cache container stores files with no special encoding to reduce overhead.
  28.  * Expiration & user data are stored in seperate files, prefixed with a '.' and
  29.  * suffixed with '.exp' & '.dat' respectively.
  30.  *
  31.  * See http://atomized.org/PEAR/Cache_trifile.html for more information.
  32.  *
  33.  * @author Ian Eure <ieure@php.net>
  34.  * @version 1.0
  35.  */
  36. {
  37.     /**
  38.      * Fetch cached file.
  39.      *
  40.      * @param string $id Cache ID to fetch
  41.      * @param string $group Group to fetch from
  42.      * @return array 1-dimensional array in the format: expiration,data,userdata
  43.      */
  44.     function fetch($id$group)
  45.     {
  46.         $file $this->getFilename($id$group);
  47.         if (PEAR::isError($file)) {
  48.             return $file;
  49.         }
  50.  
  51.         if (!file_exists($file)) {
  52.             return array(nullnullnull);
  53.         }
  54.         return array(
  55.                 file_get_contents($this->_getExpFile($file)),
  56.                 file_get_contents($file),
  57.                 file_get_contents($this->_getUDFile($file))
  58.         );
  59.     }
  60.     
  61.     /**
  62.      * Get the file to store cache data in.
  63.      *
  64.      * @return string Cache data file name
  65.      * @access private
  66.      */
  67.     function _getFile($file)
  68.     {
  69.         $dir dirname($file);
  70.         $file basename($file);
  71.         return $dir.'/.'.$file;
  72.     }
  73.     
  74.     /**
  75.      * Get the file to store expiration data in.
  76.      *
  77.      * @return string Expiration data file name
  78.      * @access private
  79.      */
  80.     function _getExpFile($file)
  81.     {
  82.         return $this->_getFile($file).'.exp';
  83.     }
  84.     
  85.     /**
  86.      * Get the file to store user data in.
  87.      *
  88.      * @return string User data file name
  89.      * @access private
  90.      */
  91.     function _getUDFile($file)
  92.     {
  93.         return $this->_getFile($file).'.dat';
  94.     }
  95.     
  96.     /**
  97.      * Cache file
  98.      *
  99.      * @param string $id Cache ID
  100.      * @param mixed $cachedata Data to cache
  101.      * @param mixed $expires When the data expires
  102.      * @param string $group Cache group to store data in
  103.      * @param mixed $userdata Additional data to store
  104.      * @return boolean true on success, false otherwise
  105.      */
  106.     function save($id$cachedata$expires$group$userdata)
  107.     {
  108.         $this->flushPreload($id$group);
  109.  
  110.         $file $this->getFilename($id$group);
  111.         if (PEAR::isError($file)) {
  112.             return $file;
  113.         }
  114.  
  115.         if (PEAR::isError($res $this->_saveData($file$cachedata))) {
  116.             return $res;
  117.         }
  118.  
  119.         $expires $this->getExpiresAbsolute($expires);
  120.         if (PEAR::isError($res $this->_saveData($this->_getExpFile($file)$expires))) {
  121.             return $res;
  122.         }
  123.  
  124.         if (PEAR::isError($res $this->_saveData($this->_getUDFile($file)$userdata))) {
  125.             return $res;
  126.         }
  127.  
  128.         return true;
  129.     }
  130.  
  131.     /**
  132.      * Save data in a file
  133.      *
  134.      * @param string $file File to save data in
  135.      * @param string $data Data to save
  136.      * @return mixed true on success, Cache_Error otherwise
  137.      */
  138.     function _saveData($file$data)
  139.     {
  140.         // Save data
  141.         if (!($fh @fopen($file'wb')))
  142.             return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path."__FILE____LINE__);
  143.         
  144.         if ($this->fileLocking{
  145.             flock($fhLOCK_EX);
  146.         }
  147.         
  148.         fwrite($fh$data);
  149.         
  150.         if ($this->fileLocking{
  151.             flock($fhLOCK_UN);
  152.         }
  153.         
  154.         fclose($fh);
  155.         return true;
  156.     }
  157. }
  158.  
  159. ?>

Documentation generated on Mon, 11 Mar 2019 15:44:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.