สอบถามการทำ cache โดยใช้ class ของ joomla 3

ถามตอบ ปัญหาการพัฒนา Extension ไม่ว่าจะเป็น Module plugin Component หรือ แม้แต่ template การปรับแต่งโค้ด Joomla ต่างๆ ทุกเวอร์ชั่น 1.5 2.5 หรือ 3.x

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
Thanapoom1514
PHP VIP Members
PHP VIP Members
โพสต์: 4329
ลงทะเบียนเมื่อ: 04/07/2022 9:46 am

สอบถามการทำ cache โดยใช้ class ของ joomla 3

โพสต์ที่ยังไม่ได้อ่าน โดย Thanapoom1514 »

สอบถามการทำ cache เก็บขอมูลกระทู้โดยใช้ curl ในการดึงข้อมูลมาเก็บใน cache ซึ่งผมจะปรับมาใช้ class ของ joomla 3 ครับ ต้อง include ไฟล์ไหนบ้างครับของ joomla 3 ที่จำเป็นต้องใช้ กับใช้ class cache ใช้ฟังก์ชันยังไงบ้างครับ
โดยโค้ด php ที่จะมาปรับมาใช้ class ของตัว joomla 3

โค้ด: เลือกทั้งหมด

    $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 {
      
        if (file_exists($cache_file) && (time() - filemtime($cache_file)) >= 600) {
            $cache_files = glob($cache_file);
            foreach ($cache_files as $file) {
                if (is_file($file) && (time() - filemtime($file)) >= 600) {
                    unlink($file);
                };
            };
        };

        $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);
    }

:gfb:
ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 21991
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

Re: สอบถามการทำ cache โดยใช้ class ของ joomla 3

โพสต์ที่ยังไม่ได้อ่าน โดย tsukasaz »

ของ joomla ลองดูตามกระทู้นี้ครับ viewtopic.php?p=172235
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
ภาพประจำตัวสมาชิก
Thanapoom1514
PHP VIP Members
PHP VIP Members
โพสต์: 4329
ลงทะเบียนเมื่อ: 04/07/2022 9:46 am

Re: สอบถามการทำ cache โดยใช้ class ของ joomla 3

โพสต์ที่ยังไม่ได้อ่าน โดย Thanapoom1514 »

ผมปรับใช้ class ของง Joomla 3 ขึ้น error แบบนี้แทนครับ
Joomla Development-1.png
Joomla Development-1.png (17.16 KiB) Viewed 1120 times

โค้ด: เลือกทั้งหมด

Error: Failed to start application: Failed to start the session because headers have already been sent by 
"C:\xampp\htdocs\Joomla_3.10.10\tools\v2_header.php" at line 54.
ซึ่งบรรทัดที่แจ้งในไฟล์ v2_header.php คือ

โค้ด: เลือกทั้งหมด

 <a class="hidden-xs" href="<?php echo $word_recent['header'];?>"><img class="logo" src="#" alt="logo"></a>

โค้ดที่ปรับใช้ class cache

โค้ด: เลือกทั้งหมด

    define('_JEXEC', 1);
    define('JPATH_BASE', '../../');
    require_once JPATH_BASE . 'includes/defines.php';
    require_once JPATH_BASE . 'includes/framework.php';

    $application = JFactory::getApplication('site');

    $cache = JFactory::getCache('recent_topic');
    $cache->setCaching(true);
    $cache->setLifeTime(600); 

    $page_post_topic = 1;
    if (isset($_GET['p'])) {
        $page_post_topic = $_GET['p'];
    };

    $cache_file = md5($_SERVER['REQUEST_URI']);

    if ($cache->get($cache_file, false)) {
        $data_array = $cache->get($cache_file, false);
    } 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);
        
        $cache->store($data_array, $cache_file);
    }

    $cache->clean(false, 'recent_topic');
:gfb:
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: สอบถามการทำ cache โดยใช้ class ของ joomla 3

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ถ้าเอาไปใช้งานกับโปรแกรม อื่นแนะนำให้ include ไฟล์ class จะทำใช้เอาไปใช้
ถ้า มา

โค้ด: เลือกทั้งหมด

  $application = JFactory::getApplication('site');
หมายถึง จะเราทั้งระบบไปใช้ ทำจะให้โปรแกรมที่เขียนทำงานหนัก
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
Thanapoom1514
PHP VIP Members
PHP VIP Members
โพสต์: 4329
ลงทะเบียนเมื่อ: 04/07/2022 9:46 am

Re: สอบถามการทำ cache โดยใช้ class ของ joomla 3

โพสต์ที่ยังไม่ได้อ่าน โดย Thanapoom1514 »

ครับ ผมได้ลองปรับสามารถบันทึกข้อมูล cache ได้แล้วครับ

โค้ด: เลือกทั้งหมด


    define('_JEXEC', 1);
    define('JPATH_BASE', '../../');
    require_once JPATH_BASE . 'includes/defines.php';
    require_once JPATH_BASE . 'includes/framework.php';

    $cache = JFactory::getCache('recent_topic','');
    $cache->setCaching(true);
    $cache->setLifeTime(600); 

    $page_post_topic = 1;
    if (isset($_GET['p'])) {
        $page_post_topic = $_GET['p'];
    };

    $cache_file = md5($_SERVER['REQUEST_URI']);

    if ($cache->get($cache_file, false)) {
        $data_array = $cache->get($cache_file, false);
    } 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);
        
        $cache->store($data_array, $cache_file);
    }

    $current_time = time();
    if ($current_time % 600 == 0) {
        $cache->clean();
    }

Joomla Development-1.png
Joomla Development-1.png (91.88 KiB) Viewed 1072 times
:gfb:
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 99