free hit counter

Data Cache In Cakephp

Data Cache In Cakephp

Data Cache In Cakephp

Cache

The Cache helper assists in caching entire layouts and views, saving time repetitively retrieving data. View Caching in Cake temporarily stores parsed layouts and views with the storage engine of choice. It should be noted that the Cache helper works quite differently than other helpers. It does not have methods that are directly called. Instead a view is marked with cache tags indicating which blocks of content should not be cached.

When a URL is requested, Cake checks to see if that request string has already been cached. If it has, the rest of the url dispatching process is skipped. Any nocache blocks are processed normally and the view is served. This creates a big savings in processing time for each request to a cached URL as minimal code is executed. If Cake doesn’t find a cached view, or the cache has expired for the requested URL it continues to process the request normally.

General Caching

Caching is intended to be a means of temporary storage to help reduce load on the server. For example you could store the results of a time-expensive database query so that it is not required to run on every page load.

With this in mind caching is not permanent storage and should never be used to permanently store anything. And only cache things that can be regenerated when needed.

Cache Engines in Cake

New since 1.2 are several cache engines or cache backends. These interface transparently with the cache helper, allowing you to store view caches in a multitude of media without worrying about the specifics of that media. The choice of cache engine is controlled through the app/config/core.php config file. Most options for each caching engine are listed in the core.php config file and more detailed information on each caching engine can be found in the Caching Section.

File

The File Engine is the default caching engine used by cake. It writes flat files to the filesystem and it has several optional parameters but works well with the defaults.

APC

The APC engine implements the Alternative PHP Cache opcode Cacher. Like XCache, this engine caches the compiled PHP opcode.

XCache

The XCache caching engine is functionally similar to APC other than it implements the XCache opcode caching engine. It requires the entry of a user and password to work properly.

Memcache

The Memcache engine works with a memcaching server allowing you to create a cache object in system memory. More information on memcaching can be found on php.net and memcached

Source : http://book.cakephp.org/1.3/en/The-Manual/Core-Helpers/Cache.html

 

Data Cache : Start now

1) /app/config/core.php, and add in it the following

 

  1. Cache::config(‘sql_cache’array(  
  2.     ‘engine’        => ‘File’,  
  3.     ‘path’      => CACHE .‘sql’. DS,  
  4.     ‘serialize’ => true,  
  5. ));  

2) /app/tmp/cache/ , create new folder ( name with sql  )

 

3 ) Read DATA

 

  1. function cachecategory($limit =100,$expires=‘+10 hour’){  
  2.         // Set cache settings  
  3.         $key = ‘cat’;  
  4.         Cache::config(‘sql_cache’array(  
  5.             ‘prefix’    => strtolower($this->name) .‘-‘,  
  6.             ‘duration’  => $expires  
  7.         ));  
  8.         // Load from cache  
  9.         $results = Cache::read($key‘sql_cache’);  
  10.         if (emptyempty($results) && !is_array($results)) {  
  11.             $results =array();  
  12.             $data = $this->Category->find(‘all’);  
  13.             Cache::write($key$data‘sql_cache’);  
  14.             $results = Cache::read($key‘sql_cache’);  
  15.         }  
  16.         else {  
  17.             $tot_cats = $this->Category->find(‘count’);  
  18.             $cat_tot=count($results);  
  19.             if($data > $tot_cats )  
  20.             {   
  21.                 $results =array();  
  22.                 $data = $this->Category->find(‘all’);  
  23.                 Cache::write($key$data‘sql_cache’);  
  24.                 $results = Cache::read($key‘sql_cache’);  
  25.             }  
  26.         }  
  27.     return $results;  
  28.           
  29.     }  

Leave a Reply