Welcome Guest, Not a member yet? Register   Sign In
How implement cache on Search query sql
#1

Hi guys,


I need to be able to cache all kinds of searches with like.
I have it this way, according to the documentation, but according to the profiler, it doesn't save in cache and the query is still running.


SearchModel
PHP Code:
public function search($product)
 
   {
 
       $this->db->cache_on();
 
       $data $this->db
            
->select('name, slug')
 
           ->from('products')
 
           ->like('name'$product)
 
           ->or_like('slug'$product)
 
           ->limit(5)
 
           ->get()
 
           ->result_array();
         
$this->db->cache_off();

 
       return json_encode($data);
 
   

SearchController:
PHP Code:
public function index($product)
 
   {
 
       $product urldecode($product);
 
       $product preg_replace('/\s+/''-'$product);
 
       $this->load->Model('api/search/SearchModel');

 
       echo $this->SearchModel->search($product);

 
       $this->output->enable_profiler(TRUE);
 
   

[Image: 7152dc383ed4e975467591358008d7db.png]


Directory application\cache is empty...
[Image: 9b422656b418d1b101dce28e2487181a.png]
Reply
#2

(This post was last modified: 12-18-2018, 04:09 PM by php_rocs.)

@Decibel150,

What version of CI are you using?  Is the CI cache directory writable? Are there any errors in the CI log file? Also, have you tried this...


PHP Code:
public function search($product)
   {
       $this->db->cache_on();
       $data $this->db
           
->select('name, slug')
           ->from('products')
           ->like('name'$product)
           ->or_like('slug'$product)
           ->limit(5)
           ->get();
        $this->db->cache_off();

       return json_encode($data->result_array());
   
Reply
#3

(12-18-2018, 04:00 PM)php_rocs Wrote: @Decibel150,

What version of CI are you using?  Is the CI cache directory writable? Are there any errors in the CI log file? Also, have you tried this...


PHP Code:
public function search($product)
   {
       $this->db->cache_on();
       $data $this->db
           
->select('name, slug')
           ->from('products')
           ->like('name'$product)
           ->or_like('slug'$product)
           ->limit(5)
           ->get();
        $this->db->cache_off();

       return json_encode($data->result_array());
   


i use the last Version (3.1.9), does not give any kind of error, and the permits are the right ones. that might be failing?
Reply
#4

Okay, I just found the bug...
it was missing to indicate the cache directory in database.php

https://i.imgur.com/j8RgLiz.png
Reply
#5

@Decibel150,

Awesome, glad you figured it out.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB