Class: Slk::Services::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/cache_store.rb

Overview

Persistent cache for user names, channel names, and subteams rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths: nil) ⇒ CacheStore

Returns a new instance of CacheStore.



10
11
12
13
14
15
16
17
18
# File 'lib/slk/services/cache_store.rb', line 10

def initialize(paths: nil)
  @paths = paths || Support::XdgPaths.new
  @user_cache = {}
  @channel_cache = {}
  @subteam_cache = {}
  @meta_cache = {}
  @on_warning = nil
  @on_cache_access = nil
end

Instance Attribute Details

#on_cache_accessObject

Returns the value of attribute on_cache_access.



8
9
10
# File 'lib/slk/services/cache_store.rb', line 8

def on_cache_access
  @on_cache_access
end

#on_warningObject

Returns the value of attribute on_warning.



8
9
10
# File 'lib/slk/services/cache_store.rb', line 8

def on_warning
  @on_warning
end

Instance Method Details

#channel_cache_file_exists?(workspace_name) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/slk/services/cache_store.rb', line 168

def channel_cache_file_exists?(workspace_name)
  File.exist?(channel_cache_file(workspace_name))
end

#channel_cache_size(workspace_name) ⇒ Object



159
160
161
162
# File 'lib/slk/services/cache_store.rb', line 159

def channel_cache_size(workspace_name)
  load_channel_cache(workspace_name)
  @channel_cache[workspace_name].size
end

#clear_channel_cache(workspace_name = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/slk/services/cache_store.rb', line 127

def clear_channel_cache(workspace_name = nil)
  if workspace_name
    @channel_cache.delete(workspace_name)
    file = channel_cache_file(workspace_name)
    FileUtils.rm_f(file)
  else
    @channel_cache.clear
    Dir.glob(@paths.cache_file('channels-*.json')).each { |f| File.delete(f) }
  end
end

#clear_user_cache(workspace_name = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/slk/services/cache_store.rb', line 94

def clear_user_cache(workspace_name = nil)
  if workspace_name
    @user_cache.delete(workspace_name)
    file = user_cache_file(workspace_name)
    FileUtils.rm_f(file)
  else
    @user_cache.clear
    Dir.glob(@paths.cache_file('users-*.json')).each { |f| File.delete(f) }
  end
end

#each_meta(workspace_name) ⇒ Object



41
42
43
44
# File 'lib/slk/services/cache_store.rb', line 41

def each_meta(workspace_name)
  load_meta_cache(workspace_name)
  (@meta_cache[workspace_name] || {}).each
end

#get_channel_id(workspace_name, channel_name) ⇒ Object

Channel cache methods



106
107
108
109
110
111
# File 'lib/slk/services/cache_store.rb', line 106

def get_channel_id(workspace_name, channel_name)
  load_channel_cache(workspace_name)
  result = @channel_cache.dig(workspace_name, channel_name)
  log_cache_access('channel_id', workspace_name, channel_name, result)
  result
end

#get_channel_name(workspace_name, channel_id) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/slk/services/cache_store.rb', line 113

def get_channel_name(workspace_name, channel_id)
  load_channel_cache(workspace_name)
  cache = @channel_cache[workspace_name] || {}
  result = cache.key(channel_id)
  log_cache_access('channel_name', workspace_name, channel_id, result)
  result
end

#get_meta(workspace_name, key, ttl: nil) ⇒ Object

Workspace metadata cache (auth.test user_id, team.info team_id, team.profile.get schema). One JSON file per workspace; entries can carry an optional fetched_at epoch for TTL checks.



23
24
25
26
27
28
29
30
31
# File 'lib/slk/services/cache_store.rb', line 23

def get_meta(workspace_name, key, ttl: nil)
  load_meta_cache(workspace_name)
  entry = @meta_cache[workspace_name][key]
  return nil unless entry
  return nil if ttl && entry['fetched_at'] && Time.now.to_i - entry['fetched_at'] > ttl

  log_cache_access('meta', workspace_name, key, entry['value'])
  entry['value']
end

#get_subteam(workspace_name, subteam_id) ⇒ Object

Subteam cache methods



