]> git.mxchange.org Git - friendica.git/blob - src/Worker/ClearCache.php
d87fd46cce4aa5d299eb17363eb71e62c54f8996
[friendica.git] / src / Worker / ClearCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Photo;
28 use Friendica\Util\Proxy as ProxyUtils;
29
30 /**
31  * Clear cache entries
32  */
33 class ClearCache
34 {
35         public static function execute()
36         {
37                 $a = DI::app();
38                 $last = DI::config()->get('system', 'cache_last_cleared');
39
40                 if ($last) {
41                         $next = $last + (3600); // Once per hour
42                         $clear_cache = ($next <= time());
43                 } else {
44                         $clear_cache = true;
45                 }
46
47                 if (!$clear_cache) {
48                         return;
49                 }
50
51                 // clear old cache
52                 DI::cache()->clear();
53
54                 // clear old item cache files
55                 clear_cache();
56
57                 // clear cache for photos
58                 clear_cache($a->getBasePath(), $a->getBasePath() . "/photo");
59
60                 // clear smarty cache
61                 clear_cache($a->getBasePath() . "/view/smarty3/compiled", $a->getBasePath() . "/view/smarty3/compiled");
62
63                 // clear cache for image proxy
64                 if (!DI::config()->get("system", "proxy_disabled")) {
65                         clear_cache($a->getBasePath(), $a->getBasePath() . "/proxy");
66
67                         $cachetime = DI::config()->get('system', 'proxy_cache_time');
68
69                         if (!$cachetime) {
70                                 $cachetime = ProxyUtils::DEFAULT_TIME;
71                         }
72
73                         $condition = ['`uid` = 0 AND `resource-id` LIKE "pic:%" AND `created` < NOW() - INTERVAL ? SECOND', $cachetime];
74                         Photo::delete($condition);
75                 }
76
77                 // Delete the cached OEmbed entries that are older than three month
78                 DBA::delete('oembed', ["`created` < NOW() - INTERVAL 3 MONTH"]);
79
80                 // Delete the cached "parse_url" entries that are older than three month
81                 DBA::delete('parsed_url', ["`created` < NOW() - INTERVAL 3 MONTH"]);
82
83                 if (DI::config()->get('system', 'optimize_tables')) {
84                         Logger::info('Optimize start');
85                         DBA::e("OPTIMIZE TABLE `auth_codes`");
86                         DBA::e("OPTIMIZE TABLE `cache`");
87                         DBA::e("OPTIMIZE TABLE `challenge`");
88                         DBA::e("OPTIMIZE TABLE `locks`");
89                         DBA::e("OPTIMIZE TABLE `oembed`");
90                         DBA::e("OPTIMIZE TABLE `parsed_url`");
91                         DBA::e("OPTIMIZE TABLE `profile_check`");
92                         DBA::e("OPTIMIZE TABLE `session`");
93                         DBA::e("OPTIMIZE TABLE `tokens`");
94                         DBA::e("OPTIMIZE TABLE `process`");
95                         Logger::info('Optimize finished');
96                 }
97
98                 DI::config()->set('system', 'cache_last_cleared', time());
99         }
100 }