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