Sha256: 88bd8c0d83d9d344ca6bb1a3e85ebf18d3fd8147b0090d59459f9e6f426d76fd

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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("server")}:#{config("port")}",
                                        :timeout => config("timeout"),
                                        :keep_alive => config("keep_alive")
    end
    
    # purge a regular expression of url
    def purge path, recursive=false, hosts=nil
      
      # fall back to configuration and convert to array
      hosts ||= config('hosts').delete(' ').split(',')
      
      # purge any page starting with regex
      regex = "^#{path}#{'$' unless recursive}"
      
      # purge with 
      client.purge "req.http.host ~ ^(#{hosts.join('|')}) && 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

1 entries across 1 versions & 1 rubygems

Version Path
shellac-0.0.3.rc2 lib/shellac/cache.rb