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
# File 'lib/slk/services/cache_store.rb', line 10

def initialize(paths: nil)
  @paths = paths || Support::XdgPaths.new
  @user_cache = {}
  @channel_cache = {}
  @subteam_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)


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

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

#channel_cache_size(workspace_name) ⇒ Object



132
133
134
135
# File 'lib/slk/services/cache_store.rb', line 132

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

#clear_channel_cache(workspace_name = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/slk/services/cache_store.rb', line 100

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/slk/services/cache_store.rb', line 67

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

#get_channel_id(workspace_name, channel_name) ⇒ Object

Channel cache methods



79
80
81
82
83
84
# File 'lib/slk/services/cache_store.rb', line 79

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



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

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_subteam(workspace_name, subteam_id) ⇒ Object

Subteam cache methods



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

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



20
21
22
23
24
25
# File 'lib/slk/services/cache_store.rb', line 20

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



27
28
29
30
31
32
33
34
# File 'lib/slk/services/cache_store.rb', line 27

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/slk/services/cache_store.rb', line 56

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



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

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



94
95
96
97
98
# File 'lib/slk/services/cache_store.rb', line 94

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_subteam(workspace_name, subteam_id, handle) ⇒ Object



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

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



36
37
38
39
40
41
# File 'lib/slk/services/cache_store.rb', line 36

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)


137
138
139
# File 'lib/slk/services/cache_store.rb', line 137

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

#user_cache_size(workspace_name) ⇒ Object

Cache status



127
128
129
130
# File 'lib/slk/services/cache_store.rb', line 127

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

#user_cached?(workspace_name, user_id) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/slk/services/cache_store.rb', line 43

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