• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot overload cache drivers (memcached)

[eluser]Antoine P.[/eluser]

I have a question about caching in CI 2.0. As I am working on a project that may involve several web sites/dev teams, I would like to overload caching driver (memcached in my case) in order to automatically add a prefix key to my cache keys, depending on an environment var previously set.

I've tried to overload the core Cache_memcached driver (libraries/Cache/drivers) but it seems cache drivers can only be replaced and not overloaded. Then, I tried to extend the core Cache library instead (libraries/with the same result.

I've found a trick which consists in modifying the CI_Driver_Library::__get method but this is not a good solution to me as it modifies my CI core.

Can someone help me please?


How have you actually managed to override the whole driver?

I'm stuck with the same problem you have, I need to prefix the keys and am not going to touch the core CI code.

I've tried all location I can think of for the file


with varying class declarations

class MY_Cache_memcached extends CI_Cache_memcached {
class MY_Cache_memcached extends Cache_memcached {
class MY_Cache_memcached extends CI_Driver {

What combination did you have?


[eluser]Antoine P.[/eluser]
Actually, I didn't succeed in overloading the driver without modifying the system files. I think you can rewrite the driver by defining the class Cache_memcached in libraries/Cache/drivers/Cache_memcached.php but if you want to overload the class, you can't (or at least I don't know how) do it easilly.

The trick I've found is creating my MY_Cache_memcached class that extend Cache_memcached in libraries/Cache/drivers/Cache_memcached.php. Then, I modified the CI_Driver_Library::_get method and added
if (class_exists(config_item('subclass_prefix').$child_class))
    $child_class = config_item('subclass_prefix').$child_class;
before class instanciation. Finally, I hard wrote the Cache_memcached parent class inclusion in MY_Cache_memcached.

You get it?

Ok thanks.

You cannot define Cache_memcached in libraries/Cache/drivers/Cache_memcached.php as you get the PHP redeclare class error.

I'll go down a similar route to you.


class MY_Cache_memcached extends Cache_memcached {

And then try overload /libraries/Driver.php.

Determined not to touch the core files. It took long enough to get a mix of

$config['enable_query_strings'] = TRUE;
$config['index_page'] = '';
$config['uri_protocol']    = 'AUTO';

to not display the ? in the URL.

eventually to

with still allowing

[eluser]Antoine P.[/eluser]
As I said, my "solution" is not a "good" one to me and it only works in a precise case (mine) but I'm not satisfied.

Someone help would be appreciate about how to overload my driver :'(

Hum I cannot seem to find a way to overload the Driver.php file

I was going to use similar logic to what has been used in


to get if it is a driver extension request or not.

$subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;

            // Is this a class extension request?
            if (file_exists($subclass))
                $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;

                if ( ! file_exists($baseclass))
                    log_message('error', "Unable to load the requested class: ".$class);
                    show_error("Unable to load the requested class: ".$class);

Going to implement what I am after another way for the time being.

I have a global helper class, going to have to put the function in there to prefix my cache keys and crawl though my code changing all occurrences.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.