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



58
59
60
61
62
# File 'app/models/page_cache.rb', line 58

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

.cache_files(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/page_cache.rb', line 64

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



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

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

.expire_all!Object



95
96
97
98
99
# File 'app/models/page_cache.rb', line 95

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

.expire_tmp!Object



89
90
91
92
93
# File 'app/models/page_cache.rb', line 89

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



82
83
84
85
86
87
# File 'app/models/page_cache.rb', line 82

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)


51
52
53
54
55
56
# File 'app/models/page_cache.rb', line 51

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