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 6888 2009-06-04 13:01:12Z vargenau $
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 seperate 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 {
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
44 function fetch($id, $group)
46 $file = $this->getFilename($id, $group);
47 if (!file_exists($file))
48 return array(NULL, NULL, NULL);
51 file_get_contents($this->_getExpFile($file)),
52 file_get_contents($file),
53 file_get_contents($this->_getUDFile($file))
58 * Get the file to store cache data in.
60 * @return string Cache data file name
63 function _getFile($file)
65 $dir = dirname($file);
66 $file = basename($file);
67 return $dir.'/.'.$file;
71 * Get the file to store expiration data in.
73 * @return string Expiration data file name
76 function _getExpFile($file)
78 return $this->_getFile($file).'.exp';
82 * Get the file to store user data in.
84 * @return string User data file name
87 function _getUDFile($file)
89 return $this->_getFile($file).'.dat';
95 * @param string $id Cache ID
96 * @param mixed $cachedata Data to cache
97 * @param mixed $expires When the data expires
98 * @param string $group Cache group to store data in
99 * @param mixed $userdata Additional data to store
100 * @return boolean true on success, false otherwise
102 function save($id, $cachedata, $expires, $group, $userdata)
104 $this->flushPreload($id, $group);
106 $file = $this->getFilename($id, $group);
107 if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
110 if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
113 if(PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userData))) {
121 * Save data in a file
123 * @param string $file File to save data in
124 * @param string $data Data to save
125 * @return mixed true on success, Cache_Error otherwise
127 function _saveData($file, $data) {
129 if (!($fh = @fopen($file, 'wb')))
130 return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__);
132 if ($this->fileLocking) {
138 if($this->fileLocking) {