  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: cache.php 1497 2007-03-29 13:40:05Z hpdl $ |
| |
| 3 | + | $Id: cache.php 1672 2007-07-26 22:07:13Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
7 | 7 | | |
  |
8 | | - | Copyright (c) 2004 osCommerce |
| |
| 8 | + | Copyright (c) 2007 osCommerce |
|
9 | 9 | | |
| |
10 | 10 | | This program is free software; you can redistribute it and/or modify |
| |
11 | 11 | | it under the terms of the GNU General Public License v2 (1991) |
| |
12 | 12 | | as published by the Free Software Foundation. |
  |
| 13 | + | */ |
|
13 | 14 | | |
  |
14 | | - | Class usage examples: |
| |
| 15 | + | /** |
| |
| 16 | + | * The osC_Cache class handles the caching of dynamically generated data |
| |
| 17 | + | */ |
|
15 | 18 | | |
  |
16 | | - | - Caching HTML: |
| |
17 | | - | if ($osC_Cache->read('key', 60) === false) { |
| |
18 | | - | $osC_Cache->startBuffer(); |
| |
19 | | - | ------ PHP/HTML LOGIC HERE ------ |
| |
20 | | - | $osC_Cache->stopBuffer(); |
| |
21 | | - | } |
| |
| 19 | + | class osC_Cache { |
|
22 | 20 | | |
  |
23 | | - | echo $osC_Cache->getCache(); |
| |
| 21 | + | /** |
| |
| 22 | + | * The cached data |
| |
| 23 | + | * |
| |
| 24 | + | * @var mixed |
| |
| 25 | + | * @access private |
| |
| 26 | + | */ |
|
24 | 27 | | |
  |
25 | | - | - Caching data (in memory): |
| |
26 | | - | if ($osC_Cache->read('key', 60) { |
| |
27 | | - | $variable = $osC_Cache->getCache(); |
| |
28 | | - | } else { |
| |
29 | | - | $variable = array('some', 'data'); |
| |
| 28 | + | private $_data; |
|
30 | 29 | | |
  |
31 | | - | $osC_Cache->writeBuffer($variable); |
| |
32 | | - | } |
| |
33 | | - | */ |
| |
| 30 | + | /** |
| |
| 31 | + | * The key ID for the cached data |
| |
| 32 | + | * |
| |
| 33 | + | * @var string |
| |
| 34 | + | * @access private |
| |
| 35 | + | */ |
|
34 | 36 | | |
  |
35 | | - | class osC_Cache { |
| |
36 | | - | var $cached_data, |
| |
37 | | - | $cache_key; |
| |
| 37 | + | private $_key; |
|
38 | 38 | | |
  |
39 | | - | function write($key, &$data) { |
| |
40 | | - | $filename = DIR_FS_WORK . $key . '.cache'; |
| |
| 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 | + | */ |
|
41 | 46 | | |
  |
42 | | - | if ($fp = @fopen($filename, 'w')) { |
| |
43 | | - | flock($fp, 2); // LOCK_EX |
| |
44 | | - | fputs($fp, serialize($data)); |
| |
45 | | - | flock($fp, 3); // LOCK_UN |
| |
46 | | - | fclose($fp); |
| |
47 | | - | |
| |
48 | | - | return true; |
| |
| 47 | + | public function write($data, $key = null) { |
| |
| 48 | + | if ( empty($key) ) { |
| |
| 49 | + | $key = $this->_key; |
|
49 | 50 | | } |
| |
50 | 51 | | |
  |
51 | | - | return false; |
| |
| 52 | + | return ( file_put_contents(DIR_FS_WORK . $key . '.cache', serialize($data), LOCK_EX) !== false ); |
|
52 | 53 | | } |
| |
53 | 54 | | |
  |
54 | | - | function read($key, $expire = 0) { |
| |
55 | | - | $this->cache_key = $key; |
| |
| 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 | + | */ |
|
56 | 63 | | |
  |
| 64 | + | public function read($key, $expire = null) { |
| |
| 65 | + | $this->_key = $key; |
| |
| 66 | + | |
|
57 | 67 | | $filename = DIR_FS_WORK . $key . '.cache'; |
| |
58 | 68 | | |
  |
59 | | - | if (file_exists($filename)) { |
| |
| 69 | + | if ( file_exists($filename) ) { |
|
60 | 70 | | $difference = floor((time() - filemtime($filename)) / 60); |
| |
61 | 71 | | |
  |
62 | | - | if ( ($expire == '0') || ($difference < $expire) ) { |
| |
63 | | - | if ($fp = @fopen($filename, 'r')) { |
| |
64 | | - | $this->cached_data = unserialize(fread($fp, filesize($filename))); |
| |
| 72 | + | if ( empty($expire) || ( is_numeric($expire) && ($difference < $expire)) ) { |
| |
| 73 | + | $this->_data = unserialize(file_get_contents($filename)); |
|
65 | 74 | | |
  |
66 | | - | fclose($fp); |
| |
67 | | - | |
| |
68 | | - | return true; |
| |
69 | | - | } |
| |
| 75 | + | return true; |
|
70 | 76 | | } |
| |
71 | 77 | | } |
| |
72 | 78 | | |
| |
73 | 79 | | return false; |
| |
74 | 80 | | } |
| |
75 | 81 | | |
  |
76 | | - | function &getCache() { |
| |
77 | | - | return $this->cached_data; |
| |
| 82 | + | /** |
| |
| 83 | + | * Return the cached data |
| |
| 84 | + | * |
| |
| 85 | + | * @access public |
| |
| 86 | + | * @return mixed |
| |
| 87 | + | */ |
| |
| 88 | + | |
| |
| 89 | + | public function getCache() { |
| |
| 90 | + | return $this->_data; |
|
78 | 91 | | } |
| |
79 | 92 | | |
  |
80 | | - | function startBuffer() { |
| |
| 93 | + | /** |
| |
| 94 | + | * Start the buffer to cache its contents |
| |
| 95 | + | * |
| |
| 96 | + | * @access public |
| |
| 97 | + | */ |
| |
| 98 | + | |
| |
| 99 | + | public function startBuffer() { |
|
81 | 100 | | ob_start(); |
| |
82 | 101 | | } |
| |
83 | 102 | | |
  |
84 | | - | function stopBuffer() { |
| |
85 | | - | $this->cached_data = ob_get_contents(); |
| |
| 103 | + | /** |
| |
| 104 | + | * Stop the buffer and cache its contents |
| |
| 105 | + | * |
| |
| 106 | + | * @access public |
| |
| 107 | + | */ |
|
86 | 108 | | |
  |
| 109 | + | public function stopBuffer() { |
| |
| 110 | + | $this->_data = ob_get_contents(); |
| |
| 111 | + | |
|
87 | 112 | | ob_end_clean(); |
| |
88 | 113 | | |
  |
89 | | - | $this->write($this->cache_key, $this->cached_data); |
| |
| 114 | + | $this->write($this->_data); |
|
90 | 115 | | } |
| |
91 | 116 | | |
  |
92 | | - | function writeBuffer(&$data) { |
| |
93 | | - | $this->cached_data = $data; |
| |
| 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 | + | */ |
|
94 | 123 | | |
  |
95 | | - | $this->write($this->cache_key, $this->cached_data); |
| |
96 | | - | } |
| |
97 | | - | |
| |
98 | | - | function clear($key) { |
| |
| 124 | + | public static function clear($key) { |
|
99 | 125 | | $key_length = strlen($key); |
| |
100 | 126 | | |
| |
101 | 127 | | $d = dir(DIR_FS_WORK); |
| |
102 | 128 | | |
  |
103 | | - | while ($entry = $d->read()) { |
| |
104 | | - | if ((strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key)) { |
| |
| 129 | + | while ( ($entry = $d->read()) !== false ) { |
| |
| 130 | + | if ( (strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key) ) { |
  |
105 | 131 | | @unlink(DIR_FS_WORK . $entry); |
| |
106 | 132 | | } |
| |
107 | 133 | | } |