Fix cache image
This commit is contained in:
@@ -6,22 +6,58 @@
|
||||
*/
|
||||
|
||||
class ImageCacheManager {
|
||||
private static $memcached = null;
|
||||
|
||||
const EXPIRATION_SECONDS = 2592000; // 30 * 24 * 60 * 60 -> 30 gg
|
||||
|
||||
private static function getMemcached() {
|
||||
if (is_null(self::$memcached)) {
|
||||
self::$memcached = new Memcached();
|
||||
self::$memcached->addServer('memcached', 11211);
|
||||
}
|
||||
return self::$memcached;
|
||||
}
|
||||
|
||||
private static function getImageKey(MediaModel $media, $size) {
|
||||
return self::getImageKeyById($media->id, $size);
|
||||
}
|
||||
private static function getImageKeyById(string $id, $size) {
|
||||
return $id."-".$size."-image";
|
||||
}
|
||||
|
||||
private static function getMimeKey(MediaModel $media, $size) {
|
||||
return self::getMimeKeyById($media->id, $size);
|
||||
}
|
||||
private static function getMimeKeyById(string $id, $size) {
|
||||
return $id."-".$size."-mime";
|
||||
}
|
||||
|
||||
public static function cacheImage(MediaModel $media, $size){
|
||||
self::checkValidModel($media);
|
||||
$cachedPath = self::getImageCachePath($media->id, $size);
|
||||
$mimePath = self::getImageMimePath($media->id, $size);
|
||||
// $cachedPath = self::getImageCachePath($media->id, $size);
|
||||
// $mimePath = self::getImageMimePath($media->id, $size);
|
||||
$originalFilePath = self::getOriginalPath($media);
|
||||
|
||||
if ( SFSManager::fileExists($cachedPath) ){
|
||||
SFSManager::deleteFile($cachedPath);
|
||||
SFSManager::deleteFile($mimePath);
|
||||
}
|
||||
if (!is_dir(pathinfo($cachedPath,PATHINFO_DIRNAME))){
|
||||
SFSManager::createDirectory(pathinfo($cachedPath,PATHINFO_DIRNAME));
|
||||
}
|
||||
SFSManager::writeFile(mime_content_type($originalFilePath), $mimePath);
|
||||
SFSManager::writeFile(self::getImageContent($media, $size), $cachedPath);
|
||||
// if ( SFSManager::fileExists($cachedPath) ){
|
||||
// SFSManager::deleteFile($cachedPath);
|
||||
// SFSManager::deleteFile($mimePath);
|
||||
// }
|
||||
// if (!is_dir(pathinfo($cachedPath,PATHINFO_DIRNAME))){
|
||||
// SFSManager::createDirectory(pathinfo($cachedPath,PATHINFO_DIRNAME));
|
||||
// }
|
||||
// SFSManager::writeFile(mime_content_type($originalFilePath), $mimePath);
|
||||
// SFSManager::writeFile(self::getImageContent($media, $size), $cachedPath);
|
||||
self::getMemcached()->set(
|
||||
self::getMimeKey($media, $size),
|
||||
mime_content_type($originalFilePath),
|
||||
self::EXPIRATION_SECONDS
|
||||
);
|
||||
self::getMemcached()->set(
|
||||
self::getImageKey($media, $size),
|
||||
self::getImageContent($media, $size),
|
||||
self::EXPIRATION_SECONDS
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,8 +66,9 @@ class ImageCacheManager {
|
||||
* @param int $size
|
||||
*/
|
||||
public static function isCached($id, $size){
|
||||
$cachedPath = self::getImageCachePath($id, $size);
|
||||
return SFSManager::fileExists($cachedPath);
|
||||
return self::getMemcached()->get(self::getImageKeyById($id, $size)) ? true : false;
|
||||
// $cachedPath = self::getImageCachePath($id, $size);
|
||||
// return SFSManager::fileExists($cachedPath);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,8 +80,9 @@ class ImageCacheManager {
|
||||
* @return string
|
||||
*/
|
||||
public static function getMimeType($id, $size){
|
||||
$mimePath = self::getImageMimePath($id, $size);
|
||||
return file_get_contents($mimePath);
|
||||
return self::getMemcached()->get(self::getMimeKeyById($id, $size));
|
||||
// $mimePath = self::getImageMimePath($id, $size);
|
||||
// return file_get_contents($mimePath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,14 +92,14 @@ class ImageCacheManager {
|
||||
* @return string
|
||||
*/
|
||||
public static function getContent($id, $size){
|
||||
$cachedPath = self::getImageCachePath($id, $size);
|
||||
return file_get_contents($cachedPath);
|
||||
return self::getMemcached()->get(self::getImageKeyById($id, $size));
|
||||
// $cachedPath = self::getImageCachePath($id, $size);
|
||||
// return file_get_contents($cachedPath);
|
||||
}
|
||||
|
||||
public static function printCachedMedia($id, $size){
|
||||
$cachedPath = self::getImageCachePath($id, $size);
|
||||
public static function printCachedMedia($id, $size) {
|
||||
header('Content-type: '.self::getMimeType($id, $size));
|
||||
return readfile($cachedPath);
|
||||
echo self::getContent($id, $size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ if (array_key_exists("id",$_GET)) {
|
||||
$size=intval($_GET['size']);
|
||||
}
|
||||
|
||||
if ($size == 3){
|
||||
if ($size !== MediaImage::SIZE_ORIGINAL){
|
||||
if (!ImageCacheManager::isCached($_GET["id"], $size)){
|
||||
$img = GlobalVariables::get("dao")->getFirst("MediaModel",array("id"=>$_GET['id']));
|
||||
if (!is_null($img)){
|
||||
|
||||
+12
-12
@@ -209,7 +209,7 @@ catch (Exception $ex){
|
||||
if(!is_null($media)){
|
||||
?>
|
||||
<div class="ansaImage mediaContent">
|
||||
<?php $media->renderMedia();?>
|
||||
<?php $media->renderMedia(['size' => MediaImage::SIZE_BIG]);?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
@@ -246,7 +246,7 @@ catch (Exception $ex){
|
||||
<div id="politica_<?php echo $i;?>" class="newsSummary"><?php
|
||||
$media = DaoMediaHandler::getMainMediaFromModel($politica);
|
||||
if(!is_null($media)){?>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_BIG]);?></div>
|
||||
<?php }?>
|
||||
<div class="newsTitle"><?php echo $stringTitolo;?></div>
|
||||
<div class="newsSubTitle"><?php echo $string;?></div>
|
||||
@@ -276,7 +276,7 @@ catch (Exception $ex){
|
||||
<div id="cronaca_<?php echo $i;?>" class="newsSummary"><?php
|
||||
$media = DaoMediaHandler::getMainMediaFromModel($cronaca);
|
||||
if(!is_null($media)){?>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_BIG]);?></div>
|
||||
<?php }?>
|
||||
<div class="newsTitle"><?php echo $stringTitolo;?></div>
|
||||
<div class="newsSubTitle"><?php echo $string;?></div>
|
||||
@@ -305,7 +305,7 @@ catch (Exception $ex){
|
||||
<div id="sport_<?php echo $i;?>" class="sportSummary"><?php
|
||||
$media = DaoMediaHandler::getMainMediaFromModel($sport);
|
||||
if(!is_null($media)){?>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="newsMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_BIG]);?></div>
|
||||
<?php }?>
|
||||
<div class="newsTitle"><?php echo $stringTitolo;?></div>
|
||||
<div class="newsSubTitle"><?php echo $string;?></div>
|
||||
@@ -334,7 +334,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $cultura->getAbsoluteUrl();?>" id="cultura_<?php echo $i;?>" class="culturaNews"><?php
|
||||
$media = DaoMediaHandler::getMainMediaFromModel($cultura);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="culturaTitle"><?php echo $string;?></div>
|
||||
</a>
|
||||
@@ -358,7 +358,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $ristoranti->getAbsoluteUrl();?>" id="ristoranti_<?php echo $i;?>" class="ristorantiBlock"><?php
|
||||
$media = DaoMediaHandler::getMainMediaFromModel($ristoranti);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="ristorantiTitle"><?php echo $string;?></div>
|
||||
</a>
|
||||
@@ -381,7 +381,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $salute->getAbsoluteUrl();?>" id="salute_<?php echo $i;?>" class="saluteContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($salute);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="saluteTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
@@ -430,7 +430,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $gossip->getAbsoluteUrl();?>" id="gossip_<?php echo $i;?>"class="gossipContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($gossip);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="gossipTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
@@ -455,7 +455,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $professioni->getAbsoluteUrl();?>" id="gossip_<?php echo $i;?>"class="gossipContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($professioni);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="gossipTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
@@ -479,7 +479,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $elezioni->getAbsoluteUrl();?>" class="animaliContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($elezioni);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="viaggiTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
@@ -503,7 +503,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $viaggi->getAbsoluteUrl();?>" id="viaggi_<?php echo $i;?>"class="animaliContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($viaggi);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="viaggiTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
@@ -525,7 +525,7 @@ catch (Exception $ex){
|
||||
<a href="<?php echo $animalNews->getAbsoluteUrl();?>" class="animaliContainer">
|
||||
<?php $media = DaoMediaHandler::getMainMediaFromModel($animalNews);
|
||||
if(!is_null($media)){?>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia();?></div>
|
||||
<div class="culturaMainMedia mediaContent"><?php $media->renderMedia(['size' => MediaImage::SIZE_MEDIUM]);?></div>
|
||||
<?php }?>
|
||||
<div class="animaliTitle"><?php echo $stringTitle;?></div>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user