TransWikia.com

Rails devise cache refreshed at every request

Stack Overflow Asked by sparkle on January 6, 2021

I am using Rails 6, Devise and Redis for caching.
If I run

redis-cli monitor

I see that Rails try to fetch a key and it refresh the cache. Even if I just reload the same page or I change webpage during the same user-logged session. Maybe is it because of utm and referrer field on the user model?
utm/referrer are filled at user signup and they don’t will never change.

This is an issue because I perform a "get"/"set" operation when I just need a "get".

1604411976.019840 [0 [::1]:50683] "get" "_session_id:2::252360dc050fb8d65e84a43a7a55da72ce1428fbdde4d9cda696296e3bdb130d"

1604411978.440996 [0 [::1]:50683] "set" "_session_id:2::252360dc050fb8d65e84a43a7a55da72ce1428fbdde4d9cda696296e3bdb130d"
"x04bo: ActiveSupport::Cache::Entryt:x0b@value{nI"x0fsession_idx06:x06ETI"%45f803f32b1a89b8d6b0fc38ff90b00cx06;aTI"x19warden.user.user.keyx06;aT[a[x06ix06I""$2a$11$6p01wAYCqmAGsjxzT40z8Ox06;aTI"butmx06;aT{n:x0bsource0:rcampaign0:x0bmedium0:x0ccontent0:tterm0I"x10_csrf_tokenx06;aTI"1SzDbADoyFrsa/DwWutlecf+7RiSURygsxiU5+XL/o5c=x06;aTI"rreferrerx06;aT"Ehttp://localhost:3000/en/profiles/34/items/35940:r@version0:x10@created_atfx161604411978.440767:x10@expires_in0

config/devise

config.skip_session_storage = [:http_auth]

app/models/user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable,  and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :trackable

Schema.yml

create_table "users", force: :cascade do |t|
        t.string "email", default: "", null: false
        t.string "encrypted_password", default: "", null: false
        t.string "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.string "locale"
        t.integer "sign_in_count"
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string "current_sign_in_ip"
        t.string "last_sign_in_ip"
        t.string "stripe_id"
        t.string "paypal"
        t.decimal "referral_commission"
        t.date "trial_end_date"
        t.string "coupon_code"
        t.json "utm"
        t.string "referrer"
        t.index ["email"], name: "index_users_on_email", unique: true
        t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
      end

config/application.rb

config.middleware.insert_after ActionDispatch::Static, Rack::Deflater
config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies # Required for all session management
config.middleware.use ActionDispatch::Session::CookieStore, config.session_options

The problem is not caused by the controller. There is no code.
app/controller/application_controller.rb

class ApplicationController < ActionController::Base
 # It happens even when I remove everything from here and page controller
end

class PagesController < ApplicationController
   def test
     render json: "ok"
   end
end

2 Answers

These writes are result of changes to session (data is Marshal.dumped).

Some bytes look to be broken in example, but it looks like you have referrer written to session (http://localhost:3000/en/profiles/34/items/35940 in example), which obviously change upon navigation and thus can result in writes.

Strange thing is that you have session_store = :cookie_store, but sessions are usually written to redis when cache_store is enabled

Answered by Vasfed on January 6, 2021

Seems like rails uses it's own field for expiration instead of the native setx for redis. If you notice, the last field from the active record cache is expires_in0

You have 3 pieces at play here. redis, devise and rails, gotta find which one has bad configs. Try:

  • passing a number in seconds for the expires_in facade you are using to cache
  • checking the session.rb and cache.rb for default expiration times
  • try to cache something else to see if it works

Answered by Magus on January 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP