Class: TaskHelper::API::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/task_helper/api/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(limit: 10) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/task_helper/api/cache.rb', line 4

def initialize(limit: 10)
  @limit = limit
  @calls = []
end

Instance Method Details

#get(**args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/task_helper/api/cache.rb', line 9

def get(**args)
  new_call = Call.new(args)
  cached_call = @calls.find { |call| call == new_call }
  if cached_call
    cached_call.run
  else
    @calls << new_call
    sort_calls.pop if @calls.size > @limit
    new_call.run
  end
end