redis_page 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +21 -3
- data/lib/action_controller/caching/pages.rb +19 -4
- data/lib/redis_page/sweeper.rb +4 -3
- data/lib/redis_page/sweeper_worker.rb +7 -5
- data/lib/redis_page/version.rb +1 -1
- data/lib/redis_page/view_helpers.rb +3 -2
- data/lib/redis_page.rb +22 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df9e7a7ba2a4887f6e5c54764f503d3f887191bc
|
4
|
+
data.tar.gz: db82203f0bc3a2c6a2e6f49700c631a044cb8cdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf9aad1b991ba023712c82b448bcce3725a987cec94d900f80cd447e3b7e19c1154eb4533ce733588e6139d76c8b957e1b6efa3de69c074b207087cd79d884cb
|
7
|
+
data.tar.gz: cd18e97b69ea34dd509ca7bb607b4bbbe356e48971a325545dcf9346f770c34954e8da17a2a166279c282fed27cbb63c7fd434c1ca9663cd8e34e55bfa296023
|
data/README.md
CHANGED
@@ -21,18 +21,29 @@ And then execute:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
+
### 0. Config
|
25
|
+
|
26
|
+
增加文件:`config/initializers/redis_page.rb`
|
27
|
+
|
28
|
+
```
|
29
|
+
require "redis"
|
30
|
+
RedisPage.configure do |config|
|
31
|
+
# 通过访问 http://cache:ewHN84JZLyRurX@example.com:8081/products/1 来刷新缓存
|
32
|
+
config.sweeper = { port: 8081, username: 'cache', password: 'ewHN84JZLyRurX' }
|
33
|
+
config.redis = Redis.new(host: "redis", port: 6379, db: 10)
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
24
37
|
### 1. Controller
|
25
38
|
|
26
39
|
生成页面缓存
|
27
40
|
|
28
41
|
```
|
29
42
|
class ProductController < ActionController::Base
|
30
|
-
caches_redis_page :show
|
43
|
+
caches_redis_page :show
|
31
44
|
|
32
45
|
def show
|
33
46
|
@product = Product.find(params[:id])
|
34
|
-
@model_name = Product.table_name
|
35
|
-
@model_id = @product.id
|
36
47
|
end
|
37
48
|
end
|
38
49
|
```
|
@@ -64,3 +75,10 @@ class Product < ActiveRecord::Base
|
|
64
75
|
include RedisPage::Sweeper
|
65
76
|
end
|
66
77
|
```
|
78
|
+
|
79
|
+
## Contribution
|
80
|
+
|
81
|
+
```
|
82
|
+
gem build redis_page.gemspec
|
83
|
+
gem push redis_page-0.1.0.gem
|
84
|
+
```
|
@@ -3,6 +3,7 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|
3
3
|
module ActionController
|
4
4
|
module Caching
|
5
5
|
module Pages
|
6
|
+
INSTANCE_PATH_REGEX = /^\/(\w+)\/(\d+)/
|
6
7
|
extend ActiveSupport::Concern
|
7
8
|
|
8
9
|
included do
|
@@ -12,18 +13,32 @@ module ActionController
|
|
12
13
|
def caches_redis_page(*actions)
|
13
14
|
options = actions.extract_options!
|
14
15
|
|
16
|
+
before_filter({only: actions}.merge(options)) do |c|
|
17
|
+
@page_need_to_cache = true
|
18
|
+
if options[:append_country]
|
19
|
+
# X-IP-Country 是通过 nginx GeoIP2 module 注入的 header
|
20
|
+
@cache_country = (cookies[:country] || request.headers['X-IP-Country']).upcase
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
15
24
|
after_filter({only: actions}.merge(options)) do |c|
|
16
|
-
|
25
|
+
path = request.path
|
26
|
+
path = "#{path}-#{@cache_country}" if @cache_country
|
27
|
+
c.cache_page(response.body, path)
|
28
|
+
c.record_cached_page
|
17
29
|
end
|
18
30
|
end
|
19
31
|
end
|
20
32
|
|
21
33
|
def cache_page(content, path)
|
22
34
|
Rails.logger.info "[page cache]caching: #{path}"
|
23
|
-
|
35
|
+
RedisPage.redis.set(path, content)
|
36
|
+
end
|
24
37
|
|
25
|
-
|
26
|
-
|
38
|
+
def record_cached_page
|
39
|
+
path, model_name, model_id = INSTANCE_PATH_REGEX.match(request.path).to_a
|
40
|
+
if model_id
|
41
|
+
mark_cache_instance(model_name, model_id)
|
27
42
|
end
|
28
43
|
end
|
29
44
|
|
data/lib/redis_page/sweeper.rb
CHANGED
@@ -8,9 +8,10 @@ module RedisPage
|
|
8
8
|
after_save :invalidate_instance_cache
|
9
9
|
|
10
10
|
def invalidate_instance_cache
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
RedisPage.redis.smembers("i:#{self.class.table_name}:#{self.id}").each do |info|
|
12
|
+
info = JSON.parse(info)
|
13
|
+
Rails.logger.info "[page cache]add sweeper job: #{info['url']}-#{info['country']}"
|
14
|
+
SweeperWorker.perform_async(info['url'], info['country'])
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -2,14 +2,16 @@ module RedisPage
|
|
2
2
|
class SweeperWorker
|
3
3
|
include Sidekiq::Worker
|
4
4
|
|
5
|
-
def perform(url)
|
5
|
+
def perform(url, country=nil)
|
6
6
|
uri = URI(url)
|
7
|
-
uri.port =
|
7
|
+
uri.port = RedisPage.sweeper[:port]
|
8
8
|
|
9
|
-
auth = { username:
|
9
|
+
auth = { username: RedisPage.sweeper[:username], password: RedisPage.sweeper[:password] }
|
10
|
+
options = { basic_auth: auth }
|
11
|
+
options[:cookies] = { country: country } if country
|
10
12
|
|
11
|
-
Rails.logger.info "[page cache]sweeper fetching: #{url}"
|
12
|
-
response = HTTParty.get(uri,
|
13
|
+
Rails.logger.info "[page cache]sweeper fetching: #{url}, country: #{country}"
|
14
|
+
response = HTTParty.get(uri, options)
|
13
15
|
Rails.logger.debug "[page cache]sweeper response: #{response.body}"
|
14
16
|
end
|
15
17
|
end
|
data/lib/redis_page/version.rb
CHANGED
@@ -3,7 +3,7 @@ module RedisPage
|
|
3
3
|
|
4
4
|
# 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
|
5
5
|
def c(object)
|
6
|
-
mark_cache_instance(object)
|
6
|
+
mark_cache_instance(object) if @page_need_to_cache
|
7
7
|
object
|
8
8
|
end
|
9
9
|
|
@@ -20,7 +20,8 @@ module RedisPage
|
|
20
20
|
name = object.class.table_name.downcase
|
21
21
|
id = object.id
|
22
22
|
end
|
23
|
-
|
23
|
+
Rails.logger.info "[page cache]record: #{name}##{id}"
|
24
|
+
RedisPage.redis.sadd("i:#{name}:#{id}", { url: request.url, country: @cache_country }.to_json)
|
24
25
|
end
|
25
26
|
|
26
27
|
end
|
data/lib/redis_page.rb
CHANGED
@@ -1 +1,23 @@
|
|
1
1
|
require 'redis_page/railtie'
|
2
|
+
|
3
|
+
module RedisPage
|
4
|
+
class Config
|
5
|
+
attr_accessor :sweeper, :redis
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.config
|
9
|
+
@@config ||= Config.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
yield self.config
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.redis
|
17
|
+
config.redis
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.sweeper
|
21
|
+
config.sweeper
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_page
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- saberma
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|