Jump to content

AvbFastCache Module


ukyo
 Share

Recommended Posts

AvbFastCache Module

Module AuthorBig Thanks to phpFastCache authorsUsage Almost Like original phpfastcache library :

I made some modification on original phpfastcache library for use it with ProcessWire. On my side i tested files and sqlite its look working well.

You can set default settings from module setting panel or you can use it like original library with custom settings for each call, from module setting panel you can set storage type, cache path, security key, fallback and also you can delete cached data from module settings panel.

Modified set function, working like core $cache->get function this function will check a cached data exist ? if not save cache data and return cached data back.

Here is some example usages :

// Load Module
$AvbFastCache = $modules->AvbFastCache;
// Set cache settings from module
$_c = phpFastCache($AvbFastCache->storage, $AvbFastCache->getConfig(), $AvbFastCache->expire);

$output = $_c->set("cacheKeyword", function($page)) {
    $output = '<h1>{$page->title}</h1>';
    $output .= "<div class='body'>{$page->body}</div>";
    
    return $output;
});

//=> OR

// Do MemCache
$_c2 = phpFastCache("memcached");

// Write to Cache Save API Calls and Return Cache Data
echo $_c2->set("identity_keyword", function()) {
    $results = cURL->get("http://www.youtube.com/api/json/url/keyword/page");

    $output = "";
    foreach($results as $video) {
        $output .= $vieo->title; // Output Your Contents HERE
    }

    return $output;
}, 3600*24);

// This will check id=1 or parent_id=1 and will return last modified page UNIX_TIMESTAMP as result
echo $AvbFastCache->getLastModified(1);

// This will check id=1 or parent_id=1 and template=basic-page and will return last modified page UNIX_TIMESTAMP as result
echo $AvbFastCache->getLastModified(1, 'basic-page');

// What can you do with last modified dates ? Let me show you an example

// Think you are in news page, $page->id is news page id we are using $user->language->id because if we have multi language website
// Here getLastModified() function will get last modified date for us, if news page or children pages have any update new cache data will be created automatically
// Like this you can set expire time 0 unlimited from module panel !
// Think we are in "new-list" template and listing children pages
$keyword = "newsPage" . $page->id . $user->language->id . $AvbFastCache->getLastModified($page->id, 'news-single');

// Load library with your settings
$_c3 = phpFastCache($AvbFastCache->storage, $AvbFastCache->getConfig(), $AvbFastCache->expire);

// Write to Cache and Display Result
echo $_c3->set($keyword, function($page)) {

    $output = "";

    foreach($page->children as $p) $output .= "<h2>{$p->title}</h2>";

    return $output;
});

You can check phpfastcache usage from phpfastcache wiki or phpfastcache offical website

Note : I didn't tested this module with older ProcessWire versions, tested with 2.6.1 or newer versions. Module not have core dependency, it could work also older versions but need to check for be sure !

  • Like 7
Link to comment
Share on other sites

With ProcessWire 2.6.7 or newer version, You can do something like :
 
ready.php, simple example, you can extent this may i missed something and you can have better idea for best caching methods..

// You can set your site templates for cache or ignore cache
$allowedCacheTemplate = array(
        'home',
        'basic-page',
        'contact' => array(
            'ignore' => true
        ),
        'reference',
        'reference-list' => array(
            'list' => true,
            'childTemplate' => 'reference',
            'pagination' => true
        ),
        'service-list' => array(
            'pagination' => true
        )
    );

    wire()->addHook("Page::render", function(HookEvent $event) use($page, $user, $input, $allowedCacheTemplate) {
        $template = $event->template;

        // Check allowed cache templates, if template has ignore or don't have template return $event->return;
        if(isset($allowedCacheTemplate[$template]['ignore']) || !isset($allowedCacheTemplate[$template])) return $event->return;

        // Start Caching
        $avbfastcache = wire('modules')->get('AvbFastCache');
        $_c = phpFastCache($avbfastcache->storage, $avbfastcache->getConfig(), $avbfastcache->expire);
        // Set default keyword
        $keyword = $template . $page->id . $page->modified . $user->language->id;
        // For list view, get children last modified unix time and add it to $keyword
        if(isset($allowedCacheTemplate[$template]['list'])) {
            $filterTemplte = (isset($allowedCacheTemplate[$template]['childTemplate'])) ? $allowedCacheTemplate[$template]['childTemplate'] : NULL;
            $getLastModified = $avbfastcache->getLastModified($page->id, $filterTemplte);
            $keyword .= $getLastModified;
        }
        // If there is a pagination on this template also add $input->pageNum to template
        if(isset($allowedCacheTemplate[$template]['pagination'])) {
            $keyword .= $input->pageNum;
        }
        // Out keyword is ready to use, check cache exist?
        $html = $_c->get($keyword);
        // If cache not exist create cache
        if($html == null) {
            $html = $event->return;;
            $_c->set($keyword, $html);
        }
        // return the result
        return $html;
    });

With this method you can cache template before render !

  • Like 1
Link to comment
Share on other sites

what is the advantage over using the built-in cache or procache?

ProCache is a paid version cache module ! Do not compare this free module with paid modules but if you know how to extend module you can do it like ProCache or better than it !

Build-in cache module is only support database cache, on the templates side its support files cache but you can't use it for custom uses. I used both of them there is no place for expire time set option, cached data info or clear cache section etc..

If you check  phpfastcache offical website you will see advantages of using this module. For example you can use multiple cache drivers in same time with different options or you can set a default options for use any where. You can set expire date, you can clear cached data (for files, sqllite drivers), and you can set the what need to do after a page modified etc.....

Here is my website : http://altivebir.com/ using this cache module.

post-2064-0-66734800-1437383025_thumb.pn

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi @ukyo

I want to use your AVBfastcache module in my new project but can't find any information about its capability with PW 3.

Also, there is no activity in repo and forum more than a year, so I'm curious about your plans to maintain this module.

Link to comment
Share on other sites

19 hours ago, Zeka said:

Hi @ukyo

I want to use your AVBfastcache module in my new project but can't find any information about its capability with PW 3.

Also, there is no activity in repo and forum more than a year, so I'm curious about your plans to maintain this module.

I will update module with latest phpFastCache (v6). I think this update will come next month.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...