PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] CakePHP 3: find() with cache

 March 15, 2022     caching, cakephp, cakephp-3.0     No comments   

Issue

About the get() method, I read here:

Like find() get has caching integrated. You can use the cache option when calling get() to perform read-through caching

But later, in the section dedicated to the find() method (here), the cache is not mentioned, there are no examples for the cache and the cache option is not mentioned among the supported options.

So I would like to know: can I use the cache option with the find() method? If so, how?

Thanks.


Thanks to ndm. So:

$query = $this->Pages->find('all');
$query->cache('all_pages');

$this->set('pages', $query->all());

Or (more simple):

$query = $this->Pages->find('all')->cache('all_pages');

$this->set('pages', $query->all());

Solution

The query builder doesn't suport caching via options, it has a separate method that is to be used, Query::cache(), which you'd use like

$query = $table->find();
$query->cache('cache_key');

$query->cache('cache_key', 'configName');

$query->cache(function($query) {
    return md5(
        serialize($query->clause('select')) .
        serialize($query->clause('where'))
    );
});

// ...

See

  • Cookbook > Query Builder > Caching Loaded Results
  • \Cake\Datasource\QueryTrait::cache()

get() supports caching via an option as that's the only way to configure the internal find call, since it's being executed immediately so that get() can return throw possible errors and return an entity.



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing