-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
99 lines (86 loc) · 2.53 KB
/
Copy pathCache.php
File metadata and controls
99 lines (86 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Cymatic;
class Cache
{
// Types of supported cache frameworks
public static $CACHE_TYPE_APC = 'CACHE_TYPE_APC';
public static $CACHE_TYPE_MEMCACHED = 'CACHE_TYPE_MEMCACHED';
public static $CACHE_TYPE_REDIS = 'CACHE_TYPE_REDIS';
/**
* User selected cache type
*
* @var string
*/
protected $cacheType;
/**
* @var object
*/
protected $cacheInstance = null;
/**
* Cache constructor.
*
* @param $cacheType
* @param array $cacheSettings
* @throws \Exception
*/
public function __construct($cacheType, $cacheSettings = array())
{
$this->cacheType = $cacheType;
switch ($this->cacheType) {
case Cache::$CACHE_TYPE_APC:
if (!function_exists('apc_fetch')) {
throw new \Exception("php_apc extension is not installed");
}
break;
case Cache::$CACHE_TYPE_MEMCACHED:
if (!class_exists('Memcached')) {
throw new \Exception("Memcached class is not available, please install php_memcached extension");
}
$this->cacheInstance = new Memcached();
$this->cacheInstance->addServer(...$cacheSettings);
break;
case Cache::$CACHE_TYPE_REDIS:
if (!class_exists('Redis')) {
throw new \Exception("Redis class is not available, please install php_redis extension");
}
$this->cacheInstance = new Redis();
$this->cacheInstance->connect(...$cacheSettings);
break;
}
}
/**
* Set cache
*
* @param $name
* @param $value
*/
public function __set($name, $value)
{
switch ($this->cacheType) {
case Cache::$CACHE_TYPE_APC:
apc_add($name, $value);
break;
case Cache::$CACHE_TYPE_MEMCACHED:
case Cache::$CACHE_TYPE_REDIS:
$this->cacheInstance->set($name, $value);
break;
}
}
/**
* Return cache if exists
*
* @param $name
* @return string
*/
public function __get($name)
{
switch ($this->cacheType) {
case Cache::$CACHE_TYPE_APC:
return apc_fetch($name);
case Cache::$CACHE_TYPE_MEMCACHED:
case Cache::$CACHE_TYPE_REDIS:
return $this->cacheInstance->get($name);
}
return '';
}
}