-
Notifications
You must be signed in to change notification settings - Fork 983
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #13618 - cache expensive vmware api calls
- Loading branch information
1 parent
9182590
commit 0520693
Showing
17 changed files
with
291 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ComputeResourceCaching | ||
extend ActiveSupport::Concern | ||
|
||
def reload_cache | ||
cache.reload | ||
end | ||
|
||
private | ||
|
||
def cache | ||
@cache ||= ComputeResourceCache.new(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# This class caches attributes for a compute resource in | ||
# rails cache to speed up slow or expensive API calls | ||
class ComputeResourceCache | ||
attr_accessor :compute_resource, :cache_duration | ||
|
||
delegate :logger, :to => ::Rails | ||
|
||
def initialize(compute_resource, cache_duration: 180.minutes) | ||
self.compute_resource = compute_resource | ||
self.cache_duration = cache_duration | ||
end | ||
|
||
# Tries to retrieve the value for a given key from the cache | ||
# and returns the retrieved value. If the cache is empty, | ||
# the given block is executed and the block's return stored | ||
# in the cache. This value is then returned by this method. | ||
def cache(key, &block) | ||
return get_uncached_value(key, &block) unless cache_enabled? | ||
cached_value = read(key) | ||
return cached_value if cached_value | ||
return unless block_given? | ||
uncached_value = get_uncached_value(key, &block) | ||
write(key, uncached_value) | ||
uncached_value | ||
end | ||
|
||
def delete(key) | ||
logger.debug "Deleting from compute resource cache: #{key}" | ||
Rails.cache.delete(cache_key + key.to_s) | ||
end | ||
|
||
def read(key) | ||
logger.debug "Reading from compute resource cache: #{key}" | ||
Rails.cache.read(cache_key + key.to_s, cache_options) | ||
end | ||
|
||
def write(key, value) | ||
logger.debug "Storing in compute resource cache: #{key}" | ||
Rails.cache.write(cache_key + key.to_s, value, cache_options) | ||
end | ||
|
||
def reload | ||
# Rolls the cache_scope to reload the cache as not all | ||
# cache implementations (eg. memcached) support deleting | ||
# keys by a regex | ||
Rails.cache.delete(cache_scope_key) | ||
true | ||
rescue StandardError => e | ||
Foreman::Logging.exception('Failed to reload compute resource cache', e) | ||
false | ||
end | ||
|
||
def cache_scope | ||
Rails.cache.fetch(cache_scope_key, cache_options) do | ||
Foreman.uuid | ||
end | ||
end | ||
|
||
def cache_enabled? | ||
compute_resource.persisted? && compute_resource.caching_enabled | ||
end | ||
|
||
private | ||
|
||
def get_uncached_value(key, &block) | ||
return unless block_given? | ||
start_time = Time.now.utc | ||
result = compute_resource.instance_eval(&block) | ||
end_time = Time.now.utc | ||
duration = end_time - (start_time).round(4) | ||
logger.info("Loaded compute resource data for #{key} in #{duration} seconds") | ||
result | ||
end | ||
|
||
def cache_key | ||
"compute_resource_#{compute_resource.id}-#{cache_scope}/" | ||
end | ||
|
||
def cache_scope_key | ||
"compute_resource_#{compute_resource.id}-cache_scope_key" | ||
end | ||
|
||
def cache_options | ||
{ | ||
:expires_in => cache_duration, | ||
:race_condition_ttl => 1.minute | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
attributes :user, :datacenter, :server, :set_console_password | ||
attributes :user, :datacenter, :server, :set_console_password, :caching_enabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20170127094357_add_caching_enabled_to_compute_resource.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddCachingEnabledToComputeResource < ActiveRecord::Migration | ||
def change | ||
add_column :compute_resources, :caching_enabled, :boolean, default: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.