]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Update use statement lists with new Friendica\Database\dba class
[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 Fetch cached data according to the key
51          *
52          * @param string $key The key to the cached data
53          *
54          * @return mixed Cached $value or "null" if not found
55          */
56         public static function get($key)
57         {
58                 $time = microtime(true);
59
60                 $return = self::getDriver()->get($key);
61
62                 self::getApp()->save_timestamp($time, 'cache');
63
64                 return $return;
65         }
66
67         /**
68          * @brief Put data in the cache according to the key
69          *
70          * The input $value can have multiple formats.
71          *
72          * @param string  $key      The key to the cached data
73          * @param mixed   $value    The value that is about to be stored
74          * @param integer $duration The cache lifespan
75          *
76          * @return bool
77          */
78         public static function set($key, $value, $duration = self::MONTH)
79         {
80                 $time = microtime(true);
81
82                 $return = self::getDriver()->set($key, $value, $duration);
83
84                 self::getApp()->save_timestamp($time, 'cache_write');
85
86                 return $return;
87         }
88
89         /**
90          * @brief Delete a value from the cache
91          *
92          * @param string $key The key to the cached data
93          *
94          * @return bool
95          */
96         public static function delete($key)
97         {
98                 $time = microtime(true);
99
100                 $return = self::getDriver()->delete($key);
101
102                 self::getApp()->save_timestamp($time, 'cache_write');
103
104                 return $return;
105         }
106
107         /**
108          * @brief Remove outdated data from the cache
109          *
110          * @param integer $max_level The maximum cache level that is to be cleared
111          *
112          * @return void
113          */
114         public static function clear()
115         {
116                 return self::getDriver()->clear();
117         }
118 }