1
0
mirror of https://github.com/danog/dns.git synced 2024-11-26 20:14:51 +01:00

Add CacheFactory

This commit is contained in:
Chris Wright 2014-11-26 16:02:43 +00:00
parent 4dd32347a0
commit 980c7c9130
2 changed files with 24 additions and 13 deletions

23
lib/CacheFactory.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Amp\Dns;
use Amp\Dns\Cache\MemoryCache;
use Amp\Dns\Cache\APCCache;
class CacheFactory
{
/**
* Get an instance of the best available caching back-end that does not have any dependencies
*
* @return Cache
*/
public function select()
{
if (extension_loaded('apc') && ini_get("apc.enabled") && @apc_cache_info()) {
return new APCCache;
}
return new MemoryCache;
}
}

View File

@ -5,8 +5,6 @@ namespace Amp\Dns;
use Amp\Reactor;
use Amp\Failure;
use Amp\Future;
use Amp\Dns\Cache\MemoryCache;
use Amp\Dns\Cache\APCCache;
class Client {
const OP_MS_REQUEST_TIMEOUT = 0b0001;
@ -103,17 +101,7 @@ class Client {
$this->reactor = $reactor ?: \Amp\reactor();
$this->requestBuilder = $requestBuilder ?: new RequestBuilder;
$this->responseInterpreter = $responseInterpreter ?: new ResponseInterpreter;
if (!$cache) {
if (extension_loaded('apc') && ini_get("apc.enabled") && @apc_cache_info()) {
$cache = new APCCache;
}
else {
$cache = new MemoryCache;
}
}
$this->cache = $cache;
$this->cache = $cache ?: (new CacheFactory)->select();
}
/**