4
4

More than 5 years have passed since last update.

Facebookユーザ情報をRedisに保存してAPIリクエスト回数を減らす

Posted at

Facebook Graph APIにはFacebook側で更新があるとこちらのサーバにPOSTで更新情報を通知してくれるrealtime-updateという仕組みが用意されている。RedisにFacebook uidをkeyとして情報を保存し、使い回す処理を書いた。

app/helpers/application_helper.rb
  def fb_user
    begin
      cached_data = $redis.get(current_user.uid)
      return Hashie::Mash.new(JSON.parse(cached_data))
    rescue
      me              = FbGraph::User.me(decrypted_token)
      fb_user         = me.fetch
      $redis.set fb_uid, fb_user.to_json  rescue logger.error "[Redis] set #{fb_uid} failed"
      return fb_user
    end
  rescue => e
    nil
  end
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4