Suitable architecture for storing "Seen" messages [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm sure all of you have seen at some point at some website some message - i.e. you logged in and it stated something like "There will be a planned maintenance downtime at Tuesday 5PM PST". And then you have the tick mark for the message. When you click on it, you don't get the message shown any more.
Now, my question is how to handle this in the database. I.e. if you have 1M registered users and you decide to store this "seen" record for every user you'll end up with a 1M table which has to be read from every time someone logs in. On the other hand, if you have 10 messages like that - of course your table grows much faster. And finally, there could be messages like "Johnny,Harry,Diane and 5 of your other friends have updated their profile."
You could have like 10-50 messages like that daily. What I'm trying to think of is the best approach to this. I've implemented a lot of solutions in the past but I'm rethinking and am wondering of how others in the community are handling problems like that!
Edit:
#hakre
Thank you for your comment. Actually, I did describe one way I handled it in the question itself. Don't get me wrong but if I propose solutions, answers tend to discuss those proposed solutions which isn't something I'd like.

Do you have any NoSQL at hands?
I use redis, and I would create an set with all the users ids as members of the set (add the ids if the user should be marked as seen)... To check if the user has seen the message a simple SISMEMBER will suffice and it's O(1)...
Plus, you won't need any db query or alter table for the seen field.
http://redis.io/commands/sismember

Related

How to prevent posting duplicate comments on Wordpress? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have looked everywhere, here and on the web, and couldn't find the solution.
Basically, I'm trying to prevent posting duplicate comments on WordPress.
I have read somewhere that it's not that simple, and first WordPress needs to compare already posted comments.
Maybe someone has a quick and easy solution, or have done/implemented something similar on their own Wordpress blog.
Maybe a simple code for functions.php?
Any help would be greatly appreciated.
Although what you ask seems reasonable, in reality it has a design flow: ensuring that every comment, ever, is unique is expensive as it depends linearly on all your comment base. This means that every time a user posts a comment, that operation will take more and more to complete.
Beside this, defeating this expensive validation is trivial and involves just adding some padding characters: suddenly, you must implement more custom checks for every comment posted, thus slowing down your site more.
The more your site slows down, the more resources (and money) and money you need. Assuming you are fighting spam, there are plugins designed for that purpose (like Akismet cited in the comments). A spam filter plugin doesn't scan all your comments, but uses more advanced techniques to evaluate different aspect of the text to be analyzed, and then decide whether or not to filter it.

PHP - Best way to keep track of IPs that voted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a small side project on which guests can vote on some sort of posts with either agree or disagree, then I want to save the IP that voted in the database. I was thinking of saving an array of IP's that voted in the 'posts' table, however that might be insufficient I guess. Would a separate table like the following be recommended?;
post_votes:
post_id, IP
Doesn't matter if the user voted agree or not, as long as the vote is counted they won't be able to vote again. What's the best approach for this? Thanks in advance.
Yes, the best way is to use a seperate table with identifiers (e.g. post_id) to identify the posts.
Never save more than one information per field - saving the information in an array in the same table is cruel.
However, do NOT store the IP address 'as is' in the database as this is illegal in many countries and can cause a lot of trouble!
Instead use a hashing function like hash_hmac (http://php.net/manual/de/function.hash-hmac.php), hash the IP address and save the hash instead..
Because hashes are one-way functions, this will ensure anonymity for the user but still give you the opportunity to prevent users from voting several times.

Is it ok (is it really bad) to make many small queries in a loop? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Skip that if you value your time.
I am making a website that has a special "control panel" for a few (less than 20) people. It is a calendar where you can see which days aren't occupied yet and book "events" to them if it's needed. Panel uses php and mysql database to hold usernames, events etc. It is pretty much finished, but i noticed that i need to check if exact same data isn't stored in database already (it wouldn't cause problems, but it would be better to avoid redundancy).
First solution i came up with is just add a query (checking if there is an appointment in the time) to every added row (they are added in a loop) and do the INSERT only if checking query returned 0 rows.
You can really ignore anything higher than that, i added explanation only to clarify situation more.
My question is: Is it really bad for the server if there are many small queries run in a loop, or that doesn't really matter because queries are not that hard for a server? Maybe it really matters and i should for example run one, bigger query and save it into an array or something?
It probably doesn't matter but i am using object oriented setup from there
I am asking mostly to expand my knowledge, because this utility will be used only by one person anyways, so server will keep up even if it is not really efficient.
This is too long for a comment.
Is it a really bad idea? Depends on your definition of bad. In general, multiple such queries are more expensive. And multiple queries introduce more opportunities for race conditions. Doesn't sound like a "good" idea to me.
More importantly, though, is checking for duplicates in the application. You have a database and can use the database to prevent duplicates. This is called a "unique" constraint or a unique index (the former is implemented using the latter). Your question doesn't have enough information to make a concrete suggestion, but that sounds like what you really need.

SQL table - Data management [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am creating a website where users can easily calculate the calories they eat and see the repartition in term of fat, carbo, etc.
I want the users to be able to retrieve data from previous days.
I then need to store the data sent by my users everyday (basically, they input how much of each food they have eaten everyday and I am making the calculation then store the results).
The question if the following: what would be the best way to store the data? I have to store the data for each user for each day. I can't think of a simple solution (I think creating a new table for each new day would not be great, would it?).
I'm using PHP and MySQL for now.
Thanks for the help!
It seems that you are a step ahead of your self with the daily breakdown question.
First, you need to decide what you need to store, e.g. fields and normalise the way they are stored.
For example, you would have the following tables:
Users:
Id
..
EatItems:
UserId
ProductId
Calories
Fat
DateTime
Once you have these tables up and running, you can build reporting layer on top of that to breakdown consumption by user / date or anything else you might be interested in.
You could have a table that holds the input/calculated data/date which relates to a user/account.
When the user views previous day's, select the data that relates to that user.
I wouldn't create a table for each day. One table would suffice.
However, I would suggest attempting something and posting the code for specific issues you have if you run into before posting here.

An achievement system like here on stackoverflow in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Conceptually speaking how would one go about writing an achievement system for a website using PHP and MySQL?
Is the only real way of doing it is to constantly do MySQL queries to test for achievements and such?
You have two options, and most likely can combine them:
You can have a cron-job that runs every X-minutes, and runs over the database looking at each user and checks if they deserve a new badge.
For example, StackOverflow implements this for the Nice Answer badge. Everytime it runs, it checks how many Answers with +10 upvotes you have, and sees if you need to be awarded another badge. (It sees 5 posts with 10 upvotes and 4 Nice Answer Badges, you get a badge). Jeff has already stated that this means if you get an answer that gets a 10 vote, then downvoted, and then another post gets 10 votes, you won't get a badge.
The second option is event-based triggers. Which are really simply:
$badgeSystem->giveBadge("Some Badge Name", $User_ID);
This can be used for events you know are happening. Like the Autobiographer badge. Chances are the user won't fill out his profile unless the submit button is pressed, so the site could just check if the user has filled out the entire thing, and if they have, and they still need the badge, they get it.
The cron-job should be used for actions that are constantly checked. Things like quantitative goals like visiting the site for 150 days, or editing 500 times.
The event-based trigger should happen when an event will only happen if the user doesa specific action, such as submitting a form.
(You could probably use either for almost any situation. The event-based trigger gives quicker feedback though..)
Another option is to use the API from a gamification platform company such as http://iactionable.com
You might want to consider asking a similar question on meta, to see if Jeff is willing to share any of his lessons learned in this arena. If you do, the question should be specifically about the way SO approached the problem. This question, however, is a perfectly valid question for this site (since it is asking about created a new project, inspired by what was done on SO).
I suspect, though, that the SO team has a job that periodically runs a set of queries to look for new badges to award, and then awards them. This is why badges aren't awarded immediately after the action is taken to earn them.

Categories