Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is faster? File_exist or MySQL query?

Users in my webgame are having certain player information cached in the $_SESSION of PHP. Each time they load the game it checks if the session exists, if not they get the player information from a MySQL database and then it gets stored in the $_SESSION.

Now my problem is, what if the player information gets updated by another process or player? They can't update the $_SESSION cache of the other player.

I know memcached is most probably the solution for this, but I'm not sure if I should take the time for something like this. $_SESSION cache is doing well for me, except for this.

  • I was thinking about creating a MySQL table for it which get read at every request and if there's a record for the player that it recreates the cache.
  • One other solution would be to create a file in a directory with the id of the player in the name of the file. Every request PHP will check with file_exist if it should clear the cache or not.

What would you guys do? It gets executed every request so it's pretty important to get this optimized.

like image 309
Martin Avatar asked Jan 03 '13 16:01

Martin


1 Answers

From a design standpoint alone I'd avoid the file_exists and directory approach. Sure 'file_exists' is fast, but it won't scale well... What happens if a use changes their name?

If you're using APC (and you should) you could APC's user memory cache. As long as you're on a single server it should give you similar performance benifits as memcached without the need for a separate memory caching server process. If a user entry changes frequently, you could run into fragmemntation issues with APC though. In that case, time to bite the bullet and go with memcached--you can even store your session data in memcached for a performance boost.

Also, neither APC or your file_exists solution will scale to multiple load balanced servers--you'd need a DB solution or memcached for that.

like image 111
Ray Avatar answered Sep 23 '22 15:09

Ray