1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | /** |
---|
25 | Provides a persistent cache mechanism across multiple page loads/sessions etc... |
---|
26 | */ |
---|
27 | abstract class PersistentCache |
---|
28 | { |
---|
29 | var $default_lifetime = 86400; |
---|
30 | protected $instance_key = PHPWG_VERSION; |
---|
31 | |
---|
32 | /** |
---|
33 | @return a key that can be safely be used with get/set methods |
---|
34 | */ |
---|
35 | function make_key($key) |
---|
36 | { |
---|
37 | if ( is_array($key) ) |
---|
38 | { |
---|
39 | $key = implode('&', $key); |
---|
40 | } |
---|
41 | $key .= $this->instance_key; |
---|
42 | return md5($key); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | Searches for a key in the persistent cache and fills corresponding value. |
---|
47 | @param string $key |
---|
48 | @param out mixed $value |
---|
49 | @return false if the $key is not found in cache ($value is not modified in this case) |
---|
50 | */ |
---|
51 | abstract function get($key, &$value); |
---|
52 | |
---|
53 | /** |
---|
54 | Sets a key/value pair in the persistent cache. |
---|
55 | @param string $key - it should be the return value of make_key function |
---|
56 | @param mixed $value |
---|
57 | @param int $lifetime |
---|
58 | @return false on error |
---|
59 | */ |
---|
60 | abstract function set($key, $value, $lifetime=null); |
---|
61 | |
---|
62 | /** |
---|
63 | Purge the persistent cache. |
---|
64 | @param boolean $all - if false only expired items will be purged |
---|
65 | */ |
---|
66 | abstract function purge($all); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | Implementation of a persistent cache using files. |
---|
72 | */ |
---|
73 | class PersistentFileCache extends PersistentCache |
---|
74 | { |
---|
75 | private $dir; |
---|
76 | |
---|
77 | function __construct() |
---|
78 | { |
---|
79 | global $conf; |
---|
80 | $this->dir = PHPWG_ROOT_PATH.$conf['data_location'].'cache/'; |
---|
81 | } |
---|
82 | |
---|
83 | function get($key, &$value) |
---|
84 | { |
---|
85 | $loaded = @file_get_contents($this->dir.$key.'.cache'); |
---|
86 | if ($loaded !== false && ($loaded=unserialize($loaded)) !== false) |
---|
87 | { |
---|
88 | if ($loaded['expire'] > time()) |
---|
89 | { |
---|
90 | $value = $loaded['data']; |
---|
91 | return true; |
---|
92 | } |
---|
93 | } |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | function set($key, $value, $lifetime=null) |
---|
98 | { |
---|
99 | if ($lifetime === null) |
---|
100 | { |
---|
101 | $lifetime = $this->default_lifetime; |
---|
102 | } |
---|
103 | |
---|
104 | if (rand() % 97 == 0) |
---|
105 | { |
---|
106 | $this->purge(false); |
---|
107 | } |
---|
108 | |
---|
109 | $serialized = serialize( array( |
---|
110 | 'expire' => time() + $lifetime, |
---|
111 | 'data' => $value |
---|
112 | )); |
---|
113 | |
---|
114 | if (false === @file_put_contents($this->dir.$key.'.cache', $serialized)) |
---|
115 | { |
---|
116 | mkgetdir($this->dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR); |
---|
117 | if (false === @file_put_contents($this->dir.$key.'.cache', $serialized)) |
---|
118 | { |
---|
119 | return false; |
---|
120 | } |
---|
121 | } |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | function purge($all) |
---|
126 | { |
---|
127 | $files = glob($this->dir.'*.cache'); |
---|
128 | if (empty($files)) |
---|
129 | { |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | $limit = time() - $this->default_lifetime; |
---|
134 | foreach ($files as $file) |
---|
135 | { |
---|
136 | if ($all || @filemtime($file) < $limit) |
---|
137 | @unlink($file); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | ?> |
---|