How do I use cache in CakePHP?

2019-03-28 16:38发布

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

6条回答
迷人小祖宗
2楼-- · 2019-03-28 16:42

CakePHP provides by default file based caching at model level also...

then also to maintain manual caching for any application you can use

Cache::set(array('duration' => '+100 days'));
Cache::write(file_path);

type functions to maintain file based cache

For memcache or any other concept you can take the following links as reference:

查看更多
Deceive 欺骗
3楼-- · 2019-03-28 16:48
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.

查看更多
成全新的幸福
5楼-- · 2019-03-28 16:49

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();
查看更多
唯我独甜
6楼-- · 2019-03-28 16:51

Check out the /app/config/core.php file.

查看更多
7楼-- · 2019-03-28 16:54

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.

查看更多
登录 后发表回答