Is there difference between caching PHP objects on disk rather than not? If cached, objects would only be created once for ALL the site visitors, and if not, they will be created once for every visitor. Is there a performance difference for this or would I be wasting time doing this? Thank you :)
The performance comes down to subsequent use so even if your object is cached once and used by all ... or cached once per visitor ... it is the subsequent use that counts. If your cached object is used 10,000,000 times a day then you will save.
If the cached object is used once or not at all, the gain is negligible.
You'll have to take in account that dynamic content must be cached whenever it changes. But, to static content, it's much more faster to use cached content, especially in database schemas, configurations, and that kind of thing.
Assuming that they are large objects, and especially if they are being built based on data pulled from a database, caching is a good idea. For some testing I did with a particular instance, it was quicker to build an object, write it to a file, and load from the file, than to go to the DB every time.
However: there are better ways than writing to a file. You would probably be better off storing the object in memory with memcached or APC, and don't forget that you may have issues with locks on the file is several people are hitting the site at once.
It's like this: File is way faster then Database. Memory is way faster then files (when not swapping disk space).
So cache to memory what you need the most, and try and think when you need to make the effort to cache to file.
Remember: premature optimisation usually isn't the best thing to do.
Caching objects on disk will give you the additional overhead of a disk write whenever there is a cache miss, whereas on a cache hit you will be able to save the step of instantiating the object.
If construction of the object is enough of a burden that saving it sometimes will more than outweighs the additional burden of a disk write, then go ahead and cache on disk.
I'd be curious, however, to know more about why instantiating your objects is so costly that you would want to do this.
@Aiden Bell
Errm. Depends on the object. If one instance can be shared then you save storage ... but it depends if the object is altered by the user. You will be reading/writing the object creating a bottle-neck. Without more information, you will have to lookup the theory here. – Aiden Bell 11 mins ago
-- These instances don't have data that can be altered by the user, however, they would contain data that would be used by users throughout the site.
Basically, when it comes down to it, the main question is:
Multiple objects in memory, PER user (each user has his own set of instantiated objects)
VS
Single objects in cached in file for all users (all users use the same objects, for example, same error handler class, same template handler class, and same database handle class)
--
Hi, anyone else have any ideas?