Welcome Guest, Not a member yet? Register   Sign In
Cannot overload cache drivers (memcached)
#1

[eluser]Antoine P.[/eluser]
Hello,

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?
#2

[eluser]CoderBoy[/eluser]
Hey,

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

Code:
application/core/MY_Cache_memcached.php
application/libraries/MY_Cache_memcached.php
application/libraries/Cache/Drivers/MY_Cache_memcached.php

with varying class declarations

Code:
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?

Thanks
#3

[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
Code:
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?
#4

[eluser]CoderBoy[/eluser]
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.

Code:
application/libraries/Cache/Drivers/MY_Cache_memcached.php

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

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

to not display the ? in the URL.

http://127.0.0.1/?/controller/method

eventually to

http://127.0.0.1/controller/method

with still allowing

http://127.0.0.1/index.php?c=controller&m=method
#5

[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 :'(
#6

[eluser]CoderBoy[/eluser]
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

system/core/Loader.php

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

Code:
$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.




Theme © iAndrew 2016 - Forum software by © MyBB