]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/Cache/Container/trifile.php
Remove svn:keywords
[SourceForge/phpwiki.git] / lib / pear / Cache / Container / 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 require_once 'Cache/Container/file.php';
21
22 /**
23  * Tri-file cache.
24  *
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.
28  *
29  * See http://atomized.org/PEAR/Cache_trifile.html for more information.
30  *
31  * @author Ian Eure <ieure@php.net>
32  * @version 1.0
33  */
34 class Cache_Container_trifile extends Cache_Container_file {
35     /**
36      * Fetch cached file.
37      *
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
41      */
42     function fetch($id, $group)
43     {
44         $file = $this->getFilename($id, $group);
45         if (!file_exists($file))
46             return array(NULL, NULL, NULL);
47
48         return array(
49                 file_get_contents($this->_getExpFile($file)),
50                 file_get_contents($file),
51                 file_get_contents($this->_getUDFile($file))
52         );
53     }
54
55     /**
56      * Get the file to store cache data in.
57      *
58      * @return string Cache data file name
59      * @access private
60      */
61     function _getFile($file)
62     {
63         $dir = dirname($file);
64         $file = basename($file);
65         return $dir.'/.'.$file;
66     }
67
68     /**
69      * Get the file to store expiration data in.
70      *
71      * @return string Expiration data file name
72      * @access private
73      */
74     function _getExpFile($file)
75     {
76         return $this->_getFile($file).'.exp';
77     }
78
79     /**
80      * Get the file to store user data in.
81      *
82      * @return string User data file name
83      * @access private
84      */
85     function _getUDFile($file)
86     {
87         return $this->_getFile($file).'.dat';
88     }
89
90     /**
91      * Cache file
92      *
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
99      */
100     function save($id, $cachedata, $expires, $group, $userdata)
101     {
102         $this->flushPreload($id, $group);
103
104         $file = $this->getFilename($id, $group);
105         if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
106             return $res;
107         }
108         if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
109             return $res;
110         }
111         if(PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userData))) {
112             return $res;
113         }
114
115         return true;
116     }
117
118     /**
119      * Save data in a file
120      *
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
124      */
125     function _saveData($file, $data) {
126         // Save 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__);
129
130         if ($this->fileLocking) {
131             flock($fh, LOCK_EX);
132         }
133
134         fwrite($fh, $data);
135
136         if($this->fileLocking) {
137             flock($fh, LOCK_UN);
138         }
139
140         fclose($fh);
141         return true;
142     }
143 }
144
145 ?>