1

オブジェクトをキャッシュするのに苦労しています。

tracked_point.rb

class TrackedPoint < ActiveRecord::Base
  attr_accessible :recorded_at, :worker_id, :worker, :x, :y, :z, :geom, :location_id, :location, :frequency
  attr_accessor :x, :y
  belongs_to :worker
  belongs_to :location, counter_cache: true

  has_many :infraction_locations
  has_many :infractions, through: :infraction_locations

  validates :location_id, presence: true

  set_rgeo_factory_for_column(:geom, FACTORY)

  after_save :check_infractions

  def check_infractions
    self.write_to_cache
    self.zones.each do |zone|
      zone.write_to_cache
      ZoneWorker.perform_async(zone.id, self.id)
    end
    PrivatePub.publish_to "/tracked_points/new", :tracked_point => {x: self.geom.x, y: self.geom.y, z: self.geom.z, worker_id: self.worker_id}
    Rails.cache.write([self.cached_worker, "cached_previous_tracked_point_id"], self.id)
  end

  def write_to_cache
    Rails.cache.write([self.class.name, id], self)
  end
end

tracked_pointが作成されると、コールcheck_infractionsバックが発生します (技術的after_saveには、それも実行したいからですupdate)。

console:

1.9.3-p392 :001 > worker = Worker.find 88
1.9.3-p392 :002 > worker.tracked_points.create(x:3, y:5, location_id: 15, recorded_at: Time.now)

これにより、エラーが発生します。

You are trying to cache a Ruby object which cannot be serialized to memcached.

これは、self.write_to_cacheが呼び出された行で発生しました。だから私は手動でそのコードを実行しようとしました:

1.9.3-p392 :007 > tp = worker.tracked_points.create(x:3, y: 3, location_id: 15, recorded_at: Time.now)
1.9.3-p392 :007 > tp.write_to_cache

同じエラー。を作成してみましょうTrackedPoint

1.9.3-p392 :008 > tp2= TrackedPoint.create(worker_id: 88, x: 5, y: 5, recorded_at: Time.now, location_id: 15)

同じエラー。

TrackedPointただし、データベースから最後のものを取得すると、それをキャッシュできます。

1.9.3-p392 :012 > tp = TrackedPoint.last
1.9.3-p392 :013 > tp.write_to_cache
  true

after_saveコールバックとオブジェクトの作成でキャッシュが失敗する理由はありますが、データベースからオブジェクトをプルするとキャッシュできますか?

  • ルビー 1.9.3-p392
  • レール3.2.13
  • ダリ2.6.4
4

0 に答える 0