简体   繁体   English

使用户数据库与外部邮件列表服务保持同步的最有效方法是什么?

[英]What's the most efficient way to keep a user database in sync with an external mailing list service?

I'd like some advice on how I should synchronize a list of email addresses on 11k users against an external mailing list program, in this case Mailchimp. 我想要一些有关如何将11k用户的电子邮件地址列表与外部邮件列表程序(在本例中为Mailchimp)进行同步的建议。

Normally the way I'd do this is simply to have an :after_save callback, to send a single update to the external api. 通常,我这样做的方法只是有一个:after_save回调,以将单个更新发送到外部api。

But already each hour, a rake task is run to update a property on every user in the database. 但是已经每小时执行一次rake任务,以更新数据库中每个用户的属性。 If I simply did that, every hour, the the poor mailchimp API would get be hit 11,000 times. 如果我只是简单地这样做的话,可怜的mailchimp API每小时就会被攻击11,000次。

What's the most efficient, simple way to do this, to check only if a single attribute you're watching has changed from what it was before the save? 仅检查您正在观看的单个属性是否与保存之前的属性相比发生变化,最有效,最简单的方法是什么?

If there's a variable that persists across the transaction lifecycle I would simply do something like this, where I check if the value has changed, and if it's different execute come other code. 如果有一个变量在整个事务生命周期中都存在,那么我会做类似的事情,在其中检查值是否已更改,以及是否不同,请执行其他代码。

class User

  :before_save :store_old_email

  :after_save :sync_with_chimp


  def store_old_email
    $ugly_of_global_variable_to_store_email = user.email
  end

  :sync_with_chimp 
    if $ugly_of_global_variable_to_store_email != user.email
      //update_mail_chimp_api
    end
  end

end

I've checked the rails api here, and I'm still slightly unclear on how I should be doing this. 我已经在这里检查了rails api,但是我仍然不清楚应该如何执行此操作。

Would you use the dirty? 你会用脏的吗? class here to do this? 在这里上课吗?

This is the way I went with in the end. 这就是我最后的选择。

It turns out Rails gives you loads of handy callbacks in the dirty to do this. 事实证明,Rails在肮脏的环境中为您提供了方便的回调方法。

Any suggestions on how to make this code less repetitive wold be gratefully received. 我们非常感谢您提供有关如何减少此代码重复性的建议。

def update_mailchimp(optin)
  # Create a Hominid object (A wrapper to the mailchimp api), and pass in a hash from the yaml file 
  # telling which mailing list id to update with subscribe/unsubscribe notifications)
  @hominid = Hominid.new
  client_site_list_id = YAML.load(File.read(RAILS_ROOT + "/config/mailchimp.yml"))

  case optin  
    when 'subscribe_newsletter'
      logger.debug("subscribing to newsletter...")
      "success!" if @hominid.subscribe(client_site_list_id['client_site_to_mailchimp_API_link'], email, {:FNAME => first_name, :LNAME => last_name}, 'html')
    when 'unsubscribe_newsletter'
      logger.debug("unsubscribing from newsletter...")
      "success!" if @hominid.subscribe(client_site_list_id['client_site_to_mailchimp_API_link'], email, {:FNAME => first_name, :LNAME => last_name}, 'html')
    when 'subscribe_monthly_update'
      logger.debug("subscribing to monthly update...")
      "success!" if @hominid.subscribe(client_site_list_id['monthly_update'], email, {:FNAME => first_name, :LNAME => last_name}, 'html')
    when 'unsubscribe_monthly_update'
      logger.debug("unsubscribing from monthly update...")
      "success!" if @hominid.unsubscribe(client_site_list_id['monthly_update'], email, {:FNAME => first_name, :LNAME => last_name}, 'html')
    end
end

# Keep the users in sync with mailchimp's own records - by only firing requests to the API if details on a user have changed after saving.

def check_against_mailchimp
  logger.info("Checking if changes need to be sent to mailchimp...")
  if newsletter_changed?
    logger.info("Newsletter changed...")
    newsletter ? update_mailchimp('subscribe_newsletter') : update_mailchimp('unsubscribe_newsletter')
  end
  if monthly_update_changed?
    logger.info("update preferences changed...")
    monthly_update ? update_mailchimp('subscribe_monthly_update') : update_mailchimp('unsubscribe_monthly_update')
  end
end

you could change your users model to an active resource instead of active record and just use mailchimps api as your db for users 您可以将用户模型更改为活动资源而不是活动记录,而仅将mailchimps api用作用户的数据库

this is an older post about active resource but might get you started down the right path 这是有关活动资源的较旧文章,但可能会让您正确地开始

http://www.therailsway.com/2007/9/3/using-activeresource-to-consume-web-services http://www.therailsway.com/2007/9/3/using-activeresource-to-consume-web-services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PostgreSQL数据库中定期同步数据的最有效方法是什么? - What's the most efficient way to periodically sync data in a PostgreSQL DB? 跟踪累计终生销售数字的最有效方法是什么? - What's the most efficient way to keep track of an accumulated lifetime sales figure? 将外部数据(tsv或json)提供给Ruby on Rails的最有效方法是什么? - What's the most efficient way to serve external data (tsv or json) to Ruby on Rails? 测试邮件列表软件的最佳方法是什么? - What is the best way to test mailing list software? 查询集合父模型的最有效方法是什么? - What's the most efficient way of querying a collection parent models? 在Ruby中深度复制对象的最有效方法是什么? - What's the most efficient way to deep copy an object in Ruby? 删除字符串的第一个字符的最有效方法是什么? - What's the most efficient way to remove first character of a string? Rails 视图中的查询 - 重复查询的最有效方法是什么? - Queries in Rails views - what's the most efficient way to repeat one? 模型的信息与外部来源保持同步 - model's information keep in sync with external source 存储和访问图像的最有效方法是什么 - What is the most efficient way to store and access images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM