Skip to content

Commit

Permalink
Optimize cache system and number of external requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHoaro committed Apr 17, 2017
1 parent 1c069ef commit 3a4f93d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
61 changes: 36 additions & 25 deletions src/Favicon/Favicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Favicon
{
protected static $TYPE_CACHE_URL = 'url';
protected static $TYPE_CACHE_IMG = 'img';
protected $url = '';
protected $cacheDir;
protected $cacheTimeout;
Expand Down Expand Up @@ -140,29 +142,29 @@ public function get($url = '', $type = FaviconDLType::HOTLINK_URL)
}

// Get the base URL without the path for clearer concatenations.
$original = rtrim($this->baseUrl($this->url, true), '/');
$url = rtrim($this->endRedirect($this->baseUrl($this->url, false)), '/');

if(($favicon = $this->checkCache($url)) || ($favicon = $this->getFavicon($url))) {
$base = true;
}
elseif(($favicon = $this->checkCache($original)) || ($favicon = $this->getFavicon($original, false))) {
$base = false;
}
else {
return false;
$url = rtrim($this->baseUrl($this->url, true), '/');
$original = $url;
if (($favicon = $this->checkCache($original, self::$TYPE_CACHE_URL)) === false
&& ! $favicon = $this->getFavicon($original, false)
) {
$url = rtrim($this->endRedirect($this->baseUrl($this->url, false)), '/');
if (($favicon = $this->checkCache($url, self::$TYPE_CACHE_URL)) === false
&& ! $favicon = $this->getFavicon($url)
) {
$url = $original;
}
}

$this->saveCache($base ? $url : $original, $favicon);
$this->saveCache($url, $favicon, self::$TYPE_CACHE_URL);

switch ($type) {
case FaviconDLType::DL_FILE_PATH:
return $this->getImage($favicon, false);
return $this->getImage($url, $favicon, false);
case FaviconDLType::RAW_IMAGE:
return $this->getImage($favicon, true);
return $this->getImage($url, $favicon, true);
case FaviconDLType::HOTLINK_URL:
default:
return $favicon;
return empty($favicon) ? false : $favicon;
}
}

Expand Down Expand Up @@ -204,25 +206,29 @@ private function getFavicon($url, $checkDefault = true) {
/**
* Find remote favicon and return it as an image
*/
private function getImage($faviconUrl = '', $image = false)
private function getImage($url, $faviconUrl = '', $image = false)
{
$favicon = $this->checkCache($faviconUrl);
if (empty($faviconUrl)) {
return false;
}

$favicon = $this->checkCache($url, self::$TYPE_CACHE_IMG);
// Favicon not found in the cache
if( $favicon === false ) {
$favicon = $this->dataAccess->retrieveUrl($faviconUrl);
// Definitely not found
if (!$this->checkImageMTypeContent($favicon)) {
return false;
} else {
$this->saveCache($faviconUrl, $favicon);
$this->saveCache($url, $favicon, self::$TYPE_CACHE_IMG);
}
}

if( $image ) {
return $favicon;
}
else
return md5($faviconUrl);
return self::$TYPE_CACHE_IMG . md5($url);
}

/**
Expand Down Expand Up @@ -265,10 +271,12 @@ private function getInPage($url) {
return false;
}

private function checkCache($url) {
private function checkCache($url, $type) {
if ($this->cacheTimeout) {
$cache = $this->cacheDir . '/' . md5($url);
if (file_exists($cache) && is_readable($cache) && (time() - filemtime($cache) < $this->cacheTimeout)) {
$cache = $this->cacheDir . '/'. $type . md5($url);
if (file_exists($cache) && is_readable($cache)
&& ($this->cacheTimeout === -1 || time() - filemtime($cache) < $this->cacheTimeout)
) {
return $this->dataAccess->readCache($cache);
}
}
Expand All @@ -279,12 +287,15 @@ private function checkCache($url) {
* Will save data in cacheDir if the directory writable and any previous cache is expired (cacheTimeout)
* @param $url
* @param $data
* @param $type
* @return string cache file path
*/
private function saveCache($url, $data) {
private function saveCache($url, $data, $type) {
// Save cache if necessary
$cache = $this->cacheDir . '/' . md5($url);
if ($this->cacheTimeout && !file_exists($cache) || (is_writable($cache) && time() - filemtime($cache) > $this->cacheTimeout)) {
$cache = $this->cacheDir . '/'. $type . md5($url);
if ($this->cacheTimeout && !file_exists($cache)
|| (is_writable($cache) && $this->cacheTimeout !== -1 && time() - filemtime($cache) > $this->cacheTimeout)
) {
$this->dataAccess->saveCache($cache, $data);
}
return $cache;
Expand Down
8 changes: 5 additions & 3 deletions tests/Favicon/FaviconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testInfoRedirect() {
* @covers Favicon::get
* @uses Favicon
*/
public function testGetExistingRootFavicon() {
public function testGetExistingFavicon() {
$url = 'http://domain.tld/';
$path = 'sub/';

Expand All @@ -214,7 +214,7 @@ public function testGetExistingRootFavicon() {

// Get from URL MOCK
$dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnCallback(array($this, 'contentExistingFav')));
$this->assertEquals(self::slash($url) . self::TEST_LOGO_NAME, $fav->get());
$this->assertEquals(self::slash($url . $path) . self::TEST_LOGO_NAME, $fav->get());
}

/**
Expand Down Expand Up @@ -374,6 +374,8 @@ public function testGetValidFavNoCacheSetup() {
$dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnValue(file_get_contents($this->RESOURCE_FAV_ICO)));

$this->assertEquals(self::slash($url) . self::DEFAULT_FAV_CHECK, $fav->get());
directory_clear(__DIR__ .'/../../resources/cache');
touch(__DIR__ .'/../../resources/cache/.gitkeep');
}

public function testGetDownloadedFavPath()
Expand All @@ -391,7 +393,7 @@ public function testGetDownloadedFavPath()
$dataAccess->expects($this->any())->method('retrieveHeader')->will($this->returnValue(array(0 => 'HTTP/1.1 200 OK')));
$dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnValue(file_get_contents($this->RESOURCE_FAV_ICO)));

$expected = md5('http://domain.tld/'. self::DEFAULT_FAV_CHECK);
$expected = 'img'. md5('http://domain.tld');
$this->assertEquals($expected, $fav->get('', FaviconDLType::DL_FILE_PATH));
}

Expand Down

0 comments on commit 3a4f93d

Please sign in to comment.