redis_page 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f4beebad0af5093a904e8018586f26331fdbf8c
4
+ data.tar.gz: b187937071618ac5b9a53a073c234de825d89e06
5
+ SHA512:
6
+ metadata.gz: 06d3caae4fe5ea15b205236db21815d1172122a175a52974a9359e00004cd6a4660f648411a01f4881f869344ef7b4e463d6b89ec2a558b24dab07b554b3648a
7
+ data.tar.gz: 2e61816f7303f0a0cad3fa1d1b7e435653354b9e2b254e86cb45ea94985bcd88541162f7b047eee806e2eac72e69d163f830aedf6bc9babad62e3332fdb8e12b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 saberma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # redis_page
2
+
3
+ [actionpack-page_caching](https://github.com/rails/actionpack-page_caching) 是将页面缓存到文件中,这样有两个缺点:
4
+
5
+ 1. nginx web 服务器需要访问到 app 服务器生成的文件;
6
+ 2. 多台 app 服务器都会生成自己的缓存文件,难以共享,NFS 等又不大稳定;
7
+
8
+ redis_page 改为将页面缓存至 redis,nginx 安装 redis 插件后即可直接使用。
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'redis_page'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ ### 1. Controller
25
+
26
+ 生成页面缓存
27
+
28
+ ```
29
+ class ProductController < ActionController::Base
30
+ caches_redis_page :show, :new
31
+
32
+ def show
33
+ @product = Product.find(params[:id])
34
+ @model_name = Product.table_name
35
+ @model_id = @product.id
36
+ end
37
+ end
38
+ ```
39
+
40
+ ### 2. View
41
+
42
+ 记录哪些实体更新时要刷新的 url,例如:iPhone 在首页中显示了,则记录下 iPhone 实体与首页的关联关系
43
+
44
+ ```
45
+ - Product.all.each do |product|
46
+ = @product.title
47
+ ```
48
+
49
+ 修改为:
50
+
51
+ ```
52
+ - Product.all.each do |product|
53
+ = c(@product).title
54
+ ```
55
+
56
+ c 方法会记录当前页面 url
57
+
58
+ ### 3. Model
59
+
60
+ 更新实体后刷新所有关联的页面缓存
61
+
62
+ ```
63
+ class Product < ActiveRecord::Base
64
+ include RedisPage::Sweeper
65
+ end
66
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "actionpack/redis/page/caching"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
2
+
3
+ module ActionController
4
+ module Caching
5
+ module Pages
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ end
10
+
11
+ module ClassMethods
12
+ def caches_redis_page(*actions)
13
+ options = actions.extract_options!
14
+
15
+ after_filter({only: actions}.merge(options)) do |c|
16
+ c.cache_page(response.body, request.path)
17
+ end
18
+ end
19
+ end
20
+
21
+ def cache_page(content, path)
22
+ Rails.logger.info "[page cache]caching: #{path}"
23
+ $redis.set(path, content)
24
+
25
+ if @model_name
26
+ mark_cache_instance(@model_name, @model_id)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ require 'action_controller/caching/pages'
2
+
3
+ module ActionController
4
+ module Caching
5
+ eager_autoload do
6
+ autoload :Pages
7
+ end
8
+
9
+ include Pages
10
+ end
11
+ end
12
+
13
+ ActionController::Base.send(:include, ActionController::Caching::Pages)
14
+ ActionController::Base.send(:include, RedisPage::ViewHelpers)
@@ -0,0 +1,21 @@
1
+ require 'redis_page/view_helpers'
2
+
3
+ module RedisPage
4
+ class Railtie < Rails::Railtie
5
+ initializer "redis_page" do
6
+ ActiveSupport.on_load(:action_controller) do
7
+ require 'action_controller/page_caching'
8
+ end
9
+ end
10
+
11
+ initializer "redis_page.activerecord" do
12
+ ActiveSupport.on_load(:active_record) do
13
+ require 'redis_page/sweeper'
14
+ end
15
+ end
16
+
17
+ initializer "redis_page.view_helpers" do
18
+ ActionView::Base.send :include, ViewHelpers
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'redis_page/sweeper_worker'
2
+
3
+ module RedisPage
4
+ module Sweeper
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ after_save :invalidate_instance_cache
9
+
10
+ def invalidate_instance_cache
11
+ $redis.smembers("i:#{self.class.table_name}:#{self.id}").each do |url|
12
+ Rails.logger.info "[page cache]add sweeper job: #{url}"
13
+ SweeperWorker.perform_async(url)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module RedisPage
2
+ class SweeperWorker
3
+ include Sidekiq::Worker
4
+
5
+ def perform(url)
6
+ uri = URI(url)
7
+ uri.port = 8081
8
+
9
+ auth = { username: "cache", password: "ewHN84JZLyRurX" }
10
+
11
+ Rails.logger.info "[page cache]sweeper fetching: #{url}"
12
+ response = HTTParty.get(uri, basic_auth: auth)
13
+ Rails.logger.debug "[page cache]sweeper response: #{response.body}"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module RedisPage
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ module RedisPage
2
+ module ViewHelpers
3
+
4
+ # 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
5
+ def c(object)
6
+ mark_cache_instance(object)
7
+ object
8
+ end
9
+
10
+ # 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
11
+ # @params
12
+ # 1. object 直接传递实体对象
13
+ # 2. name, id 或者传递实体表名及id
14
+ def mark_cache_instance(*array)
15
+ name, id = array
16
+ object = array.first
17
+ if id
18
+ name = name.downcase
19
+ else
20
+ name = object.class.table_name.downcase
21
+ id = object.id
22
+ end
23
+ $redis.sadd("i:#{name}:#{id}", request.url)
24
+ end
25
+
26
+ end
27
+ end
data/lib/redis_page.rb ADDED
@@ -0,0 +1 @@
1
+ require 'redis_page/railtie'
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_page/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_page"
8
+ spec.version = RedisPage::VERSION
9
+ spec.authors = ["saberma"]
10
+ spec.email = ["mahb45@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
14
+ end
15
+
16
+ spec.summary = %q{use redis to cache your rails page.}
17
+ spec.description = %q{use redis to cache your rails page.}
18
+ spec.homepage = "https://github.com/saberma/redis_page"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activesupport", "~> 4.0.9"
27
+ spec.add_dependency "actionpack", "~> 4.0.9"
28
+ spec.add_dependency "sidekiq", "~> 3.5.0"
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.8"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_page
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - saberma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.9
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.9
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.5.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: use redis to cache your rails page.
84
+ email:
85
+ - mahb45@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/action_controller/caching/pages.rb
100
+ - lib/action_controller/page_caching.rb
101
+ - lib/redis_page.rb
102
+ - lib/redis_page/railtie.rb
103
+ - lib/redis_page/sweeper.rb
104
+ - lib/redis_page/sweeper_worker.rb
105
+ - lib/redis_page/version.rb
106
+ - lib/redis_page/view_helpers.rb
107
+ - redis_page.gemspec
108
+ homepage: https://github.com/saberma/redis_page
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'' to prevent pushes to
113
+ rubygems.org, or delete to allow pushes to any server.'
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.1.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: use redis to cache your rails page.
134
+ test_files: []