开发者

PHP model design with caching

开发者 https://www.devze.com 2023-04-08 15:12 出处:网络
I\'m designing the model for an application that will get/set objects in a remote database. For performance, we need to cache the data locally. I\'ve read in various threads that it\'s best to put the

I'm designing the model for an application that will get/set objects in a remote database. For performance, we need to cache the data locally. I've read in various threads that it's best to put the caching logic into a parent model rather than the main controller. This is probably a basic OOP question, but I'm not sure about the implementation.

In my PHP design, I have three objects. Let's call them P (for Parent model), L (for Local) and R (for Remote). Currently, I'm planning on having them interact like this:

class P
{
   var $column1;
   var $column2;
   ...
   var $R;
   var $L;
   ...

   function __construct()
   {
      $this->R = new Remote();
      $this->L = new Local();
   }

   function get($id)
   {
      if (is_cached())
      {
         $this->L->($id);
      }
      else
      {
         $this->R->get($id);
      }
   }
}

class R
{
   public function get($id)
   {
      return $this->remoteDB->get($id);
   }
}

class L
{
   public function get($id)
   {
      return $this->localDB->get($id);
   }
}

The problem I s开发者_开发问答ee with this design is that it is A) probably requiring load() functions in all three objects which seems inefficient and B) requires passing properties from one object to another (rather than just referencing them directly).

Any suggestions for a better way of doing this?


I would suggest that you look into the existing frameworks that deal with database access tiers and have built in cache management.

A Few: Kohana, Zend, Code Ignitor

I'm also not sure why you can't abstract your load functions and have your models extend the abstraction?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号