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