]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Changing Friendica\App\Mode from static methods to public methods
[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\CacheDriverFactory;
8
9 /**
10  * @brief Class for storing data for a short time
11  */
12 class Cache extends \Friendica\BaseObject
13 {
14         const MONTH        = 2592000;
15         const WEEK         = 604800;
16         const DAY          = 86400;
17         const HOUR         = 3600;
18         const HALF_HOUR    = 1800;
19         const QUARTER_HOUR = 900;
20         const FIVE_MINUTES = 300;
21         const MINUTE       = 60;
22
23         /**
24          * @var Cache\ICacheDriver
25          */
26         private static $driver       = null;
27         public  static $driver_class = null;
28         public  static $driver_name  = null;
29
30         public static function init()
31         {
32                 self::$driver_name  = Config::get('system', 'cache_driver', 'database');
33                 self::$driver       = CacheDriverFactory::create(self::$driver_name);
34                 self::$driver_class = get_class(self::$driver);
35         }
36
37         /**
38          * Returns the current cache driver
39          *
40          * @return Cache\ICacheDriver
41          */
42         private static function getDriver()
43         {
44                 if (self::$driver === null) {
45                         self::init();
46                 }
47
48                 return self::$driver;
49         }
50
51         /**
52          * @brief Returns all the cache keys sorted alphabetically
53          *
54          * @return array|null Null if the driver doesn't support this feature
55          */
56         public static function getAllKeys()
57         {
58                 $time = microtime(true);
59
60                 $return = self::getDriver()->getAllKeys();
61
62                 // Keys are prefixed with the node hostname, let's remove it
63                 array_walk($return, function (&$value) {
64                         $value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value);
65                 });
66
67                 sort($return);
68
69                 self::getApp()->save_timestamp($time, 'cache');
70
71                 return $return;
72         }
73
74         /**
75          * @brief Fetch cached data according to the key
76          *
77          * @param string $key The key to the cached data
78          *
79          * @return mixed Cached $value or "null" if not found
80          */
81         public static function get($key)
82         {
83                 $time = microtime(true);
84
85                 $return = self::getDriver()->get($key);
86
87                 self::getApp()->save_timestamp($time, 'cache');
88
89                 return $return;
90         }
91
92         /**
93          * @brief Put data in the cache according to the key
94          *
95          * The input $value can have multiple formats.
96          *
97          * @param string  $key      The key to the cached data
98          * @param mixed   $value    The value that is about to be stored
99          * @param integer $duration The cache lifespan
100          *
101          * @return bool
102          */
103         public static function set($key, $value, $duration = self::MONTH)
104         {
105                 $time = microtime(true);
106
107                 $return = self::getDriver()->set($key, $value, $duration);
108
109                 self::getApp()->save_timestamp($time, 'cache_write');
110
111                 return $return;
112         }
113
114         /**
115          * @brief Delete a value from the cache
116          *
117          * @param string $key The key to the cached data
118          *
119          * @return bool
120          */
121         public static function delete($key)
122         {
123                 $time = microtime(true);
124
125                 $return = self::getDriver()->delete($key);
126
127                 self::getApp()->save_timestamp($time, 'cache_write');
128
129                 return $return;
130         }
131
132         /**
133          * @brief Remove outdated data from the cache
134          *
135          * @param boolean $outdated just remove outdated values
136          *
137          * @return void
138          */
139         public static function clear($outdated = true)
140         {
141                 return self::getDriver()->clear($outdated);
142         }
143 }