]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Merge pull request #4858 from annando/issue-4655
[friendica.git] / src / Core / Cache.php
1 <?php
2 /**
3  * @file src/Core/Cache.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9
10 /**
11  * @brief Class for storing data for a short time
12  */
13 class Cache extends \Friendica\BaseObject
14 {
15         const MONTH        = 2592000;
16         const WEEK         = 604800;
17         const DAY          = 86400;
18         const HOUR         = 3600;
19         const HALF_HOUR    = 1800;
20         const QUARTER_HOUR = 900;
21         const FIVE_MINUTES = 300;
22         const MINUTE       = 60;
23
24         /**
25          * @var Cache\ICacheDriver
26          */
27         static $driver = null;
28
29         public static function init()
30         {
31                 switch(Config::get('system', 'cache_driver', 'database')) {
32                         case 'memcache':
33                                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
34                                 $memcache_port = Config::get('system', 'memcache_port', 11211);
35
36                                 self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
37                                 break;
38                         case 'memcached':
39                                 $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
40
41                                 self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
42                                 break;
43                         default:
44                                 self::$driver = new Cache\DatabaseCacheDriver();
45                 }
46         }
47
48         /**
49          * Returns the current cache driver
50          *
51          * @return Cache\ICacheDriver
52          */
53         private static function getDriver()
54         {
55                 if (self::$driver === null) {
56                         self::init();
57                 }
58
59                 return self::$driver;
60         }
61
62         /**
63          * @brief Fetch cached data according to the key
64          *
65          * @param string $key The key to the cached data
66          *
67          * @return mixed Cached $value or "null" if not found
68          */
69         public static function get($key)
70         {
71                 $time = microtime(true);
72
73                 $return = self::getDriver()->get($key);
74
75                 self::getApp()->save_timestamp($time, 'cache');
76
77                 return $return;
78         }
79
80         /**
81          * @brief Put data in the cache according to the key
82          *
83          * The input $value can have multiple formats.
84          *
85          * @param string  $key      The key to the cached data
86          * @param mixed   $value    The value that is about to be stored
87          * @param integer $duration The cache lifespan
88          *
89          * @return bool
90          */
91         public static function set($key, $value, $duration = self::MONTH)
92         {
93                 $time = microtime(true);
94
95                 $return = self::getDriver()->set($key, $value, $duration);
96
97                 self::getApp()->save_timestamp($time, 'cache_write');
98
99                 return $return;
100         }
101
102         /**
103          * @brief Delete a value from the cache
104          *
105          * @param string $key The key to the cached data
106          *
107          * @return bool
108          */
109         public static function delete($key)
110         {
111                 $time = microtime(true);
112
113                 $return = self::getDriver()->delete($key);
114
115                 self::getApp()->save_timestamp($time, 'cache_write');
116
117                 return $return;
118         }
119
120         /**
121          * @brief Remove outdated data from the cache
122          *
123          * @param integer $max_level The maximum cache level that is to be cleared
124          *
125          * @return void
126          */
127         public static function clear()
128         {
129                 return self::getDriver()->clear();
130         }
131 }