]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Merge remote-tracking branch 'upstream/develop' into public-redir
[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                         case 'redis':
44                                 $redis_host = Config::get('system', 'redis_host', '127.0.0.1');
45                                 $redis_port = Config::get('system', 'redis_port', 6379);
46
47                                 self::$driver = new Cache\RedisCacheDriver($redis_host, $redis_port);
48                                 break;
49                         default:
50                                 self::$driver = new Cache\DatabaseCacheDriver();
51                 }
52         }
53
54         /**
55          * Returns the current cache driver
56          *
57          * @return Cache\ICacheDriver
58          */
59         private static function getDriver()
60         {
61                 if (self::$driver === null) {
62                         self::init();
63                 }
64
65                 return self::$driver;
66         }
67
68         /**
69          * @brief Fetch cached data according to the key
70          *
71          * @param string $key The key to the cached data
72          *
73          * @return mixed Cached $value or "null" if not found
74          */
75         public static function get($key)
76         {
77                 $time = microtime(true);
78
79                 $return = self::getDriver()->get($key);
80
81                 self::getApp()->save_timestamp($time, 'cache');
82
83                 return $return;
84         }
85
86         /**
87          * @brief Put data in the cache according to the key
88          *
89          * The input $value can have multiple formats.
90          *
91          * @param string  $key      The key to the cached data
92          * @param mixed   $value    The value that is about to be stored
93          * @param integer $duration The cache lifespan
94          *
95          * @return bool
96          */
97         public static function set($key, $value, $duration = self::MONTH)
98         {
99                 $time = microtime(true);
100
101                 $return = self::getDriver()->set($key, $value, $duration);
102
103                 self::getApp()->save_timestamp($time, 'cache_write');
104
105                 return $return;
106         }
107
108         /**
109          * @brief Delete a value from the cache
110          *
111          * @param string $key The key to the cached data
112          *
113          * @return bool
114          */
115         public static function delete($key)
116         {
117                 $time = microtime(true);
118
119                 $return = self::getDriver()->delete($key);
120
121                 self::getApp()->save_timestamp($time, 'cache_write');
122
123                 return $return;
124         }
125
126         /**
127          * @brief Remove outdated data from the cache
128          *
129          * @param integer $max_level The maximum cache level that is to be cleared
130          *
131          * @return void
132          */
133         public static function clear()
134         {
135                 return self::getDriver()->clear();
136         }
137 }