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 // $Id: trifile.php 184642 2005-04-18 19:05:01Z dufuz $
22 require_once 'Cache/Container/file.php';
27 * This cache container stores files with no special encoding to reduce overhead.
28 * Expiration & user data are stored in separate files, prefixed with a '.' and
29 * suffixed with '.exp' & '.dat' respectively.
31 * See http://atomized.org/PEAR/Cache_trifile.html for more information.
33 * @author Ian Eure <ieure@php.net>
36 class Cache_Container_trifile extends Cache_Container_file
41 * @param string $id Cache ID to fetch
42 * @param string $group Group to fetch from
43 * @return array 1-dimensional array in the format: expiration,data,userdata
45 function fetch($id, $group)
47 $file = $this->getFilename($id, $group);
48 if (PEAR::isError($file)) {
52 if (!file_exists($file)) {
53 return array(null, null, null);
56 file_get_contents($this->_getExpFile($file)),
57 file_get_contents($file),
58 file_get_contents($this->_getUDFile($file))
63 * Get the file to store cache data in.
65 * @return string Cache data file name
68 function _getFile($file)
70 $dir = dirname($file);
71 $file = basename($file);
72 return $dir.'/.'.$file;
76 * Get the file to store expiration data in.
78 * @return string Expiration data file name
81 function _getExpFile($file)
83 return $this->_getFile($file).'.exp';
87 * Get the file to store user data in.
89 * @return string User data file name
92 function _getUDFile($file)
94 return $this->_getFile($file).'.dat';
100 * @param string $id Cache ID
101 * @param mixed $cachedata Data to cache
102 * @param mixed $expires When the data expires
103 * @param string $group Cache group to store data in
104 * @param mixed $userdata Additional data to store
105 * @return boolean true on success, false otherwise
107 function save($id, $cachedata, $expires, $group, $userdata)
109 $this->flushPreload($id, $group);
111 $file = $this->getFilename($id, $group);
112 if (PEAR::isError($file)) {
116 if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
120 $expires = $this->getExpiresAbsolute($expires);
121 if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
125 if (PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userdata))) {
133 * Save data in a file
135 * @param string $file File to save data in
136 * @param string $data Data to save
137 * @return mixed true on success, Cache_Error otherwise
139 function _saveData($file, $data)
142 if (!($fh = @fopen($file, 'wb')))
143 return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__);
145 if ($this->fileLocking) {
151 if ($this->fileLocking) {