tags:

views:

65

answers:

3

I have the following arrays below in PHP that I store a user_id number in to keep track of moderator/admins on my site, then in a page I can just use in_array() to determine if a user should have moderator privileges, I figure this saves some mysql queries by using an array instead.

I am wondering, would there be any performance gain from combining these into 1 bigger array instead of seperate ones?

If so could you show an example how I can combine them and use it?

Maybe list some reasons for 1 way over the other?

$moderators = array(1,99,88,77,2,3,4,5);
$bulletin_moderators = array(1,1091,13,103);
$forum_moderators = array(1,1091,34850,13,103,21);
$blog_moderators = array(1,1091,21);
$photo_moderators = array(1,1091,13,34850,103,21);
$chat_moderators = array(1,44534);
+6  A: 

I think this is already fairly extreme an optimization. Unless you have some benchmarks showing a clear need for this, there's no reason not to just store the roles db-side and do a query. It ought to take only a tiny fraction of a second.

Eevee
yeah if you're already authenticating the user against the db, then if you store the users role(s) there then you can just grab it at the same time
mrinject
Not only should these be fast queries, hard-coding them like this makes it impossible easily write an administration tool to add/remove moderators, and getting into a pattern like this may lead to magic numbers all over your code, which is a maintainability nightmare.Are you sure this isn't a premature optimization?
Richard Pistole
Thanks but this isn't really helpful in my situation.
jasondavis
the main reason for this is not to exactly show a user there permission, but more to show other users which users have other permissions, this is a great way to cut down on 1 extra query to the DB
jasondavis
If for some reason you cannot store this in the database, then I'd use a hash of user_id => { role_name => 1, ... }. Then you can just test for $permissions{$user_id}{moderator}, etc. Please excuse my Perl syntax.
Eevee
Queries are not that limited a resource! Don't make your code more awkward to avoid a 10-millisecond query. You could store all your data in your source code and cut down on all the queries, but that doesn't make it a good idea.
Eevee
+3  A: 

Performance gain? No. Maintainability gain? Definitely. Try this:

$roles=array(
    'moderators'          => array(1,99,88,77,2,3,4,5),
    'bulletin_moderators' => array(1,1091,13,103),
    'forum_moderators'    => array(1,1091,34850,13,103,21),
    'blog_moderators'     => array(1,1091,21),
    'photo_moderators'    => array(1,1091,13,34850,103,21),
    'chat_moderators'     => array(1,44534)
);

As the above poster said though, these really shouldn't be hardcoded. A DB query wouldn't take any significant amount of time, and it would probably be cached in memory or in the database itself (assuming you use mysql).

What if you add more users? You would have to edit your source. What if you had multiple servers? You would have to edit your source on X amount of servers.

ryeguy
that's what I was looking for, thank you!
jasondavis
A: 

There would certainly be no performance gain, but you shouldn't be too corcerned with that. Something like this has a very low impact on the overall page performance.

Personally, instead of administering all these arrays, I'd create a User class that holds all the user's permissions. Check through a function in that class. Normally, you will also need the user name and other things about the user, and a User object is a nice way to consolidate all that.

Load everything you need to know about the user at the start of the page load, cache them in a session, whatever suits your needs.

Marcel
the main reason for this is not to exactly show a user there permission, but more to show other users which users have other permissions, this is a great way to cut down on 1 extra query to the DB
jasondavis