TypeError 单例无法转储

问题描述 投票:0回答:1

我正在开发一个 Rails 4 项目,并使用 readthis_store gem 来进行缓存。我确实有以下代码

我有一个带有方法的用户模型

block_reason

class User < ActiveRecord::Base

  def block_reason
    Rails.cache.fetch([self.cache_key, __method__], expires_in: 1.hours) do
      {
          blocked: true,
          blocked_reason: reason,
          blocked_reason_text: reason_text,
          application_blocks: {
            status: self..status,
            name_check: self.name_check,
            id_check: self.id_check,
          }
       }
    end
  end
end

在我的控制器中,我有以下方法索引,它调用 block_reason 方法

class Private::V1::JobsController < Private::V1::ApiController
  def index
    response = @current_user.block_reason.try(:to_h)

    json_success(nil, response)
  end
end

但是当我进行 APi 调用时,我收到错误

TypeError: singleton can't be dumped
  from readthis/entity.rb:50:in `dump'
  from readthis/entity.rb:50:in `dump'
  from readthis/cache.rb:315:in `write_entity'
  from readthis/cache.rb:86:in `block in write'
  from connection_pool.rb:63:in `block (2 levels) in with'
  from connection_pool.rb:62:in `handle_interrupt'
  from connection_pool.rb:62:in `block in with'
  from connection_pool.rb:59:in `handle_interrupt'
  from connection_pool.rb:59:in `with'
  from readthis/cache.rb:346:in `block in invoke'
  from readthis/cache.rb:338:in `block in instrument'
  from active_support/notifications.rb:166:in `instrument'
  from readthis/cache.rb:338:in `instrument'
  from readthis/cache.rb:345:in `invoke'
  from readthis/cache.rb:85:in `write'
  from readthis/cache.rb:146:in `fetch'

我知道错误发生在 readthis_store gem 中,并且与 Marshal.dump 定义缓存为单例的对象有关。

但我不知道如何解决这个问题。我尝试了各种选择,但都无济于事。

不确定是否有人遇到过这个问题并能够在 Rails 4 上解决它。

ruby ruby-on-rails-4 caching redis marshalling
1个回答
0
投票

检查所有被缓存的内容实际上都是可序列化的。我从你的代码中看到的是你有两个点:

...
  status: self..status
...
© www.soinside.com 2019 - 2024. All rights reserved.