-
Notifications
You must be signed in to change notification settings - Fork 2
/
CacheManager.php
77 lines (54 loc) · 1.58 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
<?php
/**
* Created by PhpStorm.
* User: isnain
* Date: 09.08.21
* Time: 10:14
*/
class CacheManager
{
private $cache;
public function setCache(string $cachingSystem)
{
switch ($cachingSystem){
case "redis":
$this->cache=new \Redis();
break;
case "memcache":
$this->cache=new \Memcache();
break;
default:
throw new \Exception("Cache Manager Not Found");
}
}
public function connect(string $host, string $port){
$this->cache->connect($host,$port);
}
public function set(string $key, string $value, string $is_compressed=null, string $ttl=null){
if($this->cache instanceof \Memcache)
$this->cache->set($key,$value,$is_compressed,$ttl);
else if($this->cache instanceof \Redis)
$this->cache->set($key,$value,$ttl);
}
public function get(string $key){
return $this->cache->get($key);
}
public function lpush(string $key, string $value){
if($this->cache instanceof \Memcache)
throw new \Exception("method not supported");
else if($this->cache instanceof \Redis)
$this->cache->lPush($key,$value);
}
}
$cm=new CacheManager();
$cm->setCache('redis');
$cm->connect('somehost','121');
$cm->set('one','1');
$cm->lpush('two','1');
$cm->lpush('two','2');
echo $cm->get('one');
$cm->setCache('memcache');
$cm->connect('somehost','121');
$cm->set('one','1');
$cm->lpush('two','2'); // generates exception
echo $cm->get('one');