Class: KStor::StoreCache

Inherits:
Object
  • Object
show all
Defined in:
lib/kstor/store.rb

Overview

Simplistic cache for list of users and groups.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStoreCache

Create new cache.



13
14
15
16
# File 'lib/kstor/store.rb', line 13

def initialize
  @cache = {}
  @cache.extend(Mutex_m)
end

Instance Attribute Details

#groupsObject

Returns cached list of groups

Returns:

  • returns cached list of groups



48
# File 'lib/kstor/store.rb', line 48

property :groups

#usersObject

Returns cached list of users

Returns:

  • returns cached list of users



46
# File 'lib/kstor/store.rb', line 46

property :users

Class Method Details

.property(name) ⇒ Object

Declare a cached list of values.

Parameters:

  • name (Symbol)

    name of list



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kstor/store.rb', line 26

def property(name)
  define_method(name) do |&block|
    @cache.synchronize do
      return @cache[name] if @cache.key?(name)

      @cache[name] = block.call
    end
  end

  define_method("#{name}=".to_sym) do |list|
    @cache.synchronize { @cache[name] = list }
  end

  define_method("forget_#{name}") do
    @cache.synchronize { @cache.delete(name) }
  end
end