Class: T2Server::Server::RunCache

Inherits:
Object
  • Object
show all
Defined in:
lib/t2-server/run-cache.rb

Overview

This class is used to cache Run objects in a Server so they don’t need to be created so often. When manipulating this cache the user credentials should be passed in, or the global user “:all” will be used instead.

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ RunCache

Returns a new instance of RunCache.



43
44
45
46
# File 'lib/t2-server/run-cache.rb', line 43

def initialize(server)
  @server = server
  @cache = {}
end

Instance Method Details

#add_run(runs, credentials = nil) ⇒ Object

Add a run, or runs, to the cache.



49
50
51
52
53
54
55
# File 'lib/t2-server/run-cache.rb', line 49

def add_run(runs, credentials = nil)
  cache = user_cache(credentials)

  [*runs].each do |run|
    cache[run.id] = run
  end
end

#clear!(credentials = nil) ⇒ Object

Delete all runs objects from the cache. This does not delete runs from the remote server - just their locally cached instances.



79
80
81
# File 'lib/t2-server/run-cache.rb', line 79

def clear!(credentials = nil)
  user_cache(credentials).clear
end

#refresh_all!(run_list, credentials = nil) ⇒ Object

This method adds all new runs (creating instances where required) in the list provided AND removes any runs no longer in the list.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/t2-server/run-cache.rb', line 59

def refresh_all!(run_list, credentials = nil)
  cache = user_cache(credentials)

  # Add new runs to the user cache.
  run_list.each_key do |id|
    if !cache.has_key? id
      cache[id] = Run.create(@server, "", credentials, run_list[id])
    end
  end

  # Clear out the expired runs.
  if cache.length > run_list.length
    cache.delete_if {|key, _| !run_list.member? key}
  end

  cache
end

#runs(credentials = nil) ⇒ Object

Get all the specified user’s runs.



84
85
86
# File 'lib/t2-server/run-cache.rb', line 84

def runs(credentials = nil)
  user_cache(credentials)
end