How can i impelement a Facebook N开发者_StackOverflow社区ewsFeed like feature in my PHP based community application? For example "X user uploaded a new photo, Z attending to a event". Fetching all this data from different tables consumes too resources.
Thanks.
There are a couple of different ways you can do this, but the one I would suggest is this. Keep a single 'history' table that holds all of the records for every user, a timestamp, and a description of the action along with flags (or predefined keys) for what type of action occurred. This would allow you to query a single table selectively for certain types of actions and within a set frame of time to display. However, with a lot of users this table will grow quickly so you may consider purging old data that you know you will not use again later. Pay close attention to indexes to keep the queries fast, and consider using query caching since you will probably be generating the same results frequently.
As long as you aren't planning on using this historical data for auditing users actions then it should be fine to dump it after a while. If you do need an audit trail then you may need to consider creating a temp_history table for showing recent actions and then in a CRON move their recent actions over to a permanent audit table for long term storage.
Start by defining what you call activity on your website. A activity can contain multiple hyper links. So a php multidimensional array might help in here which can go in the table as serialized object.
Say x is now connected with y, z, a and b. you can define y, z, a and b as profile links. Say x commented on y new photo. In this case y become a new hyper link to the person's profile photo. Say x became a fan of zoozoo group. In this case zoozoo becomes a hyper link to the fan page.
Create a activity table, to scale it large make it activity_USER_ID, every time a user registers you create this table and check that you don't have to create it once again.
Define Activities as in 1,2,3 or whatever you feel is the best activity that people might be interested in communities you support.
table structure should be simple : activity php array, timestamp. If you are using jQuery and Ajax to populate the activity like done in facebook with mouse scroll then you can always convert the php array to json and format it on the client side.
Do this whenever user logs in.
If the user base is large say a million then you might want to put in some job processing system which will put events in event_MM_DD_YYYY and you can then do a batch processing of those events to store it in the activity table.
Never display more than 10 20 30 recent activities.
Hope this helps for you :)
I think you've answered your own question: pull the data from a single table.
A simple approach would be that all your other components can talk to a central "event logger" which records something for the users news feed.
I liked Paul's solution (easy with certain frameworks like Symfony). If you want ot go further in terms of speed, you can use in addition a per-user caching system which will query for data and build new feeds on first request. Then retrieve the cached data on following requests. The event logger will clear the cache for concerned users when trigerred.
In facebook case, i think they use a 'push' ajax implementation to notify client-side there is news to show.
Xcache,Apc or filesystem based caching system can help you building such a system.
精彩评论