Module: NodeCached

Included in:
GitRoom::Mounter
Defined in:
lib/gitroom/concerns/node_cached.rb

Constant Summary collapse

CACHE_FILES =
16384
MAX_FILE_SIZE =
65536
EXPIRE_TIME =
1.minutes
READ_SCORE =
2
WRITE_SCORE =
-7

Instance Method Summary collapse

Instance Method Details

#cache_file!(path, content) ⇒ Object



93
94
95
# 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) ⇒ Object



105
106
107
# 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) ⇒ Object



88
89
90
91
# 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) ⇒ Object



97
98
99
# 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) ⇒ Object



101
102
103
# 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) ⇒ Object



84
85
86
# 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) ⇒ Object



80
81
82
# 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) ⇒ Object



70
71
72
73
74
# 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) ⇒ Object



66
67
68
# 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) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# 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) ⇒ Object

redis.expire key, seconds



19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
# 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