Hi,

I did an example to see the time it takes to run a query and cache it but it doesn’t work and I cannot find why. Please give me a hand. No cache file gets created as well.

In config/database.php:
I’ve set $db[‘default’][‘cache_on’] = FALSE; because I don’t want to cache every queries.
I’ve set $db[‘default’][‘cachedir’] = ‘application/cache’; to store cache files.

Thanks in advance

VIEW : cache_view.php

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
    
    <h1><?php echo $heading; ?></h1>
    
    <?php echo anchor('cache/benchmark_cache', 'Start query benchmark'); ?>

</body>
</html>

CONTROLLER : cache.php

<?php if (! defined('BASEPATH')) {    exit('No direct script access allowed'); } ?>


<?php
class Cache extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        
        // Helper for <a...></a>
        $this->load->helper('url');
    }
    
    public function index()
    {
        // Webpage basics
        $data['title'] = "Cache";
        $data['heading'] = "Cache Benchmark checking";
        
        $this->load->view('cache_view.php', $data);
    }
    
    public function benchmark_cache()
    {
        // Webpage basics
        $data['title'] = "Cache";
        $data['heading'] = "Benchmark result";

        // Load model for db processes
        $this->load->model('cache_model');
        
        $this->benchmark->mark('code_start');
        
        $data['queryresult'] = $this->cache_model->get_benchmark();
        //sleep(5);

        $this->benchmark->mark('code_end');
        

        echo $this->benchmark->elapsed_time('code_start', 'code_end');

    }
}

//** EOF

MODEL : cache_model.php

<?php if (! defined('BASEPATH')) {    exit('No direct script access allowed'); } ?>


<?php
class Cache_model extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
        
        $this->db->protect_identifiers('assessment');
    }
    
    public function get_benchmark()
    {
        $this->db->cache_on();
        
        $sql = "SELECT * FROM assessment ORDER BY 2";
        $data['dbquery'] = $this->db->query($sql);
        
        return $data['dbquery'];
    }
}

//** EOF

Forgot to mention in subject. This is a Codeigniter example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.