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