การนำ cache ไปใช้กับ module บน Joomla

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

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

jay_limm
PHP VIP Members
PHP VIP Members
โพสต์: 4933
ลงทะเบียนเมื่อ: 02/06/2014 9:58 am

การนำ cache ไปใช้กับ module บน Joomla

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

การใช้งาน cache กับ module ของ joomla
เรียกดู cache โดย

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

$cache = JFactory::getCache('ชื่อแคชที่ต้องการ'); 
แล้วจะรู้ได้อย่างไรว่าเราจะใช้ cache ที่มีชื่อว่าอะไร
บางที Bug ก็ไม่มีเหตุผล และไม่ต้องการคำจำกัดความ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41368
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: การนำ cache ไปใช้กับ module บน Joomla

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

ตัวอย่างการทำ cache ใน plugin
แต่ทำใน Module ก็ใช้หลักการเดียวกัน

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

<?php
/**
{{LICENSE}} 
*/

defined('_JEXEC') or die;

class PlgContentMlinkarticle extends JPlugin
{
    
    public function onContentPrepare($context, &$row, &$params, $page = 0)
    {
                JHTML::_('behavior.tooltip');
                //$profiler = new JProfiler();
                //echo $profiler->mark('Start <br />');
                
                $cache = JFactory::getCache('mlinkarticle');
               //  $cache->setCaching(true);
                $store = unserialize($cache->_getStorage()->get($row->id, 'mlinkarticle'));
                
                if(!empty($store)) {
                    $row->text = $store;
                } else {
                    $regex = '/href\=\"index\.php\?option\=com\_content&view\=article&id=([0-9]*)/i';
                    $matches = array();
                    preg_match_all($regex, $row->text, $matches);

                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);

                    $query->select('id, title, introtext')->from('#__content');
                    $db->setQuery($query);
                    $db->query();

                    $content = $db->loadObjectList('id');

                    $loop = count($matches[0]);
                    if($loop > 0) {
                        for($i = 0; $i < $loop; $i++) {
                            $id = $matches[1][$i];
                            if(isset($content[$id])) {
                                $tip = $content[$id]->title.'::'.substr(htmlentities($content[$id]->introtext), 0, 25).'...';
                                $row->text = str_replace($matches[0][$i], 'class="hasTip" title="'.$tip.'" '.$matches[0][$i], $row->text);
                                $row->text = str_replace('{--mlinkarticle--}', '', $row->text);
                                $row->text = str_replace('{--mlinkarticle='.$id.'--}', '', $row->text);
                            }
                        }
                    }
                    $cache->store($row->text, $row->id);
                }
                //echo $profiler->mark('Finish <br />');
    }
}

 
ติดตาม 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
jay_limm
PHP VIP Members
PHP VIP Members
โพสต์: 4933
ลงทะเบียนเมื่อ: 02/06/2014 9:58 am

Re: การนำ cache ไปใช้กับ module บน Joomla

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

การเซต cache

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

$cache->setCaching( 'ชื่อcacheที่เซตโดยที่เราตั้งเอง' );
แล้วก็ทำการ get cache

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

$cache = JFactory::getCache('ชื่อcacheเดียวกับตอนเซตcache');
ไม่ทราบว่าตามโค้ดเข้าใจถูกหรือเปล่า
บางที Bug ก็ไม่มีเหตุผล และไม่ต้องการคำจำกัดความ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41368
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: การนำ cache ไปใช้กับ module บน Joomla

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

เริ่มสร้าง object
$cache = JFactory::getCache('ชื่อcacheเดียวกับตอนเซตcache');
ติดตาม 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
jay_limm
PHP VIP Members
PHP VIP Members
โพสต์: 4933
ลงทะเบียนเมื่อ: 02/06/2014 9:58 am

Re: การนำ cache ไปใช้กับ module บน Joomla

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

การนำ cache ไปใช้ จะต้องเช็คว่ามีการเก็บ cache ไว้หรือไม่ ตอนที่ ใช้คำสั่ง query หรือว่า ใส่ตอน fetch ข้อมูลออกมาแสดงค่ะ
กรณีที่ทำอยู่ query กับ fetch อยู่คนละไฟล์กันค่ะ
แก้ไขล่าสุดโดย jay_limm เมื่อ 04/07/2014 5:16 pm, แก้ไขไปแล้ว 1 ครั้ง.
บางที Bug ก็ไม่มีเหตุผล และไม่ต้องการคำจำกัดความ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41368
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: การนำ cache ไปใช้กับ module บน Joomla

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

ก่อน query เช็คว่า ใน cache มีอยู่หรือไม่
ถ้ามีก็เอาจาก cache แทน ไม่ต้อง query ให้เสียเวลาฐานข้อมูล
ติดตาม 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
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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