How should I cache Facebook call data into my database so rails can use it locally?
I want to start out by generating a model to hold this information:
rails g model UserData UID:string birthday:string likes:string location:string \n
activities:string books:string mov开发者_运维技巧ies:string music:string tv:string \n
interests:string post_count:string friend_count:string
Then I rake the DB.
Then I create the file $RAILS_ROOT/jobs/update_facebook.rb:
config = YAML::load(File.open("#{RAILS_ROOT}/config/facebook.yml"));
APP_ID = CONFIG['app_id']
fetching_app = FbGraph::Application.new(config['APP_ID']);
access_token = fetching_app.get_access_token(config['production']['client_secret']);
#Don't know what else to put in here
This form would be a bit redundanct... how would you know when to use database number of likes instead going to API to fetch the number of likes? you'd probably query the api way more than you would need, and querying API is bad because it's slow.
I haven't implemented this kind of caching in my app yet, but I understand the optimal way to do this is like:
you cache all API responses
you subscribe to the object you're tracking and implement the callback stuff from facebook real-time updates( http://developers.facebook.com/docs/reference/api/realtime/ )
with this, facebook will ping your app(in some way described in the link) when there's changes in the object
you drop the cache on the API call when facebook notifies the object change, so your app will only request the API when there's changes
that's the process, from what I understood... I'm sure implementation of facebook real-time stuff will make any app a tad faster
This one was simpler than I made it out to be. Just added columns to the User model with a migration:
rails g migration AddDetailsToUsers likes:string bookes:string etc.
Then just rake the migration, and add the following to the controller:
@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.likes = @graph.get_connections("me", "likes")
#Ditto for the other permissions
current_user.save
精彩评论