Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

CI 2.1.2 :: Database Cache Bug #1658

Closed
cconkrig opened this Issue Jul 27, 2012 · 6 comments

Comments

Projects
None yet
2 participants

The database cache class only returns "objects."

For example:

This will work: - the cache is returned and the db is left alone.

        return $query->result();

this will not: - this is empty and you get the error not an object

                return $query->result_array();

I spent all day today tracing the database cache code to try to figure out why all my cached queries were empty. Please add some type of check to determine what type of data to return... for now.. I am going back and re-writing a lot of my application to use the "result" option versus "result_array." I have no idea what "row" does in this scenario... If the the result types are allowed, then they have to be able to be returned corrected. I use result_array almost exclusively in my models.

Arrays should be faster - that's why I use them vs the object.

Contributor

narfbg commented Jul 27, 2012

Actually, working with objects is usually slightly faster, which comes at the price of memory usage. But that's neither of those is significant for anybody to notice.

Nevertheless - IMO, it's a bug. Does it affect code in the develop branch ot just 2.1.x?

Thanks for taking a look at this narfbg (and for the tip on the objects). I am not sure on the develop branch... I don't know how to test that. I am running 2.1.2 and it's broken there. Unfortunately this is a production server and I don't have a test machine with this code on to play with unless it's after hours, I can swap a file or two and test if you can point me to what to do.

Actually just looking at the file itself on github... I see that we are passing an object into the write function.

This is from: system / database / DB_cache.php

    /**
     * Write a query to a cache file
     *
     * @return  bool
     */
    public function write($sql, $object)
    {

..........

        if (write_file($dir_path.$filename, serialize($object)) === FALSE)
        {
            return FALSE;
        }

So it looks like it will have to be fixed somewhere else up the chain...
Here it even says "Result Object" -- this is from the "Query Function" inside system / database / DB_driver.php

        // Is query caching enabled? If so, we'll serialize the
        // result object and save it to a cache file.
        if ($this->cache_on === TRUE && $this->_cache_init())
        {
            // We'll create a new instance of the result object
            // only without the platform specific driver since
            // we can't use it with cached data (the query result
            // resource ID won't be any good once we've cached the
            // result object, so we'll have to compile the data
            // and save it)
            $CR = new CI_DB_result();
            $CR->result_object  = $RES->result_object();
            $CR->result_array   = $RES->result_array();
            $CR->num_rows       = $RES->num_rows();

            // Reset these since cached objects can not utilize resource IDs.
            $CR->conn_id        = NULL;
            $CR->result_id      = NULL;

            $this->CACHE->write($sql, $CR);
        }

All this is in the current github code. Any ideas how to fix it? Check for Array, Row, or Object?

cconkrig commented Aug 6, 2012

Anyone have any idea on this?

I know something like is_array and is_object has to be interwoven somehow...

Contributor

narfbg commented Nov 26, 2012

@radiochris Sorry about the delay on this.

I'm pretty confident in saying that this should be fixed in the mentioned develop branch (that's what you're looking at via the GitHub file browser by default, unless you've switched the branch manually). You can download a snapshot of the current development files from here: https://github.com/EllisLab/CodeIgniter/archive/develop.zip
After that, you can either use the whole thing or just replace the system/database/ directory.

@narfbg narfbg closed this Nov 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment