ukyo

Module
AvbFastCache Module

8 posts in this topic

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 !

7 people like this

Share this post


Link to post
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 !

1 person likes this

Share this post


Link to post
Share on other sites

@ukyo, thanks for this! Looks very cool, and I'll be psyched to test this at some point soon...

Share this post


Link to post
Share on other sites

I updated Hook for ProcessPageSort::execute function and added some useful options. Now you have options for sorted pages.

post-2064-0-39739400-1436662959_thumb.pn

Share this post


Link to post
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

1 person likes this

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

1 person likes this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      HELLO!
      I've been working on a user messaging module which I am nearly ready to release as version 1.
      Currently I have the below functionality and I'm looking for feedback to what other things may be useful for people looking to add user to user messaging on their sites.
      compose message to one or more users known to PW js to enable tag based UI display of user names input on compose message form display all message threads with reply forms reply to message thread displaying each user name by each message in the thread display "unread" when a thread has new content that hasnt been seen by current user delete or unpublish message thread (configurable) display total message thread count display total unread threads (threads that have new replies that the current user has not seen) delete all message threads and associated data (not meant for the users to have access to) road map
      send email to user on new message to a thread they are included in
    • By louisstephens
      So I was working on a module and trying to include a library (so I could use it across the site) using $modules->get('myModule'); . However, When I go to include the library folder (modeled after the LibFlourish module created some time ago). I keep getting: 
      site/modules/MyModule/libraryName/ProcessWire\filetoinclude.php This comes with an error notifying that "failed to open stream: No such file or directory in ...".. Is ProcessWire supposed to be in the include url, or is there a way around this? Sorry, I am quite new to working with my own modules.
       
      I just noticed the sub forum regarding Development, if someone could move this to the correct area I would appreciate it.
    • By louisstephens
      I wasn't quite sure where to post this, as this is a question regarding the module (sorry if it is in the wrong place). I was wondering if anyone has used the Email to Page module and figured out a way to "move" incoming css to another field, or how to render the css so the message just renders utilizing the css. 
      Right now all of the message contents get dropped into a textarea field and display in a template as a jumbled mess. Perhaps I am missing a formatting option in the text area field to render the css and html.
    • By blynx
      Hej,
      just finished the first working version of my photoswipe bundle. https://github.com/blynx/MarkupProcesswirePhotoswipe
      Haven't published it to the module directory, yet. Wanted to wait for some feedback.
      You can add a photoswipe enabled thumbnail gallery / lightbox to your site like this. Just pass an image field to the renderGallery method:
      <?php $pwpswp = $modules->get('Pwpswp'); echo $pwpswp->renderGallery($page->nicePictures); Options are provided like so:
      <?php $galleryOptions = [ 'imageResizerOptions' => [ 'size' => '500x500' 'quality' => 70, 'upscaling' => false, 'cropping' => false ], 'loresResizerOptions' => [ 'size' => '500x500' 'quality' => 20, 'upscaling' => false, 'cropping' => false ], 'pswpOptions' => (object) [ 'shareEl' => false, 'indexIndicatorSep' => ' von ', 'closeOnScroll' => false ] ]; echo $pswp->renderGallery($page->images, $galleryOptions); More info about all that is in the readme: https://github.com/blynx/MarkupProcesswirePhotoswipe
      It is possible to customize pretty much anything by providing your own templates and scripts.
      What do you think? Any ideas, bugs, critique, requests?
      cheers
      Steffen
    • By kixe
      FieldtypeColor is on github

      Fieldtype stores a 32bit integer value reflecting a RGBA value.
      Input
      4 types of Inputfields provided
      Html5 Inputfield of type='color' (if supported by browser)    Inputfield type='text' expecting a 24bit hexcode string (RGB). Input format: '#4496dd'.
      The background color of the input field shows selected color Inputfield of type='text' expecting 32bit hexcode strings (RGB + alpha channel) Input format: '#fa4496dd' Inputfield with Spectrum Color Picker
      (Options modifiable) Inputfield type='text' with custom JavaScript and/or CSS (since version 1.0.3) Output
      Define output format under 'Details' tab in field settings. Select from the following 8 options
      string 6-digit hex color. Example: '#4496dd' string 8-digit hex color (limited browser support).  Example: '#fa4496dd' string CSS color value RGB. Example: 'rgb(68, 100, 221)' string CSS color value RGB. Example: 'rgba(68, 100, 221, 0.98)' string CSS color value RGB. Example: 'hsl(227, 69.2%, 56.7%)' string CSS color value RGB. Example: 'hsla(227, 69.2%, 56.7%, 0.98)' string 32bit raw hex value. Example: 'fa4496dd' int 32bit. Example: '4198799069' (unformatted storage value) The Fieldtype includes
      Spectrum Color Picker by Brian Grinstead

      SCREENSHOTS
      Input type=text with changing background and font color (for better contrast)

      Input type=color (in Firefox)


      Javascript based input (Spectrum Color Picker)


      Settings Output

       
      Settings Input