Class: Spider::FileSession

Inherits:
Session show all
Defined in:
lib/spiderfw/controller/session/file_session.rb

Instance Attribute Summary

Attributes inherited from Session

#sid

Class Method Summary collapse

Methods inherited from Session

#[], #[]=, check_purge, #clear_empty_hashes!, #delete, #flash, #generate_sid, get, #initialize, #persist, #restore, #transient

Constructor Details

This class inherits a constructor from Spider::Session

Class Method Details

.[](sid) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spiderfw/controller/session/file_session.rb', line 29

def [](sid)
    check_purge
    dir = Spider.conf.get('session.file.path')
    path = "#{dir}/#{sid}"
    data = nil
    if (File.exist?(path))
        @sync.lock(Sync::SH)
        f = File.new(path, 'rb+')
        f.flock(File::LOCK_SH)
        begin
            data = Marshal.restore(f.read)
        rescue => exc
            Spider::Logger.error("Corrupt session: #{exc.message}")
            data = {}
        end
        mtime = f.mtime
        f.flock(File::LOCK_UN)
        f.close
        @sync.lock(Sync::UN)
    end
    return data
end

.[]=(sid, data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spiderfw/controller/session/file_session.rb', line 14

def []=(sid, data)
    dir = Spider.conf.get('session.file.path')
    FileUtils.mkpath(dir)
    path = "#{dir}/#{sid}"
    
    @sync.lock(Sync::EX)
    f = File.new(path, 'wb+')
    f.flock(File::LOCK_EX)
    f.puts(Marshal.dump(data))
    f.flush
    f.flock(File::LOCK_UN)
    f.close
    @sync.lock(Sync::UN)
end

.delete(sid) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spiderfw/controller/session/file_session.rb', line 62

def delete(sid)
    dir = Spider.conf.get('session.file.path')
    return unless File.exist?(dir+'/'+sid)
    @sync.lock(Sync::EX)
    f = File.new(dir+'/'+sid)
    f.flock(File::LOCK_EX)
    File.unlink(dir+'/'+sid)
    f.flock(File::LOCK_UN)
    f.close
    @sync.lock(Sync::UN)
end

.purge(life) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/spiderfw/controller/session/file_session.rb', line 52

def purge(life)
    dir = Spider.conf.get('session.file.path')
    @sync.lock(Sync::EX)
    Find.find(dir) do |path|
        next unless File.file?(path)
        File.unlink(path) if File.exist?(path) && (File.mtime(path) + life < Time.now)
    end
    @sync.lock(Sync::UN)
end

.setupObject



10
11
12
# File 'lib/spiderfw/controller/session/file_session.rb', line 10

def setup
    @sync ||= Sync.new
end