2 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
20 require_once 'Cache/Container/file.php';
25 * This cache container stores files with no special encoding to reduce overhead.
26 * Expiration & user data are stored in seperate files, prefixed with a '.' and
27 * suffixed with '.exp' & '.dat' respectively.
29 * See http://atomized.org/PEAR/Cache_trifile.html for more information.
31 * @author Ian Eure <ieure@php.net>
34 class Cache_Container_trifile extends Cache_Container_file {
38 * @param string $id Cache ID to fetch
39 * @param string $group Group to fetch from
40 * @return array 1-dimensional array in the format: expiration,data,userdata
42 function fetch($id, $group)
44 $file = $this->getFilename($id, $group);
45 if (!file_exists($file))
46 return array(NULL, NULL, NULL);
49 file_get_contents($this->_getExpFile($file)),
50 file_get_contents($file),
51 file_get_contents($this->_getUDFile($file))
56 * Get the file to store cache data in.
58 * @return string Cache data file name
61 function _getFile($file)
63 $dir = dirname($file);
64 $file = basename($file);
65 return $dir.'/.'.$file;
69 * Get the file to store expiration data in.
71 * @return string Expiration data file name
74 function _getExpFile($file)
76 return $this->_getFile($file).'.exp';
80 * Get the file to store user data in.
82 * @return string User data file name
85 function _getUDFile($file)
87 return $this->_getFile($file).'.dat';
93 * @param string $id Cache ID
94 * @param mixed $cachedata Data to cache
95 * @param mixed $expires When the data expires
96 * @param string $group Cache group to store data in
97 * @param mixed $userdata Additional data to store
98 * @return boolean true on success, false otherwise
100 function save($id, $cachedata, $expires, $group, $userdata)
102 $this->flushPreload($id, $group);
104 $file = $this->getFilename($id, $group);
105 if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
108 if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
111 if(PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userData))) {
119 * Save data in a file
121 * @param string $file File to save data in
122 * @param string $data Data to save
123 * @return mixed true on success, Cache_Error otherwise
125 function _saveData($file, $data) {
127 if (!($fh = @fopen($file, 'wb')))
128 return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__);
130 if ($this->fileLocking) {
136 if($this->fileLocking) {