forked from snubes/backend_code_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheManager.php
118 lines (89 loc) · 3.06 KB
/
CacheManager.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Created by PhpStorm.
* User: isnain
* Date: 09.08.21
* Time: 10:14
*/
interface CacheManagerInterface{
public function connect(string $host,string $port);
public function set(string $key, string $value, string $is_compressed = null, string $ttl = null);
public function get(string $key): string;
public function lpush(string $key, string $value);
}
class RedisCacheConnector {
private $reids;
public function __construct() {
$this->reids = new \Redis();
}
public function connect(string $host,string $port) {
$this->reids->connect($host,$port);
}
public function lpush(string $key, string $value) {
$this->reids->lPush($key,$value);
}
}
class MemCacheConnector {
private $memcache;
public function __construct() {
$this->memcache = new \Memcache();
}
public function connect(string $host,string $port): void {
$this->memcache->connect($host,$port);
}
public function lpush(string $key, string $value) {
throw new \Exception("method not supported");
}
}
class CacheConnectionFactory {
private const REDIS = 'redis';
private const MEMCACHED = 'memcached';
public static function connect(string $driverName = "redis") {
if (strtolower($driverName) === self::REDIS) {
return new RedisConnector();
} else if (strtolower($driverName) === self::MEMCACHED) {
return new MemCacheConnector();
} else {
throw new Exception('Invalid Driver');
}
return null;
}
}
class CacheManager implements CacheManagerInterface{
private $cacheConnector;
public function __construct($cacheConnector) {
$this->cacheConnector = $cacheConnector;
// or you can use connection factory
$connection = 'redis'; // this variable should be passed to construct
$this->cacheConnector = CacheConnectionFactory::connect($connection);
}
public function connect(string $host,string $port): self {
$this->cacheConnector->connect($host,$port);
return $this;
}
public function set(string $key, string $value, string $is_compressed = null, string $ttl = null): self {
$this->cacheConnector->set($key, $value, $is_compressed, $ttl);
return $this;
}
public function get(string $key): string {
return $this->cacheConnector->get($key);
}
public function lpush(string $key, string $value): self {
$this->cacheConnector->lpush($key, $value);
return $this;
}
}
// if you want to use connection factory then following line
$redisCache2 = new CacheManager('redis');
$redisCache = new CacheManager(new RedisCacheConnector());
$redisCache->connect('somehost','121')
->set('one','1')
->lpush('two','1')
->lpush('two','2');
echo $redisCache->get('one');
$memCache = new CacheManager(new MemCacheConnector());
$memCache->connect('somehost','121')
->set('one','1')
->lpush('two','2'); // generates exception
echo $memCache->get('one');
?>