Class: PageCache

Inherits:
Object
  • Object
show all
Defined in:
app/models/page_cache.rb

Constant Summary collapse

CACHE_PATH =
File.join Rails.root, "public/cache"
EXTENSIONS =
["html", "json", "rss"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PageCache

Returns a new instance of PageCache.



7
8
9
# File 'app/models/page_cache.rb', line 7

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'app/models/page_cache.rb', line 2

def name
  @name
end

Class Method Details

.allObject



75
76
77
78
79
# File 'app/models/page_cache.rb', line 75

def all
  cache_names.map do |name|
    PageCache.new name
  end
end

.cache_files(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/page_cache.rb', line 81

def cache_files(options = {})
  globs = EXTENSIONS.map do |extension|
    globber = "**/*.#{extension}"
    globber += "*" if options[:with_gz]
    File.join CACHE_PATH, globber
  end

  Dir.glob(globs, File::FNM_DOTMATCH).map do |file|
    file.sub "#{CACHE_PATH}/", ""
  end
end

.cache_namesObject



93
94
95
96
97
# File 'app/models/page_cache.rb', line 93

def cache_names
  cache_files.map do |file|
    file.sub /\.(?:www)(?:\.tmp)?\.(?:#{EXTENSIONS.join "|"})$/, ""
  end.sort.uniq
end

.cleanup_tmp!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/page_cache.rb', line 51

def cleanup_tmp!
  globs = []

  EXTENSIONS.each do |extension|
    globs << File.join(CACHE_PATH, "**/*.tmp.#{extension}")
    globs << File.join(CACHE_PATH, "**/*.tmp.#{extension}.gz")
  end

  globs.each do |glob|
    Dir.glob(glob, File::FNM_DOTMATCH).each do |file|
      if 2.hours.ago > File.mtime(file)
        File.delete file
      end
    end
  end
end

.expire_all!Object



112
113
114
115
116
# File 'app/models/page_cache.rb', line 112

def expire_all!
  EXTENSIONS.each do |extension|
    File.delete *Dir.glob(File.join(CACHE_PATH, "**/*.#{extension}*"), File::FNM_DOTMATCH)
  end
end

.expire_tmp!Object



106
107
108
109
110
# File 'app/models/page_cache.rb', line 106

def expire_tmp!
  EXTENSIONS.each do |extension|
    File.delete *Dir.glob(File.join(CACHE_PATH, "**/*.tmp.#{extension}*"), File::FNM_DOTMATCH)
  end
end

.expire_www!Object



99
100
101
102
103
104
# File 'app/models/page_cache.rb', line 99

def expire_www!
  EXTENSIONS.each do |extension|
    File.delete *Dir.glob(File.join(CACHE_PATH, "**/*.www.#{extension}*"), File::FNM_DOTMATCH)
    File.delete *Dir.glob(File.join(CACHE_PATH, "**/*.www.tmp.#{extension}*"), File::FNM_DOTMATCH)
  end
end

.find(name) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


68
69
70
71
72
73
# File 'app/models/page_cache.rb', line 68

def find(name)
  name = "" if name == "INDEX"
  actual = cache_names.select { |x| x == name }.first
  raise ActiveRecord::RecordNotFound.new("No records found!") unless actual
  PageCache.new actual
end

Instance Method Details

#delete_nameObject



19
20
21
22
23
24
25
# File 'app/models/page_cache.rb', line 19

def delete_name
  if name == ""
    "INDEX"
  else
    name
  end
end

#display_nameObject



11
12
13
14
15
16
17
# File 'app/models/page_cache.rb', line 11

def display_name
  if name == ""
    "INDEX"
  else
    name
  end
end

#expire!Object



41
42
43
44
45
46
47
48
# File 'app/models/page_cache.rb', line 41

def expire!
  PageCache.cache_files(:with_gz => true).select do |file|
    extracted_name = file.sub /\.(?:www)(?:\.tmp)?\.(?:#{EXTENSIONS.join "|"})(?:.gz)?$/, ""
    extracted_name == name
  end.each do |file|
    File.delete File.join(CACHE_PATH, file)
  end
end

#www?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'app/models/page_cache.rb', line 27

def www?
  return @www_exists unless @www_exists.nil?
  @www_exists = EXTENSIONS.any? do |extension|
    File.exists? File.join(CACHE_PATH, "#{name}.www.#{extension}")
  end
end

#www_tmp?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'app/models/page_cache.rb', line 34

def www_tmp?
  return @www_tmp_exists unless @www_tmp_exists.nil?
  @www_tmp_exists = EXTENSIONS.any? do |extension|
    File.exists? File.join(CACHE_PATH, "#{name}.www.tmp.#{extension}")
  end
end