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,v 1.1 2004/04/01 07:50:07 chregu Exp $
  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.      * Fetch cached file.
  38.      *
  39.      * @param string $id Cache ID to fetch
  40.      * @param string $group Group to fetch from
  41.      * @return array 1-dimensional array in the format: expiration,data,userdata
  42.      */
  43.     function fetch($id$group)
  44.     {
  45.         $file $this->getFilename($id$group);
  46.         if (!file_exists($file))
  47.             return array(NULLNULLNULL);
  48.         
  49.         return array(
  50.                 file_get_contents($this->_getExpFile($file)),
  51.                 file_get_contents($file),
  52.                 file_get_contents($this->_getUDFile($file))
  53.         );
  54.     }
  55.     
  56.     /**
  57.      * Get the file to store cache data in.
  58.      *
  59.      * @return string Cache data file name
  60.      * @access private
  61.      */
  62.     function _getFile($file)
  63.     {
  64.         $dir dirname($file);
  65.         $file basename($file);
  66.         return $dir.'/.'.$file;
  67.     }
  68.     
  69.     /**
  70.      * Get the file to store expiration data in.
  71.      *
  72.      * @return string Expiration data file name
  73.      * @access private
  74.      */
  75.     function _getExpFile($file)
  76.     {
  77.         return $this->_getFile($file).'.exp';
  78.     }
  79.     
  80.     /**
  81.      * Get the file to store user data in.
  82.      *
  83.      * @return string User data file name
  84.      * @access private
  85.      */
  86.     function _getUDFile($file)
  87.     {
  88.         return $this->_getFile($file).'.dat';
  89.     }
  90.     
  91.     /**
  92.      * Cache file
  93.      *
  94.      * @param string $id Cache ID
  95.      * @param mixed $cachedata Data to cache
  96.      * @param mixed $expires When the data expires
  97.      * @param string $group Cache group to store data in
  98.      * @param mixed $userdata Additional data to store
  99.      * @return boolean true on success, false otherwise
  100.      */
  101.     function save($id$cachedata$expires$group$userdata)
  102.     {
  103.         $this->flushPreload($id$group);
  104.  
  105.         $file $this->getFilename($id$group);
  106.         if (PEAR::isError($res $this->_saveData($file$cachedata))) {
  107.             return $res;
  108.         }
  109.         if (PEAR::isError($res $this->_saveData($this->_getExpFile($file)$expires))) {
  110.             return $res;
  111.         }
  112.         if(PEAR::isError($res $this->_saveData($this->_getUDFile($file)$userData))) {
  113.             return $res;
  114.         }
  115.  
  116.         return true;
  117.     }
  118.  
  119.     /**
  120.      * Save data in a file
  121.      *
  122.      * @param string $file File to save data in
  123.      * @param string $data Data to save
  124.      * @return mixed true on success, Cache_Error otherwise
  125.      */
  126.     function _saveData($file$data{
  127.         // Save data
  128.         if (!($fh @fopen($file'wb')))
  129.             return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path."__FILE____LINE__);
  130.         
  131.         if ($this->fileLocking{
  132.             flock($fhLOCK_EX);
  133.         }
  134.         
  135.         fwrite($fh$data);
  136.         
  137.         if($this->fileLocking{
  138.             flock($fhLOCK_UN);
  139.         }
  140.         
  141.         fclose($fh);
  142.         return true;
  143.     }
  144. }
  145.  
  146. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.