Jump to content

Proper place to put memcache caching


clevelas

Recommended Posts

I'm working on updating my PHP skills to "modern" standards (I started in PHP 3 and just do small projects, so haven't changed my approach much).  Actually going through the book "Modernizing Legacy Applications in PHP" by Paul Jones with a project that looks like it came right out of his book as a 'legacy' application.  

 

The project I'm working on (for an explanation of what I'm trying to do) talks SNMP to our network switches.  

 

In the original project, in the index.php script, I had:

 

$switch = new Switch(...)

$ports = $switch->getPorts();

 

 

The switch class used 'global $memcache' and checked memcache before talking to the switch.

 

As I understand it, in "modern" code, I should create the memcache object and inject it into the Switch class:

 

$memcache = new Memcache(...);

$switch = new Switch(..., $memcache);

$ports = $switch->getPorts();

 

And that works.  But I'm not sure if it's the correct approach.  Should the Switch class be dealing with memcache at all?  Or should that be happening somewhere else?  I don't think I'd want it in the index file.  Do I build a caching class that extends the Switch class and call that instead?

 

$memcache = new Memcache(...);

$switch = new cacheSwitch(..., $memcache);

$ports = $switch->getPorts();

 

Or do I get more complicated and build a generic SNMP class that is extended by a caching class that is extended by the Switch class?

 

This is mostly just an intellectual exercise at this point.  I have working code now and I've cleaned it up a lot already.  I'm just curious on what others think the "correct" approach is?

 

Thanks,

 - Steve

  

Link to comment
Share on other sites

One of the reasons to do dependency injection in your use case is to de-couple the specifics of caching from the specifics of the switch. Memcache is not the only caching technology out there, so you could improve the reusability and utility of the class by utilizing an interface and wrapping the memcache in a class that implements the interface.

 

You also have the option of doing setter injection rather than injecting memcache in the constructor. Again, that is something that makes things more optional.

 

Whether any of this effort is beneficial in your specific situation is up to you. If you're not planning to change code inside the Switch class, it hardly seems worth the effort, although there shouldn't be much of a noticeable difference when you pass a memcache object into the Switch class as a parameter. A fundamental change from 3.x to 5.x was PHP passing all objects by reference, thus eliminating a common reason people used Global objects in the olden days.

Link to comment
Share on other sites

Thank you for the reply gizmola.  This is the first I've heard of setter injection.  It might make sense here.  If a caching object is injected, then use it.  But it's not required.  And maybe that injection be a 'standard' caching object.  Following PSR-16 for example.

 

index.php:

 

$switch = new Switch();

//optional

$cache = new PSR16cache();

$switch->addCache($cache);

 

Switch.php:

 

class Switch {

  public function addCache (PSR16cache $cache) {

    $this->cache = $cache

  }

  public getPorts() {

    if ($this->cache) {

      if (!$this->cache->get('portkey')) {

        ...

      }

    }

  }

}

 

I agree, this may not be worth the effort if I'm not rewriting the class.  But as an intellectual exercise, it's very beneficial for future projects at least.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.