Welcome Guest, Not a member yet? Register   Sign In
Page Caching
#1

[eluser]flash_back[/eluser]
I have develop small social network TuningTube about Tuning and Styling!

SQL server is slow and main problem is on forum index page, avenge time to load is above 6-7 seconds, sometimes over 10!

so caching will solve most problem, but there is a cache! take look a forum, when I am sing in and cache forum index page every visitor will se just my stuff, like avatar, PP inbox, most used room, even a hidden room for portal administracion!

so if anyone try to cache just some specific page elements, not entire page? I will take example for unread topic/room icon on forum! If user create let say new thread, can CI cache this file, and when somebody enter at forum index/room to open to just a thread from cache, not thread icon too?

is any way to to this?
#2

[eluser]flash_back[/eluser]
anybody???
#3

[eluser]cahva[/eluser]
Hi,

I used Phil Sturgeons Codeigniter-cache as I had the same problem(eCommerce site which had user specific parts). Check it out. Its easy to use and you can cache parts you want.
#4

[eluser]flash_back[/eluser]
tnx a lot man, this is helpful Smile ziveo godina 100 Big Grin be live for 100 years, that how we say Wink
#5

[eluser]flash_back[/eluser]
i have contact Phil to help me to run this from model directly and to make it more simple, to cache just simple element whit a value Smile

maybe I miss something in idea, and best way to "tune" this cache is to contact directly cache master Smile
#6

[eluser]flash_back[/eluser]
there is no answer from Phil so I am going to develop cache that will cache just some segments and keep them (that will be tricky) until the DB entry/data is changed..

I will see, maybe is better to generate cache from let say forum comment when comment is created then when first time there is a need for this kind of data.

I just release TT [url="http://translate.google.com/translate?hl=en&sl=sr&tl=en&u=http://www.tuningtube.rs/forum/pregled/Obavestenja/Beta-v1-0.html"]beta v1.0[/url] version, next step will be blogs and then cache..

so if anybody need this, contact me for about 15 - 30 days from now Smile

rd.flash.back[at]gmail.com // flash_back[at]tuningtube.rs // all best, regards
#7

[eluser]flash_back[/eluser]
here it is, is done for 25-30 minutes, I have not set trigger for change a cache file when let say a new comment on forum is add, but this is basic change Phil cache library is done and now you can cache specific element, not whole page Smile

Code:
public function funkcija($model, $funkcija, $ID, $vrednost, $expires = NULL)
    {
        return $this->_call($model, $funkcija, $ID, $vrednost, $expires);
    }
    
    private function _call($model, $funkcija, $ID, $vrednost = array(), $expires = NULL)
    {
        $this->ci->load->helper('security');

        if(!is_array($vrednost))
        {
            $vrednost = (array) $vrednost;
        }
        
        // Clean given arguments to a 0-index array
        $vrednost = array_values($vrednost);
        
        $cache_file = $model.DIRECTORY_SEPARATOR.dohash($funkcija.serialize($ID), 'sha1');
        
        // See if we have this cached
        $cached_response = $this->get($cache_file);

        // Not FALSE? Return it
        if($cached_response)
        {
            return $cached_response;
        }
        
        else
        {
            // Call the model or library with the method provided and the same arguments
            $new_response = $vrednost;
            $this->write($vrednost, $cache_file, $expires);
            
            return $new_response[0];
        }
    }
    
    function get($filename = NULL, $use_expires = true)
    {
        // Check if cache was requested with the function or uses this object
        if ($filename !== NULL)
        {
            $this->_reset();
            $this->filename = $filename;
        }
        
        // Check directory permissions
        if ( ! is_dir($this->path) OR ! is_really_writable($this->path))
        {
            return FALSE;
        }
        
        // Build the file path.
        $filepath = $this->path.$this->filename.'.cache';
        
        // Check if the cache exists, if not return FALSE
        if ( ! @file_exists($filepath))
        {
            return FALSE;
        }
        
        // Check if the cache can be opened, if not return FALSE
        if ( ! $fp = @fopen($filepath, FOPEN_READ))
        {
            return FALSE;
        }
        
        // Lock the cache
        flock($fp, LOCK_SH);
        
        // If the file contains data return it, otherwise return NULL
        if (filesize($filepath) > 0)
        {
            $this->contents = unserialize(fread($fp, filesize($filepath)));
        }
        else
        {
            $this->contents = NULL;
        }
        
        // Unlock the cache and close the file
        flock($fp, LOCK_UN);
        fclose($fp);
        
        // Check cache expiration, delete and return FALSE when expired
        if ($use_expires && ! empty($this->contents['__cache_expires']) && $this->contents['__cache_expires'] < time())
        {
            $this->delete($filename);
            return FALSE;
        }
        
        // Check Cache dependencies
        if(isset($this->contents['__cache_dependencies']))
        {
            foreach ($this->contents['__cache_dependencies'] as $dep)
            {
                $cache_created = filemtime($this->path.$this->filename.'.cache');
                
                // If dependency doesn't exist or is newer than this cache, delete and return FALSE
                if (! file_exists($this->path.$dep.'.cache') or filemtime($this->path.$dep.'.cache') > $cache_created)
                {
                    $this->delete($filename);
                    return FALSE;
                }
            }
        }
        
        // Instantiate the object variables
        $this->expires      = @$this->contents['__cache_expires'];
        $this->dependencies = @$this->contents['__cache_dependencies'];
        $this->created      = @$this->contents['__cache_created'];
        
        // Cleanup the meta variables from the contents
        $this->contents = @$this->contents['__cache_contents'];
        
        // Return the cache
        log_message('debug', "Cache retrieved: ".$filename);
        return $this->contents[0];
    }

