Module: WikiCacheStore

Included in:
Wiki
Defined in:
lib/soks-storage.rb

Constant Summary collapse

CACHE_EXTENSION =
".marshal"

Instance Method Summary collapse

Instance Method Details

#cache_filename_for(name) ⇒ Object



40
41
42
# File 'lib/soks-storage.rb', line 40

def cache_filename_for( name )
  File.join( @cache_folder, "#{name}#{CACHE_EXTENSION}")
end

#load_cache(cache_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/soks-storage.rb', line 5

def load_cache( cache_name )
  return nil unless @cache_folder
  
  cache = nil
  
  File.open( cache_filename_for( cache_name ) ) do |f|
    cache = Marshal.load(f)
  end
  
  File.delete( cache_filename_for( cache_name ) )
  
  $LOG.info "Loaded #{cache_name} cache"
  
  return cache
  
  rescue ArgumentError
    $LOG.warn "#{cache_name} cache corrupt (bad characters in file)"
    return nil 
  
  rescue EOFError
    $LOG.warn "#{cache_name} cache corrupt (unexpected end of file)"
    return nil 
    
  rescue Errno::ENOENT
    $LOG.warn "#{cache_name} cache not found"
    return nil
end

#save_cache(cache_name, cache_object) ⇒ Object



33
34
35
36
37
38
# File 'lib/soks-storage.rb', line 33

def save_cache( cache_name, cache_object )
  return nil unless @cache_folder
  File.open( cache_filename_for( cache_name ), 'w' ) do |f|
    f.puts Marshal.dump(cache_object)
  end
end