Class: CGI::Session::FileStore
- Inherits:
-
Object
- Object
- CGI::Session::FileStore
- Defined in:
- lib/cgi/session.rb
Overview
File-based session storage class.
Implements session storage as a flat file of ‘key=value’ values. This storage type only works directly with String values; the user is responsible for converting other types to Strings when storing and from Strings when retrieving.
Instance Method Summary collapse
-
#close ⇒ Object
Update and close the session’s FileStore file.
-
#delete ⇒ Object
Close and delete the session’s FileStore file.
-
#initialize(session, option = {}) ⇒ FileStore
constructor
Create a new FileStore instance.
-
#restore ⇒ Object
Restore session state from the session’s FileStore file.
-
#update ⇒ Object
Save session state to the session’s FileStore file.
Constructor Details
#initialize(session, option = {}) ⇒ FileStore
Create a new FileStore instance.
This constructor is used internally by CGI::Session. The user does not generally need to call it directly.
session
is the session for which this instance is being created. The session id must only contain alphanumeric characters; automatically generated session ids observe this requirement.
option
is a hash of options for the initializer. The following options are recognised:
- tmpdir
-
the directory to use for storing the FileStore file. Defaults to Dir::tmpdir (generally “/tmp” on Unix systems).
- prefix
-
the prefix to add to the session id when generating the filename for this session’s FileStore file. Defaults to “cgi_sid_”.
- suffix
-
the prefix to add to the session id when generating the filename for this session’s FileStore file. Defaults to the empty string.
This session’s FileStore file will be created if it does not exist, or opened if it does.
416 417 418 419 |
# File 'lib/cgi/session.rb', line 416 def initialize(session, option={}) option = {'prefix' => 'cgi_sid_'}.update(option) @path, @hash = session.new_store_file(option) end |
Instance Method Details
#close ⇒ Object
Update and close the session’s FileStore file.
463 464 465 |
# File 'lib/cgi/session.rb', line 463 def close update end |
#delete ⇒ Object
Close and delete the session’s FileStore file.
468 469 470 471 472 |
# File 'lib/cgi/session.rb', line 468 def delete File::unlink @path+".lock" rescue nil File::unlink @path+".new" rescue nil File::unlink @path rescue nil end |
#restore ⇒ Object
Restore session state from the session’s FileStore file.
Returns the session state as a hash.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/cgi/session.rb', line 424 def restore unless @hash @hash = {} begin lockf = File.open(@path+".lock", "r") lockf.flock File::LOCK_SH f = File.open(@path, 'r') for line in f line.chomp! k, v = line.split('=',2) @hash[CGI.unescape(k)] = Marshal.restore(CGI.unescape(v)) end ensure f&.close lockf&.close end end @hash end |
#update ⇒ Object
Save session state to the session’s FileStore file.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/cgi/session.rb', line 445 def update return unless @hash begin lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600) lockf.flock File::LOCK_EX f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600) for k,v in @hash f.printf "%s=%s\n", CGI.escape(k), CGI.escape(String(Marshal.dump(v))) end f.close File.rename @path+".new", @path ensure f&.close lockf&.close end end |