zander1983 Posted April 22, 2011 Share Posted April 22, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/ Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 well saving it to a session will definitely decrease upload time..but you need to make sure that the value is updated frequently Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205041 Share on other sites More sharing options...
gizmola Posted April 22, 2011 Share Posted April 22, 2011 I can't speak to what else you have in the session, but something like that is the perfect use of a session variable. Another alternative is to cache the data in an in-memory cache like apc or memcache. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205046 Share on other sites More sharing options...
Drummin Posted April 22, 2011 Share Posted April 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205052 Share on other sites More sharing options...
gizmola Posted April 22, 2011 Share Posted April 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205066 Share on other sites More sharing options...
Drummin Posted April 22, 2011 Share Posted April 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205069 Share on other sites More sharing options...
gizmola Posted April 22, 2011 Share Posted April 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234472-session-or-database/#findComment-1205100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.