Class: Reactor::Cache::Permission

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

Constant Summary collapse

BACKING_CACHE_EXPIRATION =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePermission

Returns a new instance of Permission.



12
13
14
# File 'lib/reactor/cache/permission.rb', line 12

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

Class Method Details

.instanceObject



8
9
10
# File 'lib/reactor/cache/permission.rb', line 8

def self.instance
  self.new
end

Instance Method Details

#invalidate(user) ⇒ Object



30
31
32
# File 'lib/reactor/cache/permission.rb', line 30

def invalidate(user)
  @@backing_storage.delete(user.to_s)
end

#lookup(user, key, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/reactor/cache/permission.rb', line 16

def lookup(user, key, &block)
  cache_entry = @@backing_storage.fetch(user.to_s, :expires_in => BACKING_CACHE_EXPIRATION.minutes) do
    {key => block.call}
  end
  if cache_entry.key?(key)
    cache_entry[key]
  else
    result = block.call
    @@backing_storage.write(user.to_s, cache_entry.merge({key => result}), :expires_in => BACKING_CACHE_EXPIRATION.minutes)
    result
  end
end