跳至内容 跳至搜索

Action Dispatch Session CacheStore

一个会话存储,使用 ActiveSupport::Cache::Store 来存储会话。此存储最有用,如果你不将关键数据存储在会话中,并且不需要它们长时间存在。

选项

  • cache - 要使用的缓存。如果未指定,将使用 Rails.cache

  • expire_after - 会话在自动过期之前存储的时间长度。默认情况下,使用缓存的 :expires_in 选项。

方法
D
F
N
W

类公共方法

new(app, options = {})

# File actionpack/lib/action_dispatch/middleware/session/cache_store.rb, line 18
def initialize(app, options = {})
  @cache = options[:cache] || Rails.cache
  options[:expire_after] ||= @cache.options[:expires_in]
  super
end

实例公共方法

delete_session(env, sid, options)

从缓存中删除会话。

# File actionpack/lib/action_dispatch/middleware/session/cache_store.rb, line 44
def delete_session(env, sid, options)
  @cache.delete(cache_key(sid.private_id))
  @cache.delete(cache_key(sid.public_id))
  generate_sid
end

find_session(env, sid)

从缓存中获取会话。

# File actionpack/lib/action_dispatch/middleware/session/cache_store.rb, line 25
def find_session(env, sid)
  unless sid && (session = get_session_with_fallback(sid))
    sid, session = generate_sid, {}
  end
  [sid, session]
end

write_session(env, sid, session, options)

在缓存中设置会话。

# File actionpack/lib/action_dispatch/middleware/session/cache_store.rb, line 33
def write_session(env, sid, session, options)
  key = cache_key(sid.private_id)
  if session
    @cache.write(key, session, expires_in: options[:expire_after])
  else
    @cache.delete(key)
  end
  sid
end