module NodeCached

Constants

CACHE_FILES
EXPIRE_TIME
MAX_FILE_SIZE
READ_SCORE
WRITE_SCORE

Public Instance Methods

cache_file!(path, content) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 93
def cache_file!(path, content)
  redis.set "cache::file::#{path}::content", content.to_yaml
end
cached_file_clean!(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 105
def cached_file_clean!(path)
  redis.del "cache::file::#{path}::content"
end
cached_file_content(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 88
def cached_file_content(path)
  content = redis.get "cache::file::#{path}::content"
  content && YAML.load(content)
end
cached_file_content_expires(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 97
def cached_file_content_expires(path)
  redis.ttl "cache::file::#{path}::content"
end
cached_file_content_touch!(path, time) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 101
def cached_file_content_touch!(path, time)
  redis.expire "cache::file::#{path}::content", time
end
cached_file_rank(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 84
def cached_file_rank(path)
  redis.zrevrank "cache::files::ranks", path
end
cached_file_rank_change!(path, incr) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 80
def cached_file_rank_change!(path, incr)
  redis.zincrby "cache::files::ranks", incr, path
end
cached_node_absent!(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 70
def cached_node_absent!(path)
  redis.set "cache::node::#{path}::present?", 'false'
  redis.del "cache::file::#{path}::content"
  redis.zrem "cache::files::ranks", path
end
cached_node_present!(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 66
def cached_node_present!(path)
  redis.set "cache::node::#{path}::present?", 'true'
end
cached_node_present?(path) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 76
def cached_node_present?(path)
  redis.get "cache::node::#{path}::present?"
end
find_cached_node(path) click to toggle source

redis.expire key, seconds

# File lib/gitroom/concerns/node_cached.rb, line 19
def find_cached_node(path)
  present = cached_node_present? path
  if present.nil? || present == 'true'
    node = Node.where(:path => path).first
    if node && present.nil?
      cached_node_present! path
    elsif !node
      cached_node_absent! path
    end
    node
  end
end
read_cached_node(node, size, offset) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 32
def read_cached_node(node, size, offset)
  path = node.path
  cached_file_rank_change! path, READ_SCORE
  rank = cached_file_rank path
  content = cached_file_content path
  if content
    print "CACHE HIT :: #{path}\n"
    res = content[offset...offset + size]
    if rank < CACHE_FILES
      cached_file_content_touch! path, EXPIRE_TIME
    end
  else
    print "DATABASE HIT :: #{path}\n"
    content = node.get_full_content
    full_size = content.size
    if rank < CACHE_FILES && full_size < MAX_FILE_SIZE
      res = content[offset...offset + size]
      cache_file! path, content
      cached_file_content_touch! path, EXPIRE_TIME
    else
      res = node.get_content offset, size
    end
  end
  res
end
write_cached_node(node, buf, offset) click to toggle source
# File lib/gitroom/concerns/node_cached.rb, line 58
def write_cached_node(node, buf, offset)
  path = node.path
  cached_file_rank_change! path, WRITE_SCORE
  cached_file_clean! path
  node.set_content offset, buf
  # read_cached_node node, buf.size, offset
end