For me, caching the results of a peer class function, does not interfere
with the MVC model. Even the opposite, the sfArrayCache is created with
the idea of making expensive operations not that expensive. So I would
suggest you go ahead and put the caching in the Peer class and just make
sure that you have enough logic not to pull old data from the cache when
there is already an update in the database and you should be in good shape.
Now, I think you have two ways to query the database only once and it
depends on your use case.
1) First use case is if you call the Peer class function several times
during ONE page load. For example, in your template you have a foreach
that calls that function 10+ times. In this case you can create a static
class variable and populate the results there between calls of the
function. But let me give you an example, otherwise I am even confusing
myself.
<?php
class BlogPeer extends BaseBlogPeer
{
static $result = array();
public function expensive()
{
if (empty(self::$result)) {
self::$result = BlogPeer::doSelect(new Criteria());
}
return self::$result;
}
}
?>
Then in the place where you want to use that function you will need to
create an instance of the BlogPeer class and use the function:
<?
$peer = new BlogPeer();
for($i=0;$i<10;$i++) {
$x = $peer->expensive();
}
2) The second scenario is when you wan to cache the results of the
function because it will be called by many users but not in the same
execution. And you already have the solution there with sfArrayCache.
Hope that helps.
Kupo
Greg Freeman wrote:
> If you had to clear multiple array caches when a peer method is
> called, where would be the best place to store a clear method? In the
> peer class or somewhere else?
>
> For example similar code such as this is repeated a few times
>
> $cache = new sfArrayCache(sfConfig::get('sf_cache_dir'));
> $cache->remove('something1', 'namespace');
> $cache->remove('something2', 'namespace');
> $cache->remove('something3', 'namespace');
>
> So I think i should store this in it's own method and just call it,
> but I'm wondering if putting it in the peer class is bad practice.
>
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---