Quick Search:

View

Revision:

Diff

Diff from 1860 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/trunk/oscommerce/includes/classes/cache.php

Annotated File View

hpdl
1
1 <?php
2 /*
mattice
151
3   $Id: cache.php 1860 2009-03-06 23:25:01Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1860
8   Copyright (c) 2007 osCommerce
hpdl
1
9
hpdl
1498
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1860
13 */
hpdl
1
14
hpdl
1860
15 /**
16  * The osC_Cache class handles the caching of dynamically generated data
17  */
hpdl
1
18
hpdl
1860
19   class osC_Cache {
hpdl
1
20
hpdl
1860
21 /**
22  * The cached data
23  *
24  * @var mixed
25  * @access private
26  */
hpdl
1
27
hpdl
1860
28     private $_data;
hpdl
1
29
hpdl
1860
30 /**
31  * The key ID for the cached data
32  *
33  * @var string
34  * @access private
35  */
hpdl
1
36
hpdl
1860
37     private $_key;
hpdl
1
38
hpdl
1860
39 /**
40  * Write the data to a cache file
41  *
42  * @param string mixed $data The data to cache
43  * @param string $key The key ID to save the cached data with
44  * @access public
45  */
hpdl
1
46
hpdl
1860
47     public function write($data, $key = null) {
48       if ( empty($key) ) {
49         $key = $this->_key;
hpdl
1
50       }
51
hpdl
1860
52       return ( file_put_contents(DIR_FS_WORK . $key . '.cache', serialize($data), LOCK_EX) !== false );
hpdl
1
53     }
54
hpdl
1860
55 /**
56  * Read data from a cache file if it has not yet expired
57  *
58  * @param string $key The key ID to read the data from the cached file
59  * @param int $expire The amount of minutes the cached data is active for
60  * @access public
61  * @return boolean
62  */
hpdl
1
63
hpdl
1860
64     public function read($key, $expire = null) {
65       $this->_key = $key;
66
hpdl
1
67       $filename = DIR_FS_WORK . $key . '.cache';
68
hpdl
1860
69       if ( file_exists($filename) ) {
hpdl
1
70         $difference = floor((time() - filemtime($filename)) / 60);
71
hpdl
1860
72         if ( empty($expire) || ( is_numeric($expire) && ($difference < $expire)) ) {
73           $this->_data = unserialize(file_get_contents($filename));
hpdl
1
74
hpdl
1860
75           return true;
hpdl
1
76         }
77       }
78
79       return false;
80     }
81
hpdl
1860
82 /**
83  * Return the cached data
84  *
85  * @access public
86  * @return mixed
87  */
88
89     public function getCache() {
90       return $this->_data;
hpdl
1
91     }
92
hpdl
1860
93 /**
94  * Start the buffer to cache its contents
95  *
96  * @access public
97  */
98
99     public function startBuffer() {
hpdl
1
100       ob_start();
101     }
102
hpdl
1860
103 /**
104  * Stop the buffer and cache its contents
105  *
106  * @access public
107  */
hpdl
1
108
hpdl
1860
109     public function stopBuffer() {
110       $this->_data = ob_get_contents();
111
hpdl
1
112       ob_end_clean();
113
hpdl
1860
114       $this->write($this->_data);
hpdl
1
115     }
116
hpdl
1860
117 /**
118  * Delete cached files by their key ID
119  *
120  * @param string $key The key ID of the cached files to delete
121  * @access public
122  */
hpdl
1
123
hpdl
1860
124     public static function clear($key) {
hpdl
1
125       $key_length = strlen($key);
126
127       $d = dir(DIR_FS_WORK);
128
hpdl
1860
129       while ( ($entry = $d->read()) !== false ) {
130         if ( (strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key) ) {
hpdl
1
131           @unlink(DIR_FS_WORK . $entry);
132         }
133       }
134
135       $d->close();
136     }
137   }
138 ?>