]> git.mxchange.org Git - friendica.git/blob - include/cache.php
Merge pull request #2486 from rabuzarus/2704_xml
[friendica.git] / include / cache.php
1 <?php
2         /**
3          *  cache api
4          */
5
6         class Cache {
7                 public static function get($key) {
8
9                         $r = q("SELECT `v` FROM `cache` WHERE `k`='%s' limit 1",
10                                 dbesc($key)
11                         );
12
13                         if (count($r))
14                                 return $r[0]['v'];
15
16                         return null;
17                 }
18
19                 public static function set($key,$value, $duration = CACHE_MONTH) {
20
21                         q("REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
22                                         dbesc($key),
23                                         dbesc($value),
24                                         intval($duration),
25                                         dbesc(datetime_convert()));
26
27                 }
28
29
30 /*
31  *
32  * Leaving this legacy code temporaily to see how REPLACE fares
33  * as opposed to non-atomic checks when faced with fast moving key duplication.
34  * As a MySQL extension it isn't portable, but we're not yet very portable.
35  */
36
37 /*
38  *                      $r = q("SELECT * FROM `cache` WHERE `k`='%s' limit 1",
39  *                              dbesc($key)
40  *                      );
41  *                      if(count($r)) {
42  *                              q("UPDATE `cache` SET `v` = '%s', `updated = '%s' WHERE `k` = '%s'",
43  *                                      dbesc($value),
44  *                                      dbesc(datetime_convert()),
45  *                                      dbesc($key));
46  *                      }
47  *                      else {
48  *                              q("INSERT INTO `cache` (`k`,`v`,`updated`) VALUES ('%s','%s','%s')",
49  *                                      dbesc($key),
50  *                                      dbesc($value),
51  *                                      dbesc(datetime_convert()));
52  *                      }
53  *              }
54  */
55
56
57                 public static function clear(){
58                         q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
59                                 dbesc(datetime_convert('UTC','UTC',"now - 30 days")), intval(CACHE_MONTH));
60
61                         q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
62                                 dbesc(datetime_convert('UTC','UTC',"now - 7 days")), intval(CACHE_WEEK));
63
64                         q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
65                                 dbesc(datetime_convert('UTC','UTC',"now - 1 days")), intval(CACHE_DAY));
66
67                         q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
68                                 dbesc(datetime_convert('UTC','UTC',"now - 1 hours")), intval(CACHE_HOUR));
69                 }
70
71         }
72