File: file.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (122 lines) | stat: -rw-r--r-- 3,484 bytes parent folder | download
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
<?php
/*
 * $Horde: horde/lib/Cache/file.php,v 1.6.2.3 2003/01/03 12:48:41 jan Exp $
 *
 * Copyright 1999-2003 Anil Madhavapeddy <anil@recoil.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

/**
 * The Cache:: class provides a common abstracted interface into the
 * various caching backends.  It also provides functions for checking
 * in, retrieving, and flushing a cache.
 *
 * @author  Anil Madhavapeddy <anil@recoil.org>
 * @version $Revision: 1.6.2.3 $
 * @since   Horde 1.3
 * @package horde.cache
 */
class Cache_file {

    var $dir;
    var $prefix = 'cache_';

    /**
     * Construct a new Cache_file object.
     * @param array $params Parameters to determine where to cache
     */
    function Cache_file($params = array())
    {
        if (!empty($params['dir']) && @is_dir($params['dir'])) {
            $this->dir = $params['dir'];
        } else {
            $this->dir = Horde::getTempDir();
        }

        if (isset($params['prefix'])) {
            $this->prefix = $params['prefix'];
        }
    }

    /**
     * Attempts to store an object in the cache.
     *
     * @param string $oid  Object ID used as the caching key.
     * @param mixed $data  Data to store in the cache.
     * @return boolean     True on success, false on failure.
     */
    function store($oid, &$data)
    {
        $filename = $this->oidToFile($oid);
        $tmp_file = Horde::getTempFile('HordeCache', true, $this->dir);

        if ($fd = fopen($tmp_file, 'w')) {
            if (fwrite( $fd, $data ) < strlen($data)) {
                fclose($fd);
                return false;
            } else {
                fclose($fd);
                rename($tmp_file, $filename);
            }
        }
    }

    /**
     * Map an object ID to a unique filename.
     * @param string $oid Object ID
     * @return string Fully qualified filename.
     */
    function oidToFile($oid)
    {
        return $this->dir . DIRECTORY_SEPARATOR . $this->prefix . md5($oid);
    }

    /**
     * Attempts to retrieve a cached object from the filesystem.
     *
     * @param string $oid  Object ID to query.
     * @param int    $type Expiration heuristic.
     *                     CACHE_IMS: Expire if supplied value is greater than cached time
     * @param int $val     Value to supply for the expiration heuristic.
     * @return mixed       Cached data, or false if none was found.
     */
    function query($oid, $type = CACHE_IMS, $val = 0)
    {
        $filename = $this->oidToFile($oid);

        /* An object exists in the cache */
        if (file_exists($filename)) {

           $lastmod = filemtime($filename);
           switch ($type) {

             /* If the object has been created after the supplied
              * value, object is valid */
             case CACHE_IMS:
               if ($val > $lastmod) {
                   @unlink($filename);
               }
               break;

             /* Invalid cache query specified; unlink the object
              * to be safe */
             default:
               @unlink($filename);
               break;
           }

           /* This will fail if the object no longer exists */
           if ($fd = @fopen($filename, 'r')) {
               return fread($fd, filesize($filename));
           }
        }

        /* Nothing cached, return failure */
        return false;
    }

}

?>