]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/Cache/Container/trifile.php
Update to PEAR Cache-1.5.6
[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 // $Id: trifile.php 184642 2005-04-18 19:05:01Z dufuz $
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 class Cache_Container_trifile extends Cache_Container_file
37 {
38     /**
39      * Fetch cached file.
40      *
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
44      */
45     function fetch($id, $group)
46     {
47         $file = $this->getFilename($id, $group);
48         if (PEAR::isError($file)) {
49             return $file;
50         }
51
52         if (!file_exists($file)) {
53             return array(null, null, null);
54         }
55         return array(
56                 file_get_contents($this->_getExpFile($file)),
57                 file_get_contents($file),
58                 file_get_contents($this->_getUDFile($file))
59         );
60     }
61     
62     /**
63      * Get the file to store cache data in.
64      *
65      * @return string Cache data file name
66      * @access private
67      */
68     function _getFile($file)
69     {
70         $dir = dirname($file);
71         $file = basename($file);
72         return $dir.'/.'.$file;
73     }
74     
75     /**
76      * Get the file to store expiration data in.
77      *
78      * @return string Expiration data file name
79      * @access private
80      */
81     function _getExpFile($file)
82     {
83         return $this->_getFile($file).'.exp';
84     }
85     
86     /**
87      * Get the file to store user data in.
88      *
89      * @return string User data file name
90      * @access private
91      */
92     function _getUDFile($file)
93     {
94         return $this->_getFile($file).'.dat';
95     }
96     
97     /**
98      * Cache file
99      *
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
106      */
107     function save($id, $cachedata, $expires, $group, $userdata)
108     {
109         $this->flushPreload($id, $group);
110
111         $file = $this->getFilename($id, $group);
112         if (PEAR::isError($file)) {
113             return $file;
114         }
115
116         if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
117             return $res;
118         }
119
120         $expires = $this->getExpiresAbsolute($expires);
121         if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
122             return $res;
123         }
124
125         if (PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userdata))) {
126             return $res;
127         }
128
129         return true;
130     }
131
132     /**
133      * Save data in a file
134      *
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
138      */
139     function _saveData($file, $data)
140     {
141         // Save 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__);
144         
145         if ($this->fileLocking) {
146             flock($fh, LOCK_EX);
147         }
148         
149         fwrite($fh, $data);
150         
151         if ($this->fileLocking) {
152             flock($fh, LOCK_UN);
153         }
154         
155         fclose($fh);
156         return true;
157     }
158 }
159
160 ?>