需要重新编码速度算法(Flickr API)

时间:2011-03-08 02:22:54

标签: php api flickr

我设计了一个漂亮的小网站,它使用phpflickr(PHP Flickr API)从flickr组中提取图片,然后根据该信息显示信息提取它的标签。

我的问题是我认为我有太多的循环,因此通过API对Flickr的查询太多,这显着减慢了加载过程。这是代码:

<?php

require_once("phpFlickr/phpFlickr.php");

$f = new phpFlickr("apicode","secretcode");
$f->auth("write");
$token = $f->auth_checkToken();
$photos = $f->groups_pools_getPhotos("groupcode", null, null, "description,tags");
$nsid = $token['user']['nsid'];
$voted = $_POST['voted'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="Keywords" content="Flickr, App" />
  <meta name="Description" content="Flickr group rater" />
  <title>My App</title>
  <!--<link rel="shortcut icon" href="/media/images/favicon.ico" type="image/x-icon"/>-->
</head>
<body>

<?


if ($voted == null){

    foreach ($photos['photo'] as $photo) {

        $alreadyvoted = "false";
        $tags = $f->tags_getListPhoto($photo['id']);

        foreach($tags as $tagdata) {
            if(is_array($tagdata)) {
                foreach($tagdata as $author) {
                    if($author == $nsid) $alreadyvoted = "true";
                }
            }
        }


        if ($alreadyvoted == "false") {
            echo "<div class=\"container\" style=\"border-width: 1px; border-style: solid; padding: 20px; margin-top: 20px; width: 1100px; height: 600px; margin-left: auto; margin-right: auto;\">";
            echo "<div class=\"photo\" style=\"float: left;\">";
            echo "<a href=\"http://www.flickr.com/photos/$photo[owner]/$photo[id]/in/pool-hbu\">";
            echo "<img border='0' alt='$photo[title]' ".
                "src=\"" . $f->buildPhotoURL($photo, "Medium 640") . "\" />";
            echo "</a><div style=\"width: 500px;\"><p>$photo[description]</p></div></div>";
            echo "<div style=\"width: 400px; float:right;\"><h2>$photo[title]</h2></div>";
            echo "<div style=\"float: right; margin-left: 40px; margin-right: 40px; border: 1px solid; padding: 10px;\">";
            echo "<form action=\"index.php\" method=\"post\">
                    <input type=\"hidden\" name=\"photoid\" value=\"$photo[id]\" />
                    <input type=\"hidden\" name=\"voted\" value=\"voted$photo[id]\" />
                    Tag:<br />
                    <input type=\"radio\" name=\"tag\" value=\"delete\" />Delete<br />
                    <input type=\"radio\" name=\"tag\" value=\"save\" />Save<br />
                    <br /><a href=\"http://www.flickr.com/photos/$photo[owner]/$photo[id]/lightbox/\">View Large</a><br />
                    <br />Comment: <br /><textarea cols=\"40\" rows=\"10\" name=\"comment\"></textarea><br />
                    <br /><input type=\"submit\" value=\"Submit\" /></form>";
            echo "</div>";
            echo "</div>";
            echo "<br /><br />\n";
        }
    }


}

else if ($voted != null){

    $photoid = $_POST['photoid'];
    $totag = $_POST['tag'];
    $tocomment = $_POST['comment'];
    $tags = $f->tags_getListPhoto($photoid);

    if (($totag == null) || ($tocomment == null)) die("Make sure you have both a tag and a comment selected");
    else {
        if($totag == "delete"){
            $finaltag = "delete";
            foreach ($tags as $row){
                if (($row["raw"] == "delete") || ($row["raw"] == "delete1") || ($row["raw"] == "delete 1")) $finaltag = "delete2";
                else if (($row["raw"] == "delete2") || ($row["raw"] == "Delete 2")) $finaltag = "delete3";
                else if (($row["raw"] == "delete3") || ($row["raw"] == "Delete 3")) $finaltag = "delete4";
                else if (($row["raw"] == "delete4") || ($row["raw"] == "Delete 4")) $finaltag = "delete5";
                else if (($row["raw"] == "delete5") || ($row["raw"] == "Delete 5")) $finaltag = "delete6";
                else if (($row["raw"] == "delete6") || ($row["raw"] == "Delete 6")) $finaltag = "delete7";
                else if (($row["raw"] == "delete7") || ($row["raw"] == "Delete 7")) $finaltag = "delete8";
                else if (($row["raw"] == "delete8") || ($row["raw"] == "Delete 8")) $finaltag = "delete9";
                else if (($row["raw"] == "delete9") || ($row["raw"] == "Delete 9")) $finaltag = "delete10";
                else if (($row["raw"] == "delete10") || ($row["raw"] == "Delete 10")) $finaltag = "delete11";
                else if (($row["raw"] == "delete11") || ($row["raw"] == "Delete 11")) $finaltag = "delete12";
                else if (($row["raw"] == "delete12") || ($row["raw"] == "Delete 12")) $finaltag = "delete13";
                else if (($row["raw"] == "delete13") || ($row["raw"] == "Delete 13")) $finaltag = "delete14";

            }
        }

        else if($totag == "save"){
            $finaltag = "save";
            foreach ($tags as $row){
                if (($row["raw"] == "save") || ($row["raw"] == "save1") || ($row["raw"] == "save 1")) $finaltag = "save2";
                else if (($row["raw"] == "save2") || ($row["raw"] == "Save 2")) $finaltag = "save3";
                else if (($row["raw"] == "save3") || ($row["raw"] == "Save 3")) $finaltag = "save4";
                else if (($row["raw"] == "save4") || ($row["raw"] == "Save 4")) $finaltag = "save5";
                else if (($row["raw"] == "save5") || ($row["raw"] == "Save 5")) $finaltag = "save6";
                else if (($row["raw"] == "save6") || ($row["raw"] == "Save 6")) $finaltag = "save7";
                else if (($row["raw"] == "save7") || ($row["raw"] == "Save 7")) $finaltag = "save8";
                else if (($row["raw"] == "save8") || ($row["raw"] == "Save 8")) $finaltag = "save9";
                else if (($row["raw"] == "save9") || ($row["raw"] == "Save 9")) $finaltag = "save10";
                else if (($row["raw"] == "save10") || ($row["raw"] == "Save 10")) $finaltag = "save11";
                else if (($row["raw"] == "save11") || ($row["raw"] == "Save 11")) $finaltag = "save12";
                else if (($row["raw"] == "save12") || ($row["raw"] == "Save 12")) $finaltag = "save13";
                else if (($row["raw"] == "save13") || ($row["raw"] == "Save 13")) $finaltag = "save14";
            }
        }

        else die("Something went wrong there, try again.");

        $finalcomment = $tocomment."\n\n -Voted \"".$finaltag."\" (by the flickr rater app</a>)";

        $f->photos_comments_addComment($photoid, $finalcomment);
        $f->photos_addTags($photoid, $finaltag);

        header("Refresh: 0;");
    }

}


?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

高速缓存。

例如,将该组中的照片列表缓存5分钟,然后将给定照片的标签缓存60分钟。 (标签经常变化吗?)

通过序列化数据和写入磁盘进行缓存的示例:

$cacheTTL = 300;
$cacheFile = 'photos.dat';
if (file_exists($cacheFile) && filemtime($cacheFile) > (time() - $cacheTTL)){
    $photos = unserialize(file_get_contents($cacheFile));
}
else {
    $photos = $f->groups_pools_getPhotos("groupcode", null, null, "description,tags");
    file_put_contents($cacheFile,serialize($photos));
}
相关问题