Jump to content

Cache and to be updated always...


ukyo
 Share

Recommended Posts

Hi, 

I wrote a little function for get last modified date from pages. What is this function making ? You can get last modified page modified date from given id, parent or from all pages. What can you do with last modified date? You can use it for caching pages. Here is little function and some example usages.

/**
 * Get last modified page modified date from given $id, $parent_id, $templates_id or from all
 *
 * @param bool $id
 * @param bool $parent_id
 * @param bool $templates_id
 * @return mixed|string
 */
function getLastModified($id=false, $parent_id=false, $templates_id=false) {
    if(!is_null($id)) {
        $where = "";
        if(is_bool($id) != true) {
            $where = " WHERE";
            $where .= ($parent_id) ? " parent_id = {$id}" : " id={$id}";
            $where .= ($templates_id) ? " AND templates_id = {$templates_id}" : "";
        }
        $results = wire('db')->query("SELECT MAX(modified) as modified FROM pages{$where}");
        if($results->num_rows > 0) {
            $result = $results->fetch_assoc();
            $search = array(' ', '-', ':');
            $replace = array('', '', '');
            return str_replace($search, $replace, $result['modified']);
        }
    }
    return "";
}
Gist : https://gist.github.com/trk/a9d7e01ecfa6e40b65bc

Example Usages :

<?php
echo $cache->get("top-navigation" . getLastModified(true), function($pages) {
     echo renderYourNavigation();
});
?>
Here we are checking all pages and getting last modified date from database. With this way you don't need a cache time. If you update any page from your site, your cache file will be updated also.
<?php
echo $cache->get($page->name . getLastModified($page->id), function($page) {
    echo $page->title;
    // Do what you want...
});
?>
With this usage : you can use $page->name + last modified date as cache name and you page will be cached and to be updated always..
<?php
echo $cache->get('news-' . getLastModified(1234, true), function($pages) {
    $pages->get(1234);
    // Print out your news...
});
?>
If you set second parameter as "true" function will check pages if have parent_id = 1234.
  • Like 2
Link to comment
Share on other sites

Little Test Results :
 
Single page load times :

LOCAL TEST
- Before : 28rjpf7.jpg

- After : 9fz8fl.jpg

SERVER TEST

- Before : 2191m5c.jpg

- After: m8itzo.jpg
Homepage load times (Homepage getting slider and some of other data from different page->children and homepage have 2 repeater and website settings data etc..):

LOCAL TEST

- Before : 35dc1fk.jpg

- After : 2j0m9on.jpg

SERVER TEST (With custom .htaccess rules and google page speed module)

- Before : wa3klc.jpg 

- After : 2r4s7sh.jpg

If @ryan put a clear cache button on admin panel, it will be good than delete caches manually from database :), also i think website cache and module and other system cache need to be separated.

Also, Cache Class could have an option about use file cache or db cache. Current cache function here :

public function get($name, $expire = null, $func = null)

If we can send an bool value like : $fileCache=true Cache class could use file caching. If false or nothing Cache class could continue to use db cache.

public function get($name, $expire = null, $func = null, $fileCache=false)
Link to comment
Share on other sites

If @ryan put a clear cache button on admin panel, it will be good than delete caches manually from database  :), also i think website cache and module and other system cache need to be separated.

Actually, it's very easy to clear cache for a whole website. In cache settings, for a page, you can select to clear the whole site at once when you save that page. I will typically set that on the home page, because nothing ever gets changed on that page, it's just a template that will pull data from other templates. Whenever I need to force a cache reset, let's say after modifying some code that won't automatically be shown, I'll just save the front page to force a full website refresh.

Link to comment
Share on other sites

Its not good solution for me, because i have some slide items on homepage and some other stuff, when my customer add some data do this slider whole cache will be cleared also.

Homepage is not symbolic page for me, I am using homepage for website settings also other main things also.

Whenever I need to force a cache reset, let's say after modifying some code that won't automatically be shown, I'll just save the front page to force a full website refresh.

I have cache_expire field on homepage also, in dev mode making value "0" for production changing it.

Link to comment
Share on other sites

 Share

×
×
  • Create New...