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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การนำ cache ไปใช้กับ module บน Joomla

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

โดย mindphp » 04/07/2014 4:43 pm

ก่อน query เช็คว่า ใน cache มีอยู่หรือไม่
ถ้ามีก็เอาจาก cache แทน ไม่ต้อง query ให้เสียเวลาฐานข้อมูล

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

โดย jay_limm » 04/07/2014 4:37 pm

การนำ cache ไปใช้ จะต้องเช็คว่ามีการเก็บ cache ไว้หรือไม่ ตอนที่ ใช้คำสั่ง query หรือว่า ใส่ตอน fetch ข้อมูลออกมาแสดงค่ะ
กรณีที่ทำอยู่ query กับ fetch อยู่คนละไฟล์กันค่ะ

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

โดย mindphp » 03/07/2014 2:37 pm

เริ่มสร้าง object
$cache = JFactory::getCache('ชื่อcacheเดียวกับตอนเซตcache');

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

โดย jay_limm » 03/07/2014 2:32 pm

การเซต cache

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

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

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

$cache = JFactory::getCache('ชื่อcacheเดียวกับตอนเซตcache');
ไม่ทราบว่าตามโค้ดเข้าใจถูกหรือเปล่า

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

โดย mindphp » 03/07/2014 2:22 pm

ตัวอย่างการทำ 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 />');
    }
}

 

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

โดย jay_limm » 03/07/2014 2:21 pm

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

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

$cache = JFactory::getCache('ชื่อแคชที่ต้องการ'); 
แล้วจะรู้ได้อย่างไรว่าเราจะใช้ cache ที่มีชื่อว่าอะไร

ข้างบน