Skip to content

Commit

Permalink
added cache library
Browse files Browse the repository at this point in the history
implement caching into contest.rank api
  • Loading branch information
Belikhun committed Feb 28, 2020
1 parent ce058bc commit dfd3871
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data/logs.json
.cache

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions api/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function setting(String $key, &$target, $type) {
setting("ratelimit_maxRequest" , $rawConfig["ratelimit"]["maxRequest"] , $TYPE_NUMBER);
setting("ratelimit_time" , $rawConfig["ratelimit"]["time"] , $TYPE_NUMBER);
setting("ratelimit_banTime" , $rawConfig["ratelimit"]["banTime"] , $TYPE_NUMBER);
setting("cache_contestRank" , $rawConfig["cache"]["contestRank"] , $TYPE_NUMBER);

if ($rawConfig["publish"] !== true) {
$rawConfig["viewRank"] = false;
Expand Down
18 changes: 15 additions & 3 deletions api/contest/rank.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require_once $_SERVER["DOCUMENT_ROOT"] ."/lib/ratelimit.php";
require_once $_SERVER["DOCUMENT_ROOT"] ."/lib/belibrary.php";
require_once $_SERVER["DOCUMENT_ROOT"] ."/lib/cache.php";
require_once $_SERVER["DOCUMENT_ROOT"] ."/data/config.php";

if ($config["publish"] !== true && $_SESSION["id"] !== "admin")
Expand All @@ -31,6 +32,14 @@
"rank" => Array()
));

$cache = new cache("api.contest.rank");
$cache -> setAge($config["cache"]["contestRank"]);

if ($cache -> validate()) {
$returnData = $cache -> getData();
stop(0, "Thành công!", 200, $returnData, true);
}

require_once $_SERVER["DOCUMENT_ROOT"] ."/data/xmldb/account.php";
require_once $_SERVER["DOCUMENT_ROOT"] ."/lib/logParser.php";

Expand Down Expand Up @@ -76,9 +85,12 @@

return ($a > $b) ? -1 : 1;
});
stop(0, "Thành công!", 200, $returnData = Array (

$returnData = Array (
"list" => $list,
"nameList" => $nameList,
"rank" => $res
), true);
);

$cache -> save($returnData);
stop(0, "Thành công!", 200, $returnData, true);
7 changes: 7 additions & 0 deletions assets/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ var ratelimit = {
time: $("#ratelimit_time"),
banTime: $("#ratelimit_banTime")
}

var cache = {
contestRank: $("#cache_contestRank")
}

var setTimeToNow = $("#setTimeToNow");

function cvtime(h, m, s) {
Expand Down Expand Up @@ -135,6 +140,7 @@ function update() {
ratelimit.maxRequest.value = data.ratelimit.maxRequest;
ratelimit.time.value = data.ratelimit.time;
ratelimit.banTime.value = data.ratelimit.banTime;
cache.contestRank.value = data.cache.contestRank;

clientConfig.updateDelay.dispatchEvent(new Event("input"));
}, error => errorHandler(error));
Expand Down Expand Up @@ -318,6 +324,7 @@ $("#formContainer").addEventListener("submit", e => {
"ratelimit.maxRequest": parseInt(ratelimit.maxRequest.value),
"ratelimit.time": parseInt(ratelimit.time.value),
"ratelimit.banTime": parseInt(ratelimit.banTime.value),
"cache.contestRank": parseInt(cache.contestRank.value),
"token": API_TOKEN
}
}, () => {
Expand Down
18 changes: 18 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,24 @@

</div>

<div class="group file">
<t class="title">Cache Age</t>

<div class="item lr info sound" data-soundhoversoft>
<t class="left">
Increasing Cache Age will reduce backend calculation. But in return it will delay live data update.
<br><b>Only change these value if you know what you are doing!</b></t>
<div class="right"></div>
</div>

<div class="item sound" data-soundhoversoft>
<div class="formGroup sound" data-color="blue" data-soundselectsoft>
<input id="cache_contestRank" type="number" class="formField" autocomplete="off" placeholder="api.contest.rank" required>
<label for="cache_contestRank">api.contest.rank</label>
</div>
</div>
</div>

<div class="footer">
<button type="submit" class="sq-btn green sound" data-soundhover data-soundselect>Lưu thay đổi</button>
</div>
Expand Down
3 changes: 3 additions & 0 deletions data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"rankUpdate": true,
"logsUpdate": true,
"updateDelay": 2
},
"cache": {
"contestRank": 1
}
}
6 changes: 5 additions & 1 deletion data/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@

// Path
define("AVATAR_DIR", $_SERVER["DOCUMENT_ROOT"] ."/data/avatar");
define("PROBLEM_DIR", $_SERVER["DOCUMENT_ROOT"] ."/data/problems");
define("PROBLEM_DIR", $_SERVER["DOCUMENT_ROOT"] ."/data/problems");

// CACHE
define("CACHE_LOCATION", $_SERVER["DOCUMENT_ROOT"] ."/.cache");
define("CACHE_DEFAULT_AGE", 10);
71 changes: 71 additions & 0 deletions lib/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
//? |-----------------------------------------------------------------------------------------------|
//? | /lib/cache.php |
//? | |
//? | Copyright (c) 2018-2020 Belikhun. All right reserved |
//? | Licensed under the MIT License. See LICENSE in the project root for license information. |
//? |-----------------------------------------------------------------------------------------------|

require_once $_SERVER["DOCUMENT_ROOT"] ."/lib/belibrary.php";
require_once $_SERVER["DOCUMENT_ROOT"] ."/data/info.php";

if (!defined("CACHE_LOCATION"))
define("CACHE_LOCATION", $_SERVER["DOCUMENT_ROOT"] ."/.cache");

if (!defined("CACHE_DEFAULT_AGE"))
define("CACHE_DEFAULT_AGE", 10);

if (!file_exists(CACHE_LOCATION))
mkdir(CACHE_LOCATION);

class cache {
public $id;
public $data = Array();
private $cachefile;
private $cachepath;
private $stream;
private $template = Array(
"id" => "",
"age" => CACHE_DEFAULT_AGE,
"time" => 0,
"data" => null
);

public function __construct($cacheID, $defaultData = null) {
$this -> id = $cacheID;

$this -> cachefile = $this -> id .".cache";
$this -> cachepath = CACHE_LOCATION ."/". $this -> cachefile;
$this -> template["id"] = $this -> id;
$this -> template["data"] = $defaultData;

$this -> fetch();
}

private function fetch() {
$this -> stream = new fip($this -> cachepath, serialize($this -> template));
$data = $this -> stream -> read();

$this -> data = unserialize($data);
}

public function setAge($age) {
$this -> data["age"] = $age;
}

public function validate() {
return (time() - $this -> data["time"]) < $this -> data["age"];
}

public function getData() {
return $this -> data["data"];
}

public function save($data) {
if (isset($data))
$this -> data["data"] = $data;

$this -> data["time"] = time();
$this -> stream -> write(serialize($this -> data));
}
}

0 comments on commit dfd3871

Please sign in to comment.