Jump to content

Session or database?


zander1983

Recommended Posts

Hi

 

A part of my site allows users to send messages to other users. When a member is logged on, they see a panel on the left with a link to the messages page. If there is a message they have not seen, it looks like messages(1). As this panel is on every page, the message(1) is displayed on every page.

 

My question is a general one which i've always wondered about - I determine whether all messages have been read or not from the database. Should I go once to the database when user logs on, and save this value to a session, or should i go to the database each time the member goes to a new page....

 

The reason I ask is because I am saving a lot of data in the session already so where do I draw the line between saving stuff to a session and just repeatedly going to the database..

Link to comment
Share on other sites

I think adding a small INT row to the message table called "read" with the default setting of "0" and updating it to "1" when viewed will give you better results as sessions will go away and then show as the message as not have been read, which could be frustrating to people.  This doesn't have to be updated if the value is already "1" or the message has been read.  Just call the value['read'] along with content of the message and using an IF statement, if the read value is 0 make the update.  You could also make it a readcount that adds 1 to the number each time it's read.  This information could be used to tell the author that their message has been read and how many times.  Hey, just a thought.

Link to comment
Share on other sites

I think adding a small INT row to the message table called "read" with the default setting of "0" and updating it to "1" when viewed will give you better results as sessions will go away and then show as the message as not have been read, which could be frustrating to people.  This doesn't have to be updated if the value is already "1" or the message has been read.  Just call the value['read'] along with content of the message and using an IF statement, if the read value is 0 make the update.  You could also make it a readcount that adds 1 to the number each time it's read.  This information could be used to tell the author that their message has been read and how many times.  Hey, just a thought.

 

I think it's clearly established already that OP already has a login system and is using sessions, so your comment about "sessions going away" is completely irrelevant.  If the session "went away" then the user will no longer be logged in. 

Link to comment
Share on other sites

And what happens the next time the user logs in?  Won't it then show the message as not have read?  That's my point on the session going away,  not regarding a logged in user session.

 

No.  There is nothing magic about a session variable, other than the fact that it is serialized out to storage when it gets set, and this serialized version gets read into memory with session_start().

 

Exactly what that variable contains is up to the programmer.  In this case, the assumption is that the OP has a routine that counts the number of message that have not been marked as seen/read. 

 

Like any form of "caching" the value needs to be invalidated/updated anytime something happens that would change that.  Like all caching, you have a tradeoff between performance and accuracy. 

 

 

 

The only problem with this system is that a user will not be notified of new messages that are sent during the duration of the session, although this could be mitigated by adding in a timing feature, that insures that the "getunreadmessagecount" routine gets called again if a certain amount of time has gone by.  This could be designed to occur every 3-5 minutes, and for a high traffic site with a lot of users the savings on database queries could be significant.

 

In my opinion, a memcache based design is superior, since the cached entry can be invalidated whenever a message is read OR sent to a particular user.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.