redis_page 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f4beebad0af5093a904e8018586f26331fdbf8c
4
- data.tar.gz: b187937071618ac5b9a53a073c234de825d89e06
3
+ metadata.gz: df9e7a7ba2a4887f6e5c54764f503d3f887191bc
4
+ data.tar.gz: db82203f0bc3a2c6a2e6f49700c631a044cb8cdf
5
5
  SHA512:
6
- metadata.gz: 06d3caae4fe5ea15b205236db21815d1172122a175a52974a9359e00004cd6a4660f648411a01f4881f869344ef7b4e463d6b89ec2a558b24dab07b554b3648a
7
- data.tar.gz: 2e61816f7303f0a0cad3fa1d1b7e435653354b9e2b254e86cb45ea94985bcd88541162f7b047eee806e2eac72e69d163f830aedf6bc9babad62e3332fdb8e12b
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, :new
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
- c.cache_page(response.body, request.path)
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
- $redis.set(path, content)
35
+ RedisPage.redis.set(path, content)
36
+ end
24
37
 
25
- if @model_name
26
- mark_cache_instance(@model_name, @model_id)
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
 
@@ -8,9 +8,10 @@ module RedisPage
8
8
  after_save :invalidate_instance_cache
9
9
 
10
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)
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 = 8081
7
+ uri.port = RedisPage.sweeper[:port]
8
8
 
9
- auth = { username: "cache", password: "ewHN84JZLyRurX" }
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, basic_auth: auth)
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
@@ -1,3 +1,3 @@
1
1
  module RedisPage
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
- $redis.sadd("i:#{name}:#{id}", request.url)
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.0
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-24 00:00:00.000000000 Z
11
+ date: 2015-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport