class TwitterFriendly::Cache

Public Class Methods

new(*args) click to toggle source
# File lib/twitter_friendly/cache.rb, line 9
def initialize(*args)
  options = {expires_in: 1.hour, race_condition_ttl: 5.minutes}.merge(args.extract_options!)

  path = options[:cache_dir] || File.join('cache')
  FileUtils.mkdir_p(path) unless File.exists?(path)
  @client = ::ActiveSupport::Cache::FileStore.new(path, options)
end

Public Instance Methods

fetch(key, args:) { || ... } click to toggle source
# File lib/twitter_friendly/cache.rb, line 17
def fetch(key, args:, &block)
  block_result = nil
  yield_and_encode = Proc.new do
    block_result = yield
    encode(block_result, args: args)
  end

  # 目的のデータがキャッシュになかった場合、キャッシュにはシリアライズしたJSONを保存しつつ、
  # このメソッドの呼び出し元にはJSONにシリアライズする前の結果を返している。
  # こうしないと、不要なデコードをすることになってしまう。

  fetch_result = @client.fetch(key, &yield_and_encode)

  block_result || decode(fetch_result, args: args)
end

Private Instance Methods

decode(str, args:) click to toggle source
# File lib/twitter_friendly/cache.rb, line 39
def decode(str, args:)
  Serializer.decode(str, args: args)
end
encode(obj, args:) click to toggle source
# File lib/twitter_friendly/cache.rb, line 35
def encode(obj, args:)
  Serializer.encode(obj, args: args)
end