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