class LRUHash

def clear_unused_resources

def clear_unused_resources
  payload = {
    size: @table.size,
    examined: 0,
    cleared: 0,
    elapsed: nil,
  }
  timer_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  ran = try_synchronize do
    # Clears resources that have not been used
    # in the last 5 minutes (default value of Semian.minimum_lru_time).
    stop_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @min_time
    @table.each do |_, resource|
      payload[:examined] += 1
      # The update times of the resources in the LRU are monotonically increasing,
      # time, so we can stop looking once we find the first resource with an
      # update time after the stop_time.
      break if resource.updated_at > stop_time
      next if resource.in_use?
      resource = @table.delete(resource.name)
      if resource
        payload[:cleared] += 1
        resource.destroy
      end
    end
  end
  if ran
    payload[:elapsed] = Process.clock_gettime(Process::CLOCK_MONOTONIC) - timer_start
    Semian.notify(:lru_hash_gc, self, nil, nil, payload)
  end
end