Class: Reactor::Cache::User

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/cache/user.rb

Constant Summary collapse

BACKING_CACHE_EXPIRATION =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUser

Returns a new instance of User.



10
11
12
# File 'lib/reactor/cache/user.rb', line 10

def initialize
  @@backing_storage ||= ActiveSupport::Cache::MemoryStore.new({ size: 1.megabyte })
end

Class Method Details

.instanceObject



6
7
8
# File 'lib/reactor/cache/user.rb', line 6

def self.instance
  new
end

Instance Method Details

#get(user_name) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/reactor/cache/user.rb', line 14

def get(user_name)
  @cache ||= {}
  # Rails.logger.debug "User:Cache hit: #{hit?(user_name.to_s)} [#{@cache[user_name.to_s].inspect}]"

  key = user_name.to_s
  @@backing_storage.fetch(key, expires_in: BACKING_CACHE_EXPIRATION.minutes) do
    Reactor::Session::User.new(key)
  end
end

#invalidate(user_name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/reactor/cache/user.rb', line 28

def invalidate(user_name)
  # Rails 7.1+ versions don't allow nil values and empty string as cache keys.
  if user_name.present?
    @@backing_storage.delete(user_name.to_s)
  else
    Rails.logger.warn "Reactor::Cache::User: Skipping cache invalidation because the key derived from user_name.to_s was blank."
  end
end

#set(user_name, user) ⇒ Object



24
25
26
# File 'lib/reactor/cache/user.rb', line 24

def set(user_name, user)
  @@backing_storage.write(user_name.to_s, user, expires_in: BACKING_CACHE_EXPIRATION.minutes)
end