使用1900年之前的日期缓存模型时的编组错误

我有一些具有“published_on”属性的活动记录模型。 当我尝试使用在1/1/1900之前的published_on日期缓存模型时,我收到如下错误:

Marshalling error for key 'popular_products': year too big to marshal: 1300 UTC You are trying to cache a Ruby object which cannot be serialized to memcached. 

我可以用ruby重现类似的错误:

 irb(main)> Marshal.dump( Time.parse("1/1/1900") ) ArgumentError: year too big to marshal: 1899 UTC 

使用1900年之前的日期缓存模型的正确方法是什么?

正如@ j03w评论的那样,这是时间类型的限制。 我选择将类型更改为Date,从而解决了问题。

您还可以重载Time类的_dump和_load方法,如下所示:

 require 'time' # Time extension to have xmlschema method class Time def _dump(level) xmlschema(9) # store nsec end def self._load(str) Time.parse(str) end end irb(main):013:0> t=Time.new(1515,1,1) => 1515-01-01 00:00:00 +0100 irb(main):014:0> a=Marshal.dump(t) => "\x04\bIu:\tTime(1515-01-01T00:00:00.000000000+01:00\x06:\x06ET" irb(main):015:0> t==Marshal.load(a) => true