class Rack::State::Store::PreparedPostgres
Postgres
table-based state storage adapter using prepared statements. Because accessing state is a highly repetitive activity, prepared statements may offer some optimization.
See Postgres
for details.
Constants
- BINARY
Public Class Methods
new(connection, table = 'state')
click to toggle source
# File lib/rack/state.rb, line 353 def initialize(connection, table = 'state') @db = connection begin @db.prepare 'create_state', "INSERT INTO #{table} (token,object) VALUES ($1,$2)" @db.prepare 'read_state', "SELECT object FROM #{table} WHERE token=$1" @db.prepare 'update_state', "UPDATE #{table} SET object=$2, mtime=NOW() WHERE token=$1" @db.prepare 'delete_state', "DELETE FROM #{table} WHERE token=$1" rescue PG::DuplicatePstatement end end
Public Instance Methods
create(token, object)
click to toggle source
# File lib/rack/state.rb, line 368 def create(token, object) @db.exec_prepared('create_state', params(token, object)).clear rescue PG::UniqueViolation raise KeyError end
delete(token)
click to toggle source
# File lib/rack/state.rb, line 386 def delete(token) @db.exec_prepared('delete_state', params(token)).clear end
read(token)
click to toggle source
# File lib/rack/state.rb, line 374 def read(token) @db.exec_prepared('read_state', params(token), BINARY) do |result| ::Marshal.load result.getvalue(0,0) end rescue nil end
update(token, object)
click to toggle source
# File lib/rack/state.rb, line 380 def update(token, object) @db.exec_prepared('update_state', params(token, object)) do |result| raise KeyError if result.cmd_tuples == 0 end end
Private Instance Methods
params(token, obj = nil)
click to toggle source
# File lib/rack/state.rb, line 392 def params(token, obj = nil) obj ? [token, {value: ::Marshal.dump(obj), format:BINARY}] : [token] end