]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Merge pull request #5907 from nupplaphil/goaway_to_redirectto
[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          * @param string $prefix Prefix of the keys (optional)
55          *
56          * @return array Empty if the driver doesn't support this feature
57          */
58         public static function getAllKeys($prefix = null)
59         {
60                 $time = microtime(true);
61
62                 $return = self::getDriver()->getAllKeys($prefix);
63
64                 self::getApp()->saveTimestamp($time, 'cache');
65
66                 return $return;
67         }
68
69         /**
70          * @brief Fetch cached data according to the key
71          *
72          * @param string $key The key to the cached data
73          *
74          * @return mixed Cached $value or "null" if not found
75          */
76         public static function get($key)
77         {
78                 $time = microtime(true);
79
80                 $return = self::getDriver()->get($key);
81
82                 self::getApp()->saveTimestamp($time, 'cache');
83
84                 return $return;
85         }
86
87         /**
88          * @brief Put data in the cache according to the key
89          *
90          * The input $value can have multiple formats.
91          *
92          * @param string  $key      The key to the cached data
93          * @param mixed   $value    The value that is about to be stored
94          * @param integer $duration The cache lifespan
95          *
96          * @return bool
97          */
98         public static function set($key, $value, $duration = self::MONTH)
99         {
100                 $time = microtime(true);
101
102                 $return = self::getDriver()->set($key, $value, $duration);
103
104                 self::getApp()->saveTimestamp($time, 'cache_write');
105
106                 return $return;
107         }
108
109         /**
110          * @brief Delete a value from the cache
111          *
112          * @param string $key The key to the cached data
113          *
114          * @return bool
115          */
116         public static function delete($key)
117         {
118                 $time = microtime(true);
119
120                 $return = self::getDriver()->delete($key);
121
122                 self::getApp()->saveTimestamp($time, 'cache_write');
123
124                 return $return;
125         }
126
127         /**
128          * @brief Remove outdated data from the cache
129          *
130          * @param boolean $outdated just remove outdated values
131          *
132          * @return void
133          */
134         public static function clear($outdated = true)
135         {
136                 return self::getDriver()->clear($outdated);
137         }
138 }