milesj / utility

[Deprecated] A CakePHP plugin for common utility classes.
MIT License
69 stars 24 forks source link

Advanced Caching: pass $keys to callback #12

Closed Ali1 closed 11 years ago

Ali1 commented 11 years ago

Hi Miles

I couldn't use the advanced caching for methods that require an id parameter as the id wouldn't get passed to the callback.

However if you change $results = $callback($model); to $results = $callback($model, $keys); in CacheableBehaviour.php around line 353

the the callback function can acquire the $id of the item being cached and be able to do an effective $this->find for single items.

milesj commented 11 years ago

Wouldn't the ID be set on the model? Or in turn, couldn't you set it there?

Using the cache key to inflect the ID seems odd.

Ali1 commented 11 years ago

Yes good point about the ID didn't think of that probably because I was actually needing to pass $limit as well (which is also in the key). Hmm perhaps I could use public $limit in my model instead if you think that's better.

On 22 August 2013 18:10, Miles Johnson notifications@github.com wrote:

Wouldn't the ID be set on the model? Or in turn, couldn't you set it there?

Using the cache key to inflect the ID seems odd.

— Reply to this email directly or view it on GitHubhttps://github.com/milesj/Utility/issues/12#issuecomment-23108450 .

milesj commented 11 years ago

I'll probably add this anyways since it could be helpful.

Ali1 commented 11 years ago

Ah wait a minute I was completely baffled in my answer. The id I refer to originally isn't necessarily the primary key of the model. For example function getPostsByUser($user_id) and then you wanted to attach some comments to each post with advanced caching.

On 22 August 2013 18:42, Ali Bakir ali.m.bakir@gmail.com wrote:

Yes good point about the ID didn't think of that probably because I was actually needing to pass $limit as well (which is also in the key). Hmm perhaps I could use public $limit in my model instead if you think that's better.

On 22 August 2013 18:10, Miles Johnson notifications@github.com wrote:

Wouldn't the ID be set on the model? Or in turn, couldn't you set it there?

Using the cache key to inflect the ID seems odd.

— Reply to this email directly or view it on GitHubhttps://github.com/milesj/Utility/issues/12#issuecomment-23108450 .

Ali1 commented 11 years ago

And I'm just spitballing here but there could potentially be more cases where this is useful for example if someone wanted to create a single callback function that can be used in many methods; providing the keys means could mean a very intelligent function could be made.

$results=$this->cache(array(METHOD, $id, $limit), $this->resultsHander($model, $keys))

I'm not sure if this would work but would be quite interesting.

On 22 August 2013 18:44, Ali Bakir ali.m.bakir@gmail.com wrote:

Ah wait a minute I was completely baffled in my answer. The id I refer to originally isn't necessarily the primary key of the model. For example function getPostsByUser($user_id) and then you wanted to attach some comments to each post with advanced caching.

On 22 August 2013 18:42, Ali Bakir ali.m.bakir@gmail.com wrote:

Yes good point about the ID didn't think of that probably because I was actually needing to pass $limit as well (which is also in the key). Hmm perhaps I could use public $limit in my model instead if you think that's better.

On 22 August 2013 18:10, Miles Johnson notifications@github.com wrote:

Wouldn't the ID be set on the model? Or in turn, couldn't you set it there?

Using the cache key to inflect the ID seems odd.

— Reply to this email directly or view it on GitHubhttps://github.com/milesj/Utility/issues/12#issuecomment-23108450 .

milesj commented 11 years ago

Yeah good point, will add.

milesj commented 11 years ago

Added!