1
|
|
|
<?php /** MicroCache */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Cache; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Base\IContainer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Cache class file. |
10
|
|
|
* |
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
12
|
|
|
* @link https://github.com/lugnsk/micro |
13
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
14
|
|
|
* @license /LICENSE |
15
|
|
|
* @package Micro |
16
|
|
|
* @subpackage Cache |
17
|
|
|
* @version 1.0 |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Cache |
21
|
|
|
{ |
22
|
|
|
/** @var array $drivers Supported drivers */ |
23
|
|
|
protected static $drivers = [ |
24
|
|
|
'array' => '\\Micro\\Cache\\ArrayCache', |
25
|
|
|
'apc' => '\\Micro\\Cache\\ApcCache', |
26
|
|
|
'file' => '\\Micro\\Cache\\FileCache', |
27
|
|
|
'memcache' => '\\Micro\\Cache\\MemcacheCache', |
28
|
|
|
'memcached' => '\\Micro\\Cache\\MemcacheCache', |
29
|
|
|
'redis' => '\\Micro\\Cache\\RedisCache', |
30
|
|
|
'wincache' => '\\Micro\\Cache\\WincacheCache', |
31
|
|
|
'xcache' => '\\Micro\\Cache\\XcacheCache' |
32
|
|
|
]; |
33
|
|
|
/** @var array $servers Activated servers */ |
34
|
|
|
protected $servers = []; |
35
|
|
|
/** @var IContainer $container Config container */ |
36
|
|
|
protected $container; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor is a initialize Caches |
41
|
|
|
* |
42
|
|
|
* @access public |
43
|
|
|
* |
44
|
|
|
* @param array $config Caching config |
45
|
|
|
* |
46
|
|
|
* @result void |
47
|
|
|
* @throws Exception |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $config = []) |
50
|
|
|
{ |
51
|
|
|
$this->container = $config['container']; |
52
|
|
|
|
53
|
|
|
if (empty($config['servers'])) { |
54
|
|
|
throw new Exception('Caching not configured'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($config['servers'] AS $key => $server) { |
58
|
|
|
if (array_key_exists($server['driver'], array_keys(self::$drivers))) { |
59
|
|
|
$this->servers[$key] = new self::$drivers[$server['driver']] ( |
60
|
|
|
array_merge($server, ['container' => $this->container]) |
61
|
|
|
); |
62
|
|
|
} else { |
63
|
|
|
throw new Exception('Cache driver `' . $server['driver'] . '` not found'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get cache server by name |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* |
73
|
|
|
* @param string $driver server name |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
* @throws Exception |
77
|
|
|
*/ |
78
|
|
|
public function get($driver = null) |
79
|
|
|
{ |
80
|
|
|
if (!$driver) { |
|
|
|
|
81
|
|
|
return $this->servers[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (in_array($driver, $this->servers, true)) { |
85
|
|
|
return $this->servers[$driver]; |
86
|
|
|
} else { |
87
|
|
|
throw new Exception('Cache `' . $driver . '` not found.'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: