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 +4 -4
- data/README.md +3 -3
- data/lib/redis_page/sweeper.rb +36 -2
- data/lib/redis_page/version.rb +1 -1
- data/lib/redis_page/view_helpers.rb +12 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37a41cb3eb5f4307328c97116e992ccff5ebcba2
|
4
|
+
data.tar.gz: 15305dfc72280bf9cb96cf950df92021df3bda9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/redis_page/sweeper.rb
CHANGED
@@ -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
|
-
|
12
|
-
|
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
|
data/lib/redis_page/version.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
module RedisPage
|
2
2
|
module ViewHelpers
|
3
|
-
|
4
3
|
# 记录当前实体相关的页面,方便实体更新时,刷新页面缓存
|
5
|
-
def c(
|
6
|
-
|
7
|
-
|
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
|