สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network
Moderator: mindphp, ผู้ดูแลกระดาน
-
Thanapoom1514
- PHP VIP Members
- โพสต์: 4329
- ลงทะเบียนเมื่อ: 04/07/2022 9:46 am
โพสต์ที่ยังไม่ได้อ่าน
โดย Thanapoom1514 »
สอบถามการทำระบบ cache เก็บข้อมูลทำยังไงบ้างครับ อยากให้ทุก 10 นาทีดึงข้อมูลด้วย curl ใหม่ครับ
โค้ด: เลือกทั้งหมด
$page = 1;
if (isset($_GET['p'])) {
$page = $_GET['p'];
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $curl_json.'/0/'.$page,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'accept: application/json'
)
)
);
$json_string = curl_exec($ch);
curl_close($ch);
$data_array = json_decode($json_string, true);
$data_post_topic =$data_array[0];
$row_recent = 10;
$PaginationPage = new PaginationPage($p, $data_array[1]['record']);
$PaginationPage->parse();
$start_recent = ($p - 1) * $row_recent;
-
Thanapoom1514
- PHP VIP Members
- โพสต์: 4329
- ลงทะเบียนเมื่อ: 04/07/2022 9:46 am
โพสต์ที่ยังไม่ได้อ่าน
โดย Thanapoom1514 »
ทำ cache เก็บข้อมูลรูปแบบนี้ถูกต้องไหมครับ
เงื่อนไขเช็คว่าไฟล์แคชมีอายุไม่เกิน 10 นาที ไม่เกินก็ให้ดึงข้อมูลใน cache ได้เลย
กับถ้าเกินก็ดึงข้อมูลด้วย curl ใหม่แล้วก็ไปสร้างไฟล์ให้เก็บในโฟลเดอร์ cache โดยใช้ฟังก์ชัน file_put_contents สร้างไฟล์
กับอีกเงื่อนไขอีกตัวให้เช็คถ้าเกิน 10 นาทีก็ให้ลบไฟล์ cache
โค้ด: เลือกทั้งหมด
$page_post_topic = 1;
if (isset($_GET['p'])) {
$page_post_topic = $_GET['p'];
};
$cache_file = '../cache/' . md5($_SERVER['REQUEST_URI']);
if (file_exists($cache_file) && (time() - filemtime($cache_file)) < 600) {
$data_array = json_decode(file_get_contents($cache_file), true);
} else {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $curl_json.'/0/'.$page_post_topic,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'accept: application/json'
)
));
$json_string = curl_exec($ch);
curl_close($ch);
$data_array = json_decode($json_string, true);
file_put_contents($cache_file, $json_string);
}
if (time() % 600 == 0) {
$cache_files = glob('../cache/*');
foreach ($cache_files as $file) {
if (is_file($file) && (time() - filemtime($file)) > 600) {
unlink($file);
}
}
}
-
-
- 0 ตอบกลับ
- 1706 แสดง
-
โพสต์ล่าสุด โดย offing
19/03/2015 10:16 am
-
-
- 0 ตอบกลับ
- 1629 แสดง
-
โพสต์ล่าสุด โดย mcmakdonal
22/06/2015 10:48 am
-
-
- 1 ตอบกลับ
- 8040 แสดง
-
โพสต์ล่าสุด โดย mindphp
03/08/2023 8:17 pm
-
-
- 0 ตอบกลับ
- 1949 แสดง
-
โพสต์ล่าสุด โดย sonram
13/07/2016 4:41 pm
-
-
- 0 ตอบกลับ
- 890 แสดง
-
โพสต์ล่าสุด โดย thatsawan
26/04/2017 10:56 am
สมาชิกกำลังดูบอร์ดนี้: Amazon [Bot], Google [Bot], Majestic-12 [Bot] และบุคลทั่วไป 103