gpt4 book ai didi

session - 基于 Sinatra 数据库的 session

转载 作者:行者123 更新时间:2023-12-02 06:18:53 25 4
gpt4 key购买 nike

我目前正在将 Sinatra 与 Heroku 结合使用,唯一的同步是数据库。因此,我需要存储在数据库中的 session (希望不使用 ActiveRecord)。

是否有 Rack 中间件或其他东西可以做到这一点?

最佳答案

您可以使用 Moneta gem作为 Sinatra 的替代 session 管理器。

直接来自他们的 GitHub 页面:

Moneta provides a standard interface for interacting with various kinds of key/value stores...

将 Moneta 设置为 Rack session 存储:

# in your config.ru
require 'rack/session/moneta'

# Use only the adapter name
use Rack::Session::Moneta, :store => :Redis

# Use Moneta.new
use Rack::Session::Moneta, :store => Moneta.new(:Memory, :expires => true)

# Use the Moneta builder
use Rack::Session::Moneta do
use :Expires
adapter :Memory
end

它几乎适用于您能想到的任何东西:基于文件和内存存储、ActiveRecord、Sequel、DataMapper、Memcached、REDIS、CouchDB、MongoDB 等等。

更新

为了详细说明与您的 Rack 应用程序的集成,这就是我的 config.ru 在 Heroku 上运行并使用 Redis Cloud 的生产站点上的设置方式。附加组件:

if ENV['RACK_ENV'].to_sym == :production
use Rack::Session::Moneta,
key: 'domain.name',
domain: '.example.com',
path: '/',
expire_after: 7*24*60*60, # one week
secret: ENV['PRODUCTION_SESSION_SECRET_KEY'],

store: Moneta.new(:Redis, {
url: ENV['REDISCLOUD_URL'],
expires: true,
threadsafe: true
})
else
use Rack::Session::Moneta,
key: 'domain.name',
domain: '*',
path: '/',
expire_after: 30*24*60*60, # one month
secret: ENV['DEV_SESSION_SECRET_KEY'],

store: Moneta.new(:Redis, {
url: ENV['DEV_REDIS_URL'],
expires: true,
threadsafe: true
})
end

关于session - 基于 Sinatra 数据库的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16878571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com