tags:

views:

115

answers:

3

I've been working on a simple forum for my site. I'm adding a feature that will mark unread posts + forums since your last visit.

Im essentially storing a date of last access to the forum with each user account. When a user comes back to the site, a query is ran to fetch all the post_id and post_parent_forum values, which are then added to an array, that's stored in a SESSION variable with the key being the post_id (which is unique) and the value being the forum id (which wont be unique since posts will appear in the few forums).

In the forum index, I use in_array() for each forum, to see if the forum_id is in the array, if it is, it will be marked as "unread".

In the thread listing, I use array_key_exists() to see if the key for each thread ID is in the array, if it is, its marked as "unread".

When a post is viewed, the item is removed from the array with the key equal to the ID of the thread.

Is this method reasonable, or am I going to run into issues if the forum becomes more popular? Im concerned about running 20 array_key_exists() checks on each forum listing. Is it reasonably fast?

On a side note.... can I work directly with the SESSION stored array, or do I have to assign its value to a regular variable, remove a key, unset the old session var, and re-set it with the updated array?

+1  A: 

php arrays are hashtables! a hash key lookup is not very expensive, so no, i don't think you'll run into performance problems because of this.

in_array is a different matter (searches through the whole array), but really - it still shouldn't be a problem. beware of premature optimization!

can I work directly with the SESSION stored array

yes. $_SESSION is (normally) just an array that is populated when you call session_start() by unserializing the session files content. when the script ends or session_write_close() is called, the $_SESSION array is serialize()'d back to the file. no magic, really.

if you should do that is another question, after all $_SESSION is a global variable (ewwww).

Schnalle
I figured its quicker (and lazier) to do that.... instead or reading, modifying, unsetting and resetting.
Yegor
nothing wrong with that, if things don't get too complicated. but i've seen sessions monsters that could drive a brave man insane. if i want to stay clean i normally write a class that acts as a gatekeeper.
Schnalle
A: 

Just another route you can go...

Have a last_activity timestamp field in the user table and update it everytime the user loads a new page.

Have a last_updated timestamp field for each post/forum.

To see if a forum/post is "unread" you can just compare the timestamps.

Galen
activity timestamp would be updated regardless of me viewing a post.
Yegor
im not sure what you're saying. A users active ts is updated every page load. Post/forum ts is updated every comment/thread/whatever. So you know the last time you loaded a page and the last time the object was updated. you should be able to get unread objects with that info right?
Galen
If the post timestamp would get updated on a view as well, it would register posts as read by you even when someone else read them. If not, every post would get "read" after the user loads their first page.
Stefan Thyberg
You still have to MARK the read objects somehow. If the user activity timestamp is greater than the last updated timestamp for a thread, it will appear as "read" even though it was never opened.
Yegor
ah yes, i see what you mean now. sorry.
Galen
A: 

My practice says: don't worry about hash-tables performance, but think about db-related issues, because they are the most expensive in resources (especially in forum-like projects which are full of text). I never met issues related to PHP performance. MySQL issues always came much faster.

Jet