Class: Mbeditor::FileTreeService
- Inherits:
-
Object
- Object
- Mbeditor::FileTreeService
- Defined in:
- app/services/mbeditor/file_tree_service.rb
Class Method Summary collapse
- .build(workspace_root) ⇒ Object
-
.cached_json(workspace_root) ⇒ Object
The tree already serialized, plus a digest of that body, memoized inside the same cache entry.
- .invalidate(workspace_root) ⇒ Object
- .reset! ⇒ Object
Class Method Details
.build(workspace_root) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/services/mbeditor/file_tree_service.rb', line 10 def self.build(workspace_root) root = workspace_root.to_s MUTEX.synchronize do @cache ||= {} entry = @cache[root] if entry && (Process.clock_gettime(Process::CLOCK_MONOTONIC) - entry[:ts]) < 15 return entry[:data] end end matcher = ExclusionMatcher.new(Mbeditor.configuration.excluded_paths, root: root) data = traverse(root, root, matcher) MUTEX.synchronize do @cache ||= {} @cache[root] = { ts: Process.clock_gettime(Process::CLOCK_MONOTONIC), data: data } end data end |
.cached_json(workspace_root) ⇒ Object
The tree already serialized, plus a digest of that body, memoized inside the same cache entry. /files is polled every 10s and the payload is ~1 MB, so the digest is what turns an unchanged poll into a 304.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/services/mbeditor/file_tree_service.rb', line 34 def self.cached_json(workspace_root) root = workspace_root.to_s data = build(root) MUTEX.synchronize do entry = (@cache ||= {})[root] return entry[:json] if entry && entry[:json] && entry[:data].equal?(data) # to_json, not JSON.generate: `render json:` used ActiveSupport's # encoder, and the body must stay byte-identical. body = data.to_json payload = { body: body, digest: Digest::SHA1.hexdigest(body) } entry[:json] = payload if entry payload end end |
.invalidate(workspace_root) ⇒ Object
51 52 53 54 55 56 57 |
# File 'app/services/mbeditor/file_tree_service.rb', line 51 def self.invalidate(workspace_root) MUTEX.synchronize do @cache ||= {} @cache.delete(workspace_root.to_s) end nil end |
.reset! ⇒ Object
59 60 61 62 |
# File 'app/services/mbeditor/file_tree_service.rb', line 59 def self.reset! MUTEX.synchronize { @cache = {} } nil end |