redis_page 0.1.5 → 0.1.6

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: 032b69aeb3e0c9a5194b31c53010fd157783e068
4
- data.tar.gz: ccc7bca4c5a88df4cda1120d2800b9c000d3c3ac
3
+ metadata.gz: 37a41cb3eb5f4307328c97116e992ccff5ebcba2
4
+ data.tar.gz: 15305dfc72280bf9cb96cf950df92021df3bda9a
5
5
  SHA512:
6
- metadata.gz: fba5ee5ea042f5d1b3c5fe45e59fbcfb6b9b3ceb1a70708cd61ae9358ff4a33a85f5546250fef458d21d41b6d84fe32d761eba495c5581c759e0852f9b481c59
7
- data.tar.gz: 43be76db053d6ddf298182d617d03a77389e0ff6ffb1da0084b5a42335f8d67230132afb2dc5ba7cd2c55e9b24ed5f602c7a040dc56e7e5907e00776c421241f
6
+ metadata.gz: f638a549e0152e3a9dac2aca65a6a2651eeabb7e48a54ba09aa486f19593aebf55afd648c599638b7a5c5f0a78b95d9bd757ce77a42168625b896d4828bbe7ea
7
+ data.tar.gz: d0ebdf75cd3ea216172172bb60ce1ac590a610774c4ba3276d9b3a8e1605a55fcfcf7890f46fbf7298db40b6789e69ae9bf791e610d615794a70681308dd7d84
data/README.md CHANGED
@@ -68,15 +68,15 @@ end
68
68
  修改为:
69
69
 
70
70
  ```
71
- - Product.all.each do |product|
71
+ - c(Product).all.each do |product|
72
72
  = c(@product).title
73
73
  ```
74
74
 
75
- c 方法会记录当前页面 url
75
+ c 方法会记录当前页面 url, c(Product) 表示添加、删除商品也会刷新当前页面
76
76
 
77
77
  ### 4. Model
78
78
 
79
- 更新实体后刷新所有关联的页面缓存
79
+ 更新、删除、添加实体后刷新所有关联的页面缓存
80
80
 
81
81
  ```
82
82
  class Product < ActiveRecord::Base
@@ -6,10 +6,44 @@ module RedisPage
6
6
 
7
7
  included do
8
8
  after_save :invalidate_instance_cache
9
+ after_destroy :invalidate_clazz_cache
9
10
 
10
11
  def invalidate_instance_cache
11
- RedisPage.redis.smembers("i:#{self.class.table_name}:#{self.id}").each do |info|
12
- info = JSON.parse(info)
12
+ urls = {}
13
+ key = "i:#{self.class.table_name}:#{self.id}"
14
+ Rails.logger.info "[page cache]invalidate: #{key}"
15
+ RedisPage.redis.smembers(key).each do |info|
16
+ RedisPage.redis.srem(key, info)
17
+ add_infos(urls, info)
18
+ end
19
+ add_clazz_infos(urls)
20
+ fetch_infos(urls)
21
+ end
22
+
23
+ def invalidate_clazz_cache
24
+ urls = {}
25
+ add_clazz_infos(urls)
26
+ fetch_infos(urls)
27
+ end
28
+
29
+ private
30
+ def add_infos(urls, info)
31
+ info = JSON.parse(info)
32
+ key = "#{info['url']}-#{info['country']}"
33
+ urls[key] = info unless urls[key]
34
+ end
35
+
36
+ def add_clazz_infos(urls)
37
+ key = "c:#{self.class.table_name}"
38
+ Rails.logger.info "[page cache]invalidate: #{key}"
39
+ RedisPage.redis.smembers(key).each do |info|
40
+ RedisPage.redis.srem(key, info)
41
+ add_infos(urls, info)
42
+ end
43
+ end
44
+
45
+ def fetch_infos(urls)
46
+ urls.values.each do |info|
13
47
  Rails.logger.info "[page cache]add sweeper job: #{info['url']}-#{info['country']}"
14
48
  SweeperWorker.perform_async(info['url'], info['country'])
15
49
  end
@@ -1,3 +1,3 @@
1
1
  module RedisPage
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -1,10 +1,11 @@
1
1
  module RedisPage
2
2
  module ViewHelpers
3
-
4
3
  # 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
5
- def c(object)
6
- mark_cache_instance(object) if @page_need_to_cache
7
- object
4
+ def c(object_or_clazz)
5
+ if @page_need_to_cache
6
+ object_or_clazz.is_a?(Class) ? mark_cache_clazz(object_or_clazz) : mark_cache_instance(object_or_clazz)
7
+ end
8
+ object_or_clazz
8
9
  end
9
10
 
10
11
  # 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
@@ -24,5 +25,12 @@ module RedisPage
24
25
  RedisPage.redis.sadd("i:#{name}:#{id}", { url: request.url, country: @cache_country }.to_json)
25
26
  end
26
27
 
28
+ # 记录类相关的页面,方便实体创建时,刷新页面缓存
29
+ def mark_cache_clazz(clazz)
30
+ name = clazz.table_name
31
+ Rails.logger.info "[page cache]class: #{name}"
32
+ RedisPage.redis.sadd("c:#{name}", { url: request.url, country: @cache_country }.to_json)
33
+ end
34
+
27
35
  end
28
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_page
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - saberma