Class: CacheHttpDiskDevice

Inherits:
CasheDevice::CacheDeviceBase show all
Defined in:
lib/cache.rb

Instance Attribute Summary

Attributes inherited from CasheDevice::CacheDeviceBase

#cacheDuration, #cacheMax

Instance Method Summary collapse

Methods inherited from CasheDevice::CacheDeviceBase

#read, read

Constructor Details

#initialize(cacheDuration = 12*60, cacheMax = 50) ⇒ CacheHttpDiskDevice

Returns a new instance of CacheHttpDiskDevice.



84
85
86
87
88
# File 'lib/cache.rb', line 84

def initialize(cacheDuration = 12*60, cacheMax=50)
    super(cacheDuration, cacheMax)
    @tmpdir = Dir.tmpdir + '/bbc_cache'
    FileUtils.mkdir_p(@tmpdir)
end

Instance Method Details

#directRead(url) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cache.rb', line 98

def directRead(url)
    $log.misc { "directRead(): " + self.class.name }
    tmpfname = tempFileName(url)

    if File.exist?(tmpfname) then
        $log.misc { "File ctime  : " + File.ctime(tmpfname).to_s}
        $log.misc { "expire time : " + (File.ctime(tmpfname) + @cacheDuration).to_s }
        $log.misc { "Now Time    : " + Time.now.to_s }
    end

    if File.exist?(tmpfname) and
            File.ctime(tmpfname) + @cacheDuration > Time.now then
        data = IO.read(tmpfname)
    else
        data = BBCNet.read(url)
        open(tmpfname, "w") do |f| f.write(data) end
    end
    [ data, tmpfname ]
end

#restoreCache(key) ⇒ Object

Returns : data restore data from key.

Returns:

  • : data restore data from key.



92
93
94
# File 'lib/cache.rb', line 92

def restoreCache(key)
    IO.read(key)
end

#tempFileName(url) ⇒ Object



118
119
120
# File 'lib/cache.rb', line 118

def tempFileName(url)
    File.join(@tmpdir, url.scan(%r{(?:iplayer/)[\w\/]+$}).first.gsub!(/iplayer\//,'').gsub!(%r|/|, '_'))
end