Module: Lacquer::CacheUtils

Included in:
CacheControl
Defined in:
lib/lacquer/cache_utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/lacquer/cache_utils.rb', line 3

def self.included(base)
  base.class_eval do
    attr_reader :cache_ttl

    if respond_to? :before_filter
      before_filter :set_default_cache_ttl
      after_filter :send_cache_control_headers
    end
  end
end

Instance Method Details

#clear_cache_for(*paths) ⇒ Object

Sends url.purge command to varnish to clear cache.

clear_cache_for(root_path, blog_posts_path, ‘/other/content/*’)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lacquer/cache_utils.rb', line 28

def clear_cache_for(*paths)
  return unless Lacquer.configuration.enable_cache
  case Lacquer.configuration.job_backend
  when :delayed_job
    require 'lacquer/delayed_job_job'
    Delayed::Job.enqueue(Lacquer::DelayedJobJob.new(paths))
  when :resque
    require 'lacquer/resque_job'
    Resque.enqueue(Lacquer::ResqueJob, paths)
  when :sidekiq
    require 'lacquer/sidekiq_worker'
    Lacquer::SidekiqWorker.perform_async(paths)
  when :none
    Varnish.new.purge(*paths)
  end
end

#send_cache_control_headersObject

Sends cache control headers with page. These are the headers that varnish responds to to set cache properly.



48
49
50
51
52
# File 'lib/lacquer/cache_utils.rb', line 48

def send_cache_control_headers
  if Lacquer.configuration.enable_cache && @cache_ttl && @cache_ttl != 0
    expires_in(@cache_ttl, :public => true)
  end
end

#set_cache_ttl(ttl) ⇒ Object

Instance variable for the action ttl.



15
16
17
# File 'lib/lacquer/cache_utils.rb', line 15

def set_cache_ttl(ttl)
  @cache_ttl = ttl
end

#set_default_cache_ttlObject

Called as a before filter to set default ttl for the entire application.



21
22
23
# File 'lib/lacquer/cache_utils.rb', line 21

def set_default_cache_ttl
  set_cache_ttl(Lacquer.configuration.default_ttl)
end