PHP model design with caching
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?
精彩评论