]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
a1b1ecb9c171c199d47b58443a885f71c9c04a87
[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;
8 use Friendica\Core\Config;
9
10 /**
11  * @brief Class for storing data for a short time
12  */
13 class Cache extends \Friendica\BaseObject
14 {
15         const MONTH        = 0;
16         const WEEK         = 1;
17         const DAY          = 2;
18         const HOUR         = 3;
19         const HALF_HOUR    = 4;
20         const QUARTER_HOUR = 5;
21         const FIVE_MINUTES = 6;
22         const MINUTE       = 7;
23
24         /**
25          * @var Cache\ICacheDriver
26          */
27         static $driver = null;
28
29         public static function init()
30         {
31                 switch(Config::get('system', 'cache_driver', 'database')) {
32                         case 'memcache':
33                                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
34                                 $memcache_port = Config::get('system', 'memcache_port', 11211);
35
36                                 self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
37                                 break;
38                         case 'memcached':
39                                 $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
40
41                                 self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
42                                 break;
43                         default:
44                                 self::$driver = new Cache\DatabaseCacheDriver();
45                 }
46         }
47
48         /**
49          * @brief Return the duration for a given cache level
50          *
51          * @param integer $level Cache level
52          *
53          * @return integer The cache duration in seconds
54          */
55         public static function duration($level)
56         {
57                 switch ($level) {
58                         case self::MONTH:
59                                 $seconds = 2592000;
60                                 break;
61                         case self::WEEK:
62                                 $seconds = 604800;
63                                 break;
64                         case self::DAY:
65                                 $seconds = 86400;
66                                 break;
67                         case self::HOUR:
68                                 $seconds = 3600;
69                                 break;
70                         case self::HALF_HOUR:
71                                 $seconds = 1800;
72                                 break;
73                         case self::QUARTER_HOUR:
74                                 $seconds = 900;
75                                 break;
76                         case self::FIVE_MINUTES:
77                                 $seconds = 300;
78                                 break;
79                         case self::MINUTE:
80                         default:
81                                 $seconds = 60;
82                                 break;
83                 }
84                 return $seconds;
85         }
86
87         /**
88          * Returns the current cache driver
89          *
90          * @return Cache\ICacheDriver
91          */
92         private static function getDriver()
93         {
94                 if (self::$driver === null) {
95                         self::init();
96                 }
97
98                 return self::$driver;
99         }
100
101         /**
102          * @brief Fetch cached data according to the key
103          *
104          * @param string $key The key to the cached data
105          *
106          * @return mixed Cached $value or "null" if not found
107          */
108         public static function get($key)
109         {
110                 $time = microtime(true);
111
112                 $return = self::getDriver()->get($key);
113
114                 self::getApp()->save_timestamp($time, 'cache');
115
116                 return $return;
117         }
118
119         /**
120          * @brief Put data in the cache according to the key
121          *
122          * The input $value can have multiple formats.
123          *
124          * @param string  $key      The key to the cached data
125          * @param mixed   $value    The value that is about to be stored
126          * @param integer $duration The cache lifespan
127          *
128          * @return bool
129          */
130         public static function set($key, $value, $duration = self::MONTH)
131         {
132                 $time = microtime(true);
133
134                 $return = self::getDriver()->set($key, $value, $duration);
135
136                 self::getApp()->save_timestamp($time, 'cache_write');
137
138                 return $return;
139         }
140
141         /**
142          * @brief Remove outdated data from the cache
143          *
144          * @param integer $max_level The maximum cache level that is to be cleared
145          *
146          * @return void
147          */
148         public static function clear()
149         {
150                 return self::getDriver()->clear();
151         }
152 }