139
140
141
142
143
144
# File 'lib/slk/services/cache_store.rb', line 139

def get_subteam(workspace_name, subteam_id)
  load_subteam_cache(workspace_name)
  result = @subteam_cache.dig(workspace_name, subteam_id)
  log_cache_access('subteam', workspace_name, subteam_id, result)
  result
end

#get_user(workspace_name, user_id) ⇒ Object

User cache methods



47
48
49
50
51
52
# File 'lib/slk/services/cache_store.rb', line 47

def get_user(workspace_name, user_id)
  load_user_cache(workspace_name)
  result = @user_cache.dig(workspace_name, user_id)
  log_cache_access('user', workspace_name, user_id, result)
  result
end

#get_user_id_by_name(workspace_name, name) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/slk/services/cache_store.rb', line 54

def get_user_id_by_name(workspace_name, name)
  load_user_cache(workspace_name)
  cache = @user_cache[workspace_name] || {}
  # Reverse lookup: find user_id where display_name matches
  result = cache.find { |_id, display_name| display_name == name }&.first
  log_cache_access('user_by_name', workspace_name, name, result)
  result
end

#populate_user_cache(workspace_name, users) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/slk/services/cache_store.rb', line 83

def populate_user_cache(workspace_name, users)
  @user_cache[workspace_name] = {}

  users.each do |user|
    @user_cache[workspace_name][user.id] = user.best_name
  end

  save_user_cache(workspace_name)
  @user_cache[workspace_name].size
end

#save_user_cache(workspace_name) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/slk/services/cache_store.rb', line 75

def save_user_cache(workspace_name)
  return if @user_cache[workspace_name].nil? || @user_cache[workspace_name].empty?

  @paths.ensure_cache_dir
  file = user_cache_file(workspace_name)
  File.write(file, JSON.pretty_generate(@user_cache[workspace_name]))
end

#set_channel(workspace_name, channel_name, channel_id) ⇒ Object



121
122
123
124
125
# File 'lib/slk/services/cache_store.rb', line 121

def set_channel(workspace_name, channel_name, channel_id)
  @channel_cache[workspace_name] ||= {}
  @channel_cache[workspace_name][channel_name] = channel_id
  save_channel_cache(workspace_name)
end

#set_meta(workspace_name, key, value, persist: true) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/slk/services/cache_store.rb', line 33

def set_meta(workspace_name, key, value, persist: true)
  load_meta_cache(workspace_name)
  @meta_cache[workspace_name] ||= {}
  @meta_cache[workspace_name][key] = { 'value' => value, 'fetched_at' => Time.now.to_i }
  save_meta_cache(workspace_name) if persist
  value
end

#set_subteam(workspace_name, subteam_id, handle) ⇒ Object



146
147
148
149
150
151
# File 'lib/slk/services/cache_store.rb', line 146

def set_subteam(workspace_name, subteam_id, handle)
  load_subteam_cache(workspace_name)
  @subteam_cache[workspace_name] ||= {}
  @subteam_cache[workspace_name][subteam_id] = handle
  save_subteam_cache(workspace_name)
end

#set_user(workspace_name, user_id, display_name, persist: false) ⇒ Object



63
64
65
66
67
68
# File 'lib/slk/services/cache_store.rb', line 63

def set_user(workspace_name, user_id, display_name, persist: false)
  load_user_cache(workspace_name)
  @user_cache[workspace_name] ||= {}
  @user_cache[workspace_name][user_id] = display_name
  save_user_cache(workspace_name) if persist
end

#user_cache_file_exists?(workspace_name) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/slk/services/cache_store.rb', line 164

def user_cache_file_exists?(workspace_name)
  File.exist?(user_cache_file(workspace_name))
end

#user_cache_size(workspace_name) ⇒ Object

Cache status



154
155
156
157
# File 'lib/slk/services/cache_store.rb', line 154

def user_cache_size(workspace_name)
  load_user_cache(workspace_name)
  @user_cache[workspace_name].size
end

#user_cached?(workspace_name, user_id) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/slk/services/cache_store.rb', line 70

def user_cached?(workspace_name, user_id)
  load_user_cache(workspace_name)
  @user_cache.dig(workspace_name, user_id) != nil
end