super_cache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,33 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.rbc
19
+ *.sassc
20
+ .sass-cache
21
+ capybara-*.html
22
+ .rspec
23
+ /.bundle
24
+ /vendor/bundle
25
+ /log/*
26
+ /tmp/*
27
+ /db/*.sqlite3
28
+ /public/system/*
29
+ /coverage/
30
+ /spec/tmp/*
31
+ **.orig
32
+ rerun.txt
33
+ pickle-email-*.html
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in super_cache.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ShiningRay
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # SuperCache
2
+
3
+ SuperCache for rails is a caching middleware inspired by the "SuperCache" plugin
4
+ for WordPress.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'super_cache'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install super_cache
19
+
20
+ ## Usage
21
+
22
+ Just call `super_caches_page` inside your controller with the action names to be cached.
23
+
24
+ ```ruby
25
+ class MyController < ApplicationController
26
+ super_caches_page :index
27
+ def index
28
+ # action to be
29
+ edn
30
+ end
31
+ ```
32
+
33
+ Super_cache will store the response body into `Rails.cache`. The next time requesting
34
+ that action will get the same result.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module SuperCache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ require "super_cache/version"
2
+ require 'uri'
3
+ require 'fileutils'
4
+ # for static-caching the generated html pages
5
+
6
+ module SuperCache
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def super_caches_page(*pages)
13
+ return unless perform_caching
14
+ options = pages.extract_options!
15
+ options[:only] = (Array(options[:only]) + pages).flatten
16
+ before_filter :check_weird_cache, options
17
+ after_filter :weird_cache, options
18
+ end
19
+
20
+ def skip_super_caches_page(*pages)
21
+ options = pages.extract_options!
22
+ options[:only] = (Array(options[:only]) + pages).flatten
23
+ skip_before_filter :check_weird_cache, options
24
+ skip_after_filter :weird_cache, options
25
+ end
26
+ end
27
+
28
+ def check_weird_cache
29
+ return unless perform_caching
30
+ @cache_path ||= weird_cache_path
31
+
32
+ if content = Rails.cache.read(@cache_path, :raw => true)
33
+ return if content.size <= 1
34
+ logger.info "Hit #{@cache_path}"
35
+
36
+ headers['Content-Length'] ||= content.size.to_s
37
+ headers['Content-Type'] ||= request.format.to_s.strip unless request.format == :all
38
+ render :text => content, :content_type => 'text/html'
39
+ return false
40
+ end
41
+ rescue ArgumentError => e
42
+ @no_cache = true
43
+ logger.info e.to_s
44
+ logger.debug {e.backtrace}
45
+ end
46
+
47
+ def weird_cache
48
+ return if @no_cache
49
+ return unless perform_caching
50
+ return if request.format.to_sym == :mobile
51
+ @cache_path ||= weird_cache_path
52
+ @expires_in ||= 600
53
+ return if response.body.size <= 1
54
+ return if response.respond_to?(:status) and response.status.to_i != 200
55
+ #benchmark "Super Cached page: #{@cache_path}" do
56
+ # @cache_subject = Array(@cache_subject)
57
+ # @cache_subject.compact.flatten.select{|s|s.respond_to?(:append_cached_key)}.each do |subject|
58
+ # subject.append_cached_key @cache_path
59
+ # end
60
+ Rails.cache.write(@cache_path, response.body, :raw => true, :expires_in => @expires_in.to_i)
61
+ #end
62
+ end
63
+
64
+ protected :check_weird_cache
65
+ protected :weird_cache
66
+ private
67
+ def weird_cache_path
68
+ path = File.join request.host, request.path
69
+ q = request.query_string
70
+ request.format ||= :html
71
+ format = request.format.to_sym
72
+ path = "#{path}.#{format}" if format != :html and format != :all and params[:format].blank?
73
+ path = "#{path}?#{q}" #if !q.empty? && q =~ /=/
74
+ path
75
+ end
76
+ end
77
+
78
+ ActionController::Base.__send__ :include, SuperCache
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'super_cache/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "super_cache"
8
+ gem.version = SuperCache::VERSION
9
+ gem.authors = ["ShiningRay"]
10
+ gem.email = ["tsowly@hotmail.com"]
11
+ gem.description = %q{A simple caching middleware for rails}
12
+ gem.summary = %q{A simple caching middleware for rails}
13
+ gem.homepage = "https://github.com/shiningray/super_cache"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: super_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ShiningRay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple caching middleware for rails
15
+ email:
16
+ - tsowly@hotmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/super_cache.rb
27
+ - lib/super_cache/version.rb
28
+ - super_cache.gemspec
29
+ homepage: https://github.com/shiningray/super_cache
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: A simple caching middleware for rails
53
+ test_files: []