views:

153

answers:

3

I am wondering if it is viable to store cached items in Session variables, rather than creating a file-based caching solution? Because it is once per user, it could reduce some extra calls to the database if a user visits more than one page. But is it worth the effort?

A: 

Session should only really be used strictly for user specific data. If you're using it to cache things that should be common across multiple sessions, you're duplicating a lot of data needlessly. Why not just use the Cache that comes with ASP.NET (you can use inProcess, rather than SQL if your concern is DB roundtrips, since you'll be storing Cached data in memory)

Janie
It might very well be, but where does it say that this is about ASP.NET? :)
Thorarin
Its actually the very opposite. I'm using PHP lol.
Chacha102
Hee hee, should have read the tags, sorry!
Janie
Oh wait, when I answered it, there werent tags specifying which lang/plat...
Janie
+1  A: 

It can be, but it depends largely on what you're trying to cache, as well as some other circumstances.

  • Is the information likely to change?
  • Is it a problem if slightly outdated information is shown?
  • How heavy is the load the query imposes on the database?
  • What is the latency to the database server? (shouldn't be an issue on local network)
  • Should the information be cached on a per user basis, or globally for the entire application?
  • Amount of data involved
  • etc.

Performance gain can be significant in some cases. On a particular ASP.NET / SQL Server site I've worked on, adding a simple caching mechanism (at application level) reduced the CPU load on the web server by a factor 3 (!) and at the same time prevented a whole bunch of database timeout issues when accessing a certain table.

It's been a while since I've done anything serious in PHP, but I think your only option there is to do this at the session level. Most of my considerations above are still valid however. As for effort; it should take very little effort to implement, assuming your code is sufficiently structured.

Thorarin
+1  A: 

If the data you are caching (willing to cache) does not depend on the user, why would you store in the session... which is attached to a user ?

Considering sessions are generally stored in files, it will not optimise anything in comparaison of using files yourself.

And if you have 10 users on the site, you will have 10 times the same data in cache ? I do not think this is the best way to cache things ;-)

For data that is the same fo all users, I would really go with another solution, be it file-based or not (even for data specific to one user, or a group of users, I would probably not store it in session -- except if very small, maybe)

Some things you can look about :

  • Almost every framework provides some kind of caching mecanism. For instance :
  • You can store cached data using lots of backend ; for instance :
    • files
    • shared memory (using something like APC, for example)
    • If you have several servers and loads of data, memcached
    • (some frameworks provide classes to work with those ; switching from one to the other can even be as simple as changing a couple of lines in a config file ^^ )

Next question is : what do you need to cache ? For how long ? but that's another problem, and only you can answer that ;-)

Pascal MARTIN