Skip to content

Instantly share code, notes, and snippets.

@akira345
Last active February 8, 2016 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira345/760b53dbb37699b90f91 to your computer and use it in GitHub Desktop.
Save akira345/760b53dbb37699b90f91 to your computer and use it in GitHub Desktop.
PHPのPear_Cache_Liteのサンプルコードです。
<?php
include_once "Cache/Lite.php";
//キャッシュの生存時間は8時間
$option = array(
"cacheDir" => "./cache/",
"lifeTime" => "28800"
);
$cache = new Cache_Lite($option);
function hoge($in_year,$in_month,$cache) {
$cache_id = $in_year . $in_month; //キャッシュID。キャッシュする対象を識別するもの。
$group_name="hoge"; //キャッシュグループID。同一キャッシュIDかつ同一キャッシュグループIDで識別される。
if($cache_data = $cache->get($cache_id,$group_name)){
//キャッシュヒット
echo "HIT!!";
return $cache_data;
} else {
//キャッシュミス
echo "MISS!";
//データ作成
$tmp = "";
$tmp = "<SELECT name=nengetu> \n";
for($cnt_year = 2000;$cnt_year < 2021;++$cnt_year){
for($cnt_month = 1;$cnt_month < 13;++$cnt_month){
if(($cnt_year == $in_year) && ($cnt_month == $in_month)){
$tmp .= "<OPTION value=\"" . $cnt_year . "/" . $cnt_month . "\" selected>" . $cnt_year . "年" . $cnt_month . "月" . "</OPTION>\n";
} else {
$tmp .= "<OPTION value=\"" . $cnt_year . "/" . $cnt_month . "\">" . $cnt_year . "年" . $cnt_month . "月" . "</OPTION>\n";
}
}
}
$tmp .= "</SELECT>\n";
//キャッシュに保存
$cache->save($tmp);
return $tmp;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<title>
Cache Sample
</title>
<body>
<form>
<?php echo hoge($_GET["year"],$_GET["month"],$cache); ?>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment