From: Chris Date: 2002-11-08T00:10:46+09:00 Subject: Re: CGI::Session::FileStore question ----- Original Message ----- |Couldn't we use marshaling and get the actual objects back? This would also |make FileStore work in the same way that MemoryStore does, which seems like |a Good Thing. | |Is there a reason it wasn't done this way in the first place? Marshal may not be what you want before saving data. So do it by yourself, or wrap CGI::Session::FileStore to do marshaling. It is not too hard to define something like CGI::Session::PStore. Sufficient for your homework. matz. ---------------------------- My homework? You lost me there... Anyway, I don't really understand why Marshal might not be a good idea. Performance? Plus, it still seems like FileStore and MemoryStore should be interchangable, and they definitely aren't right now. In any case, I threw something together which seems to work pretty well. It was saving and loading Fixnums, booleans, Arrays of strings... much better than saving all of your session data in strings, IMHO. I also added a function to Session to get the hash object, because there are other Hash functions I would like to be able to use ('clear' and 'replace' in particular) on the session data, and because I wanted the program logic to be as separate as possible from the web interface. Passing a Hash reference seemed more orthogonal than passing a Session reference. (I might want to run this from other interfaces in the future.) So this is what I add to my web scripts: require 'cgi.rb' require 'cgi/session.rb' require 'html/xhtml1.rb' class CGI class Session def getHash if (!@data) @data = @dbman.restore end @data end class FileStore def restore unless @hash @hash = {} @f.flock File::LOCK_EX @f.rewind k = nil for line in @f line.chomp! if (!k) k = line else v = Marshal::load(CGI::unescape(line)) @hash[CGI::unescape(k)] = v k = nil end end end @hash end def update return unless @hash @f.rewind for k,v in @hash v = Marshal::dump(v) @f.printf "%s\n", CGI::escape(k) @f.printf "%s\n", CGI::escape(v) end @f.truncate @f.tell end end end end Thoughts? Chris