]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Merge pull request #6638 from Ixiter/develop-markdown-anchors
[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         const INFINITE     = 0;
23
24         /**
25          * @var Cache\ICacheDriver
26          */
27         private static $driver       = null;
28         public  static $driver_class = null;
29         public  static $driver_name  = null;
30
31         public static function init()
32         {
33                 self::$driver_name  = Config::get('system', 'cache_driver', 'database');
34                 self::$driver       = CacheDriverFactory::create(self::$driver_name);
35                 self::$driver_class = get_class(self::$driver);
36         }
37
38         /**
39          * Returns the current cache driver
40          *
41          * @return Cache\ICacheDriver
42          */
43         private static function getDriver()
44         {
45                 if (self::$driver === null) {
46                         self::init();
47                 }
48
49                 return self::$driver;
50         }
51
52         /**
53          * @brief Returns all the cache keys sorted alphabetically
54          *
55          * @param string $prefix Prefix of the keys (optional)
56          *
57          * @return array Empty if the driver doesn't support this feature
58          * @throws \Exception
59          */
60         public static function getAllKeys($prefix = null)
61         {
62                 $time = microtime(true);
63
64                 $return = self::getDriver()->getAllKeys($prefix);
65
66                 self::getApp()->saveTimestamp($time, 'cache');
67
68                 return $return;
69         }
70
71         /**
72          * @brief Fetch cached data according to the key
73          *
74          * @param string $key The key to the cached data
75          *
76          * @return mixed Cached $value or "null" if not found
77          * @throws \Exception
78          */
79         public static function get($key)
80         {
81                 $time = microtime(true);
82
83                 $return = self::getDriver()->get($key);
84
85                 self::getApp()->saveTimestamp($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          * @throws \Exception
101          */
102         public static function set($key, $value, $duration = self::MONTH)
103         {
104                 $time = microtime(true);
105
106                 $return = self::getDriver()->set($key, $value, $duration);
107
108                 self::getApp()->saveTimestamp($time, 'cache_write');
109
110                 return $return;
111         }
112
113         /**
114          * @brief Delete a value from the cache
115          *
116          * @param string $key The key to the cached data
117          *
118          * @return bool
119          * @throws \Exception
120          */
121         public static function delete($key)
122         {
123                 $time = microtime(true);
124
125                 $return = self::getDriver()->delete($key);
126
127                 self::getApp()->saveTimestamp($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 bool
138          */
139         public static function clear($outdated = true)
140         {
141                 return self::getDriver()->clear($outdated);
142         }
143 }