Module: ActionController::Caching::RedisPages

Extended by:
ActiveSupport::Concern
Included in:
ActionController::Caching
Defined in:
lib/action_controller/caching/redis_pages.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

INSTANCE_PATH_REGEX =
/^\/(\w+)\/(\d+)/

Instance Method Summary collapse

Instance Method Details

#cache_redis_page(content, path, options = {}) ⇒ Object

TODO: 全球化部署时需要将一个页面写到多个redis上去,需要确保: 1. 写入速度快; 2. 确保写入成功;



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/action_controller/caching/redis_pages.rb', line 48

def cache_redis_page(content, path, options = {})
  key  = path
  text = "[page cache]caching: #{path}"
  if namespace = options[:namespace]
    key  = "#{namespace}:#{key}"
    text = "#{text} in #{namespace}"
  end
  Rails.logger.info text
  # RedisPage.cache_page_redis.setex(key, RedisPage.config.ttl || 604800, content)    # 1 周后失效
  # 对于某个原本带有生存时间(TTL)的键来说, 当 SET 命令成功在这个键上执行时, 这个键原有的 TTL 将被清除。
  RedisPage.cache_page_redis.set(key, content)    # 永不失效
end

#compress_content(content) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/action_controller/caching/redis_pages.rb', line 36

def compress_content(content)
  case RedisPage.compress_method
  when :deflate
    Zlib::Deflate.deflate(content)
  when :gzip
    ActiveSupport::Gzip.compress(content)
  else
    content
  end
end

#record_cached_pageObject



61
62
63
64
65
66
# File 'lib/action_controller/caching/redis_pages.rb', line 61

def record_cached_page
  path, model_name, model_id = INSTANCE_PATH_REGEX.match(request.path).to_a
  if model_id
    mark_cache_instance(model_name, model_id)
  end
end