Caching Classes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Caching Classes

Post by programmermatt »

I am participating in a rather large project and am trying to find ways to optimize the scripts. My question is wether it would be smart to set up a server side caching system for classes such as our authentication, permission and user class instead of recreating them every page load (as they use a good amount of queries and logical structures that must recursively process data on creation) ? If so, would it be better to cache via file, DB or something like $_SESSION? How far would such caching be safe and should short or longer expirey times be set? Assuming that there is no way for the wrong cached item to go to the wrong user.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I cache classes in sessions all the time:

Code: Select all

$ClassObj= new MyClassIsCoolerThenYourClass($data);

$_SESSION['ClassObj'] = &$ClassObj;

$_SESSION['ClassObj']->getData();
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post by programmermatt »

Yeah, I know how to put objects in $_SESSION, but I am looking for wether it is truly more benificial and for any possible issues that might be caused by the caching, rather than regeneration of classes.

I now realize that this is probably a per-case issue and I should probably run benchmark tests.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Generally, if I'll be calling the same class page-to-page I store it in a session. However, very rarely do I have enormous sized classes.

Think about it this way; If you are using the same class every page you're still using the same amount of memory then if you store it in a session var.
Post Reply