I'm making a website that lets you share your music tastes, it works with a desktop 开发者_运维知识库client that monitors your media player (iTunes, whatever), and sends the details of the song that is currently playing to the site, where it is associated with your profile.
This output is displayed on your profile, and the idea is the page mirrors your local music player, right down the the position of a progress bar.
However I can't decide how to store the data. As soon as the song is over, it becomes useless, so something permanent like a MySQL database seems pointless.
Session variables don't work, because the script that does the work is not directing you to the page that displays the output. The idea is that the backend script just gets the info as it arrives and saves it somewhere, and you can get the latest info whenever you load the profile page.
I'm currently using a textfile, as a bad and temporary solution, just writing a new line each time and then reading it.
What is there between session variables and SQL?
And how can I get the output page to update every time there is new data? I need a way of storing it that allows events to be triggered, so checking it mustn't be too intensive.
I think a (My)SQL database would be alright, especially if you want to show the last few songs somebody played.
But if you just need a temporary data store to hold a single song per user, you could use a key-value store like memcached. It stores data directly in memory instead of a file or database. The key would be something unique (like the user's name or id) and the value would be data about the currently playing song.
You should look into memcached with a low cache expiry on keys of this type.
I would definitely store this in a database with a timestamp. For your display query, all you have to do is select the most recent (unless it has been more than a certain amount of time, in case the player is disconnected).
It might be best to hang on to this data. You may want it later to show which songs they play most or something. Otherwise, you can always delete the old records. I suspect keeping them will be worthwhile later. (Also consider the privacy implications of this. Make sure you tell your users the kind of data you keep in your privacy policy.)
Well I think you could use SQL, just have a table that stores the UserId, "current/last" song field, "currentlyPlaying" (bool), and you keep writting on top of that field everytime there is a new song, so that way you only have one row for each user all the time. It is still permanent, but you only keep track of the last song.
As a sidenote, you might want to save in a more permanently way all the songs a user plays.. if it is supposed to be a music tastes sharing site, having a history of played songs would let you make some nice algorithms like recommendations, etc.
精彩评论