How do I use cache in CakePHP?

19,827

Solution 1

if(!($cachedPosts = Cache::read('cached_posts'))) {
    $cachedPosts = $this->Post->find('all');
    Cache::write('cached_posts', $cachedPosts);
}

In this code example you look if you have the data cached - if not, retrieve it from the database, and write it to the cache. On the next request, the data will come from the cache, not from the database.

Solution 2

In the Cache documentation of the manual (1.2): http://book.cakephp.org/view/213/Cache

I would recommend that you disable caching while doing development; you'll find (hopefully not the hard way, like me) that your models and views are not changing as expected.

Solution 3

Read the Documentation:

Solution 4

Before using the cache we have to check that cache is enabled or disabaled in

app/config/core.php.

we have to uncomment this line in core.php

//Configure::write('Cache.disable', true);

After that we use

$varible = Cache::read('variable');
Cache::write('posts', $posts);
Cache::delete();
Share:
19,827
Arthur Johnson
Author by

Arthur Johnson

Updated on June 22, 2022

Comments

  • Arthur Johnson
    Arthur Johnson almost 2 years

    I want to use cache in CakePHP. How do I use it?

    • Lucas Jones
      Lucas Jones almost 15 years
      When I first saw this, I misread the title as "How do you use cake in cakephp" :)
    • cp3
      cp3 over 14 years
      best advice when asking a general question like this is to read the docs first, after that if you still have problems understanding, state what you didn't understand or a code sample of your app that's not behaving as expected, otherwise you'll just get links to the the manual.
  • gravyface
    gravyface almost 15 years
    Should note that if you must/want to use caching during development, you can manual clear the cache from your controller: Cache::clear() (see "7.2.6 Clearing the Cache" in the manual).