diff --git a/lib/Cache.php b/lib/Cache.php index ca0e159..5f8e9f6 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -4,6 +4,11 @@ namespace Amp\Dns; interface Cache { + /** + * Default time-to-live - 1 hour + */ + const DEFAULT_TTL = 3600; + /** * Attempt to retrieve a value from the cache * diff --git a/lib/Cache/APCCache.php b/lib/Cache/APCCache.php index a815921..96b0f92 100644 --- a/lib/Cache/APCCache.php +++ b/lib/Cache/APCCache.php @@ -43,6 +43,14 @@ class APCCache extends KeyValueCache */ public function store($name, $type, $addr, $ttl = null) { + if ($addr === null) { + throw new \InvalidArgumentException('Caching null addresses is disallowed'); + } + + if ($ttl === null) { + $ttl = self::DEFAULT_TTL; + } + apc_store($this->generateKey($name, $type), $addr, $ttl); } diff --git a/lib/Cache/MemoryCache.php b/lib/Cache/MemoryCache.php index c6f8e2d..e762dec 100644 --- a/lib/Cache/MemoryCache.php +++ b/lib/Cache/MemoryCache.php @@ -5,10 +5,6 @@ namespace Amp\Dns\Cache; use Amp\Dns\Cache; class MemoryCache implements Cache { - /** - * Default time-to-live - 1 day - */ - const DEFAULT_TTL = 86400; /** * Internal data store for cache values @@ -32,6 +28,8 @@ class MemoryCache implements Cache { $callback(true, $value); return; } + + unset($this->recordsByTypeAndName[$type][$name]); } $callback(false, null); @@ -46,6 +44,10 @@ class MemoryCache implements Cache { * @param int $ttl */ public function store($name, $type, $addr, $ttl = null) { + if ($addr === null) { + throw new \InvalidArgumentException('Caching null addresses is disallowed'); + } + if ($ttl === null) { $ttl = self::DEFAULT_TTL; } diff --git a/lib/Cache/RedisCache.php b/lib/Cache/RedisCache.php index a66000d..79a62f8 100644 --- a/lib/Cache/RedisCache.php +++ b/lib/Cache/RedisCache.php @@ -65,6 +65,14 @@ SCRIPT; * @param int $ttl */ public function store($name, $type, $addr, $ttl = null) { + if ($addr === null) { + throw new \InvalidArgumentException('Caching null addresses is disallowed'); + } + + if ($ttl === null) { + $ttl = self::DEFAULT_TTL; + } + $key = $this->generateKey($name, $type); if ($ttl > 0) {