File: trifile.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (147 lines) | stat: -rw-r--r-- 4,764 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// +----------------------------------------------------------------------+
// | PEAR :: Cache                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
// |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
// |          Ian Eure <ieure@php.net>                                    |
// +----------------------------------------------------------------------+
//
// $Id: trifile.php,v 1.1 2004/06/21 08:39:38 rurban Exp $

require_once 'Cache/Container/file.php';

/**
 * Tri-file cache.
 *
 * This cache container stores files with no special encoding to reduce overhead.
 * Expiration & user data are stored in seperate files, prefixed with a '.' and
 * suffixed with '.exp' & '.dat' respectively.
 *
 * See http://atomized.org/PEAR/Cache_trifile.html for more information.
 *
 * @author Ian Eure <ieure@php.net>
 * @version 1.0
 */
class Cache_Container_trifile extends Cache_Container_file {
    /**
     * Fetch cached file.
     *
     * @param string $id Cache ID to fetch
     * @param string $group Group to fetch from
     * @return array 1-dimensional array in the format: expiration,data,userdata
     */
    function fetch($id, $group)
    {
        $file = $this->getFilename($id, $group);
        if (!file_exists($file))
            return array(NULL, NULL, NULL);
        
        return array(
                file_get_contents($this->_getExpFile($file)),
                file_get_contents($file),
                file_get_contents($this->_getUDFile($file))
        );
    }
    
    /**
     * Get the file to store cache data in.
     *
     * @return string Cache data file name
     * @access private
     */
    function _getFile($file)
    {
        $dir = dirname($file);
        $file = basename($file);
        return $dir.'/.'.$file;
    }
    
    /**
     * Get the file to store expiration data in.
     *
     * @return string Expiration data file name
     * @access private
     */
    function _getExpFile($file)
    {
        return $this->_getFile($file).'.exp';
    }
    
    /**
     * Get the file to store user data in.
     *
     * @return string User data file name
     * @access private
     */
    function _getUDFile($file)
    {
        return $this->_getFile($file).'.dat';
    }
    
    /**
     * Cache file
     *
     * @param string $id Cache ID
     * @param mixed $cachedata Data to cache
     * @param mixed $expires When the data expires
     * @param string $group Cache group to store data in
     * @param mixed $userdata Additional data to store
     * @return boolean true on success, false otherwise
     */
    function save($id, $cachedata, $expires, $group, $userdata)
    {
        $this->flushPreload($id, $group);

        $file = $this->getFilename($id, $group);
        if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
            return $res;
        }
        if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
            return $res;
        }
        if(PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userData))) {
            return $res;
        }

        return true;
    }

    /**
     * Save data in a file
     *
     * @param string $file File to save data in
     * @param string $data Data to save
     * @return mixed true on success, Cache_Error otherwise
     */
    function _saveData($file, $data) {
        // Save data
        if (!($fh = @fopen($file, 'wb')))
            return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__);
        
        if ($this->fileLocking) {
            flock($fh, LOCK_EX);
        }
        
        fwrite($fh, $data);
        
        if($this->fileLocking) {
            flock($fh, LOCK_UN);
        }
        
        fclose($fh);
        return true;
    }
}

?>