Sha256: 766d4586c9448ca15fc44116422ccde0ff28d5cd83dbc57d8ccff9132105dacd

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'singleton'

module Shellac
  class Cache
    include Singleton
    
    if File.exist?("#{File.expand_path(Rails.root.to_s)}/config/varnish.yml")
      VARNISH_CONFIGURATION = YAML.load_file("#{Rails.root.to_s}/config/varnish.yml")[::Rails.env]
    end
    
    # return client instance
    def client
      @client ||= Varnish::Client.new   "#{config("host")}:#{config("port")}",
                                        :timeout => config("timeout"),
                                        :keep_alive => config("keep_alive")
    end
    
    # purge a regular expression of url
    def purge path, recursive
      
      if recursive
        regex = "^#{path}" # purge any page starting with regex
      else
        regex = "^#{path}$"
      end
      
      # write to log
      Rails.logger.info "*** Purging#{" (recursively)" if recursive}: #{path}"
      
      # purge with 
      client.purge "req.http.host ~ ^#{config("host")} && req.url ~ #{regex}"
    end
    
    protected
    
      def config attr
        begin
          VARNISH_CONFIGURATION[attr]
        rescue
          raise "Shellac was not properly configured for #{::Rails.env} environment. Did you run rails generate shellac:install?"
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shellac-0.0.2.4 lib/shellac/cache.rb
shellac-0.0.2.3 lib/shellac/cache.rb