Class: DefaultCache

Inherits:
Cache
  • Object
show all
Defined in:
lib/ff/ruby/server/sdk/api/default_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ DefaultCache

Returns a new instance of DefaultCache.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 9

def initialize(logger = nil)

  if logger != nil

    @logger = logger
  else

    @logger = Logger.new(STDOUT)
  end

  @keys = Set[]
  @capacity = 10 * 1000

  lambda = lambda { |*key| @logger.debug "Retrieved #{key}" }

  @in_memory = CacheBuilder.with(Cache)
                           .set_max(@capacity)
                           .set_post_get(lambda)
                           .build
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 7

def logger
  @logger
end

Instance Method Details

#delete(key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 53

def delete(key)

  if key == nil

    raise "Key is nil"
  end

  if @in_memory.exists?(key)

    @in_memory.invalidate(key)
  end

  @keys.delete(key)
end

#get(key) ⇒ Object



47
48
49
50
51
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 47

def get(key)

  value = @in_memory.get(key)
  value
end

#keysObject



68
69
70
71
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 68

def keys

  @keys
end

#set(key, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 34

def set(key, value)

  begin
    @in_memory.put(key, value)

  rescue ArgumentError => e

    @logger.error "ERROR: " + e.to_s

    raise "Invalid arguments passed to the 'set' method: key='" + key.to_s + "', value='" + value.to_s + "'"
  end
end

#verifyObject



30
31
32
# File 'lib/ff/ruby/server/sdk/api/default_cache.rb', line 30

def verify
  @in_memory != nil && @capacity > 0
end