1 | <?php |
||
2 | |||
3 | namespace LeKoala\DevToolkit\Tasks; |
||
4 | |||
5 | use stdClass; |
||
6 | use Predis\Client; |
||
7 | use Predis\Command\ServerInfo; |
||
8 | use SilverStripe\Dev\BuildTask; |
||
9 | use LeKoala\DevToolkit\BuildTaskTools; |
||
10 | use SilverStripe\Core\Injector\Injector; |
||
11 | use LeKoala\Base\Cache\RedisCacheFactory; |
||
12 | |||
13 | /** |
||
14 | */ |
||
15 | class TestCacheSupportTask extends BuildTask |
||
16 | { |
||
17 | use BuildTaskTools; |
||
18 | |||
19 | protected $title = "Test Cache Support"; |
||
20 | protected $description = 'Check what cache backends are available and working.'; |
||
21 | private static $segment = 'TestCacheSupportTask'; |
||
22 | |||
23 | public function run($request) |
||
24 | { |
||
25 | $this->request = $request; |
||
26 | |||
27 | $this->testMemcache(); |
||
28 | $this->testOpcache(); |
||
29 | $this->testRedis(); |
||
30 | } |
||
31 | |||
32 | protected function testRedis() |
||
33 | { |
||
34 | $predis = new Client('tcp://127.0.0.1:6379'); |
||
35 | $this->message($predis->executeCommand(new ServerInfo)); |
||
36 | |||
37 | $args = []; |
||
38 | $redisCache = Injector::inst()->createWithArgs(RedisCacheFactory::class, $args); |
||
39 | $this->message($redisCache); |
||
40 | } |
||
41 | |||
42 | protected function testOpcache() |
||
43 | { |
||
44 | if (!function_exists('opcache_get_status')) { |
||
45 | $this->msg("opcache_get_status function is not defined"); |
||
46 | } |
||
47 | |||
48 | $result = opcache_get_status(); |
||
49 | if ($result) { |
||
50 | $this->msg("Opcache is active"); |
||
51 | |||
52 | echo '<pre>'; |
||
53 | print_r($result); |
||
54 | echo '</pre>'; |
||
55 | } else { |
||
56 | $this->msg("Opcache is disabled. It should be enabled to ensure optimal performances", "error"); |
||
57 | } |
||
58 | } |
||
59 | protected function testMemcache() |
||
60 | { |
||
61 | if (!class_exists('Memcache')) { |
||
62 | $this->msg("Memcache class does not exist. Make sure that the Memcache extension is installed"); |
||
63 | } |
||
64 | |||
65 | $host = defined('MEMCACHE_HOST') ? MEMCACHE_HOST : 'localhost'; |
||
66 | $port = defined('MEMCACHE_PORT') ? MEMCACHE_PORT : 11211; |
||
67 | |||
68 | $memcache = new \Memcache; |
||
69 | $connected = $memcache->connect($host, $port); |
||
70 | |||
71 | if ($connected) { |
||
72 | $this->msg("Server's version: " . $memcache->getVersion()); |
||
73 | |||
74 | $result = $memcache->get("key"); |
||
75 | |||
76 | if ($result) { |
||
77 | $this->msg("Data found in cache"); |
||
78 | } else { |
||
79 | $this->msg("Data not found in cache"); |
||
80 | $tmp_object = new stdClass; |
||
81 | $tmp_object->str_attr = "test"; |
||
82 | $tmp_object->int_attr = 123; |
||
83 | $tmp_object->time = time(); |
||
84 | $tmp_object->date = date('Y-m-d H:i:s'); |
||
85 | $tmp_object->arr = array(1, 2, 3); |
||
86 | $memcache->set("key", $tmp_object, false, 10); |
||
87 | } |
||
88 | |||
89 | $this->msg("Store data in the cache (data will expire in 10 seconds)"); |
||
90 | $this->msg("Data from the cache:"); |
||
91 | echo '<pre>'; |
||
92 | var_dump($memcache->get("key")); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
93 | echo '</pre>'; |
||
94 | } else { |
||
95 | $this->msg("Failed to connect"); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 |