Tor
Tor

Reputation: 595

Rails 3, Caching object in one request does not last to the next request

I am writing an object to cache using

class Foo
  attr_accessor  :bar
end

foo = Foo.new
foo.bar = "123"

Rails.write "key", foo

When I call

Rails.read "key"

in the same request - the object is retrieved. However when I call it on an other request / other rails process I get nil.

Storing simple objects works (numbers, strings, array of numbers etc.)

Rails.cache data:

pry(main)> Rails.cache
=> #<ActiveSupport::Cache::DalliStore:0x007fa42906d5b8
 @data=
  #<Dalli::Client:0x007fa42906d478
   @options={:expires_in=>0},
   @ring=
    #<Dalli::Ring:0x007fa423bc0ad8
     @continuum=nil,
     @failover=true,
     @servers=
      [#<Dalli::Server:0x007fa423bc0c90
        @down_at=nil,
        @error=nil,
        @fail_count=0,
        @hostname="localhost",
        @last_down_at=nil,
        @lock=
         #<Monitor:0x007fa423bc0970
          @mon_count=0,
          @mon_mutex=#<Mutex:0x007fa423bc0858>,
          @mon_owner=nil>,
        @msg=nil,
        @options=
         {:down_retry_delay=>1,
          :socket_timeout=>0.5,
          :socket_max_failures=>2,
          :socket_failure_delay=>0.01,
          :value_max_bytes=>1048576,
          :username=>nil,
          :password=>nil,
          :async=>false,
          :expires_in=>0},
        @port=11211,
        @sock=#<Dalli::Server::KSocket:fd 11>,
        @version="1.4.13",
        @weight=1>]>,
   @servers=["localhost:11211"]>,

Upvotes: 0

Views: 323

Answers (1)

Tor
Tor

Reputation: 595

Well the solution to the mystery- In development env cache_classes is default to false. This fact prevents rails cache from marshaling the dumped objects since it doesn't know the class/module.

Possible bypass is to avoid low level caching in development env.

Upvotes: 1

Related Questions