$model // just used for folder where to set cache files
$funkcija // name of function that we are caching
$ID // ID of function or some value that is constant for caching value
$vrednost // value that we are caching

that is it, its just beta v0.1 version of cache which is done in 25-30 minutes but doing the job Wink
#8

[eluser]flash_back[/eluser]
now, this code caching single file from model, that was the point! now if you want to cache a array of files its easy, just add this changes to code Smile

I just add a $this->masivni_cache check to see is is massive cache, array of files! code now look like this Wink


Code:
public function funkcija($model, $funkcija, $ID, $vrednost, $expires = NULL)
    {        
        if(is_array($vrednost) AND count($vrednost) > 1):
            $this->masivni_cache = TRUE;
        endif;
        
        return $this->_call($model, $funkcija, $ID, $vrednost, $expires);
    }
    
    // Depreciated, use model() or library()
    private function _call($model, $funkcija, $ID, $vrednost = array(), $expires = NULL)
    {
        $this->ci->load->helper('security');
        
        if(!is_array($vrednost))
        {
            $vrednost = (array) $vrednost;
        }
        
        // Clean given arguments to a 0-index array
        $vrednost = array_values($vrednost);
        
        $cache_file = $model.DIRECTORY_SEPARATOR.dohash($funkcija.serialize($ID), 'sha1');
        
        // See if we have this cached
        $cached_response = $this->get($cache_file);

        // Not FALSE? Return it
        if($cached_response)
        {
            return $cached_response;
        }
        
        else
        {
            // Call the model or library with the method provided and the same arguments
            $new_response = $vrednost;
            $this->write($vrednost, $cache_file, $expires);
            // ako se ne vraca array vec pojedinacan element -> return $new_response[0];
            if (isset($this->masivni_cache) AND $this->masivni_cache == TRUE):
                $this->masivni_cache = FALSE;
                return $new_response;
            else:
                return $new_response[0];
            endif;
        }
    }
    
    function get($filename = NULL, $use_expires = true)
    {
        // Check if cache was requested with the function or uses this object
        if ($filename !== NULL)
        {
            $this->_reset();
            $this->filename = $filename;
        }
        
        // Check directory permissions
        if ( ! is_dir($this->path) OR ! is_really_writable($this->path))
        {
            return FALSE;
        }
        
        // Build the file path.
        $filepath = $this->path.$this->filename.'.cache';
        
        // Check if the cache exists, if not return FALSE
        if ( ! @file_exists($filepath))
        {
            return FALSE;
        }
        
        // Check if the cache can be opened, if not return FALSE
        if ( ! $fp = @fopen($filepath, FOPEN_READ))
        {
            return FALSE;
        }
        
        // Lock the cache
        flock($fp, LOCK_SH);
        
        // If the file contains data return it, otherwise return NULL
        if (filesize($filepath) > 0)
        {
            $this->contents = unserialize(fread($fp, filesize($filepath)));
        }
        else
        {
            $this->contents = NULL;
        }
        
        // Unlock the cache and close the file
        flock($fp, LOCK_UN);
        fclose($fp);
        
        // Check cache expiration, delete and return FALSE when expired
        if ($use_expires && ! empty($this->contents['__cache_expires']) && $this->contents['__cache_expires'] < time())
        {
            $this->delete($filename);
            return FALSE;
        }
        
        // Check Cache dependencies
        if(isset($this->contents['__cache_dependencies']))
        {
            foreach ($this->contents['__cache_dependencies'] as $dep)
            {
                $cache_created = filemtime($this->path.$this->filename.'.cache');
                
                // If dependency doesn't exist or is newer than this cache, delete and return FALSE
                if (! file_exists($this->path.$dep.'.cache') or filemtime($this->path.$dep.'.cache') > $cache_created)
                {
                    $this->delete($filename);
                    return FALSE;
                }
            }
        }
        
        // Instantiate the object variables
        $this->expires      = @$this->contents['__cache_expires'];
        $this->dependencies = @$this->contents['__cache_dependencies'];
        $this->created      = @$this->contents['__cache_created'];
        
        // Cleanup the meta variables from the contents
        $this->contents = @$this->contents['__cache_contents'];
        
        // Return the cache
        log_message('debug', "Cache retrieved: ".$filename);
        // ako se ne vraca array vec pojedinacan element -> return $this->contents[0];
        if (isset($this->masivni_cache) AND $this->masivni_cache == TRUE):
            $this->masivni_cache = FALSE;
            return $this->contents;
        else:
            return $this->contents[0];
        endif;
    }




Theme © iAndrew 2016 - Forum software by © MyBB