]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
58969f0819f4a25498c20c518018e093d7fd5093
[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
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_host = Config::get('system', 'memcached_host', '127.0.0.1');
40                                 $memcached_port = Config::get('system', 'memcached_port', 11211);
41
42                                 self::$driver = new Cache\MemcachedCacheDriver($memcached_host, $memcached_port);
43                                 break;
44                         default:
45                                 self::$driver = new Cache\DatabaseCacheDriver();
46                 }
47         }
48
49         /**
50          * @brief Return the duration for a given cache level
51          *
52          * @param integer $level Cache level
53          *
54          * @return integer The cache duration in seconds
55          */
56         public static function duration($level)
57         {
58                 switch ($level) {
59                         case self::MONTH:
60                                 $seconds = 2592000;
61                                 break;
62                         case self::WEEK:
63                                 $seconds = 604800;
64                                 break;
65                         case self::DAY:
66                                 $seconds = 86400;
67                                 break;
68                         case self::HOUR:
69                                 $seconds = 3600;
70                                 break;
71                         case self::HALF_HOUR:
72                                 $seconds = 1800;
73                                 break;
74                         case self::QUARTER_HOUR:
75                                 $seconds = 900;
76                                 break;
77                         case self::FIVE_MINUTES:
78                                 $seconds = 300;
79                                 break;
80                         case self::MINUTE:
81                         default:
82                                 $seconds = 60;
83                                 break;
84                 }
85                 return $seconds;
86         }
87
88         /**
89          * Returns the current cache driver
90          *
91          * @return Cache\ICacheDriver
92          */
93         private static function getDriver()
94         {
95                 if (self::$driver === null) {
96                         self::init();
97                 }
98
99                 return self::$driver;
100         }
101
102         /**
103          * @brief Fetch cached data according to the key
104          *
105          * @param string $key The key to the cached data
106          *
107          * @return mixed Cached $value or "null" if not found
108          */
109         public static function get($key)
110         {
111                 return self::getDriver()->get($key);
112         }
113
114         /**
115          * @brief Put data in the cache according to the key
116          *
117          * The input $value can have multiple formats.
118          *
119          * @param string  $key      The key to the cached data
120          * @param mixed   $value    The value that is about to be stored
121          * @param integer $duration The cache lifespan
122          *
123          * @return bool
124          */
125         public static function set($key, $value, $duration = self::MONTH)
126         {
127                 return self::getDriver()->set($key, $value, $duration);
128         }
129
130         /**
131          * @brief Remove outdated data from the cache
132          *
133          * @param integer $max_level The maximum cache level that is to be cleared
134          *
135          * @return void
136          */
137         public static function clear()
138         {
139                 return self::getDriver()->clear();
140         }
141 }