Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-02-04 21:20:51 +0300
committerMatija Čupić <matteeyah@gmail.com>2018-02-05 01:39:34 +0300
commit18af7fab9457db742cba89d9a075ed95bc2f6846 (patch)
tree1cf86fddd718bb2eff7694d10f835873925535d2
parent3366f377c1d4cbb02ecc5a2e47b059ed375c5e09 (diff)
Extract attribute caching mechanism into concern
-rw-r--r--app/models/ci/runner.rb46
-rw-r--r--app/models/concerns/attribute_cacheable.rb31
2 files changed, 34 insertions, 43 deletions
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb
index 5711be3ae29..c2527a7893c 100644
--- a/app/models/ci/runner.rb
+++ b/app/models/ci/runner.rb
@@ -2,6 +2,7 @@ module Ci
class Runner < ActiveRecord::Base
extend Gitlab::Ci::Model
include Gitlab::SQL::Pattern
+ include AttributeCacheable
RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
ONLINE_CONTACT_TIMEOUT = 1.hour
@@ -43,6 +44,8 @@ module Ci
after_destroy :cleanup_runner_queue
+ redis_cached_attributes :name, :version, :revision, :platform, :architecture, :contacted_at
+
enum access_level: {
not_protected: 0,
ref_protected: 1
@@ -68,30 +71,6 @@ module Ci
ONLINE_CONTACT_TIMEOUT.ago
end
- def name
- cached_attribute(:name) || read_attribute(:name)
- end
-
- def version
- cached_attribute(:version) || read_attribute(:version)
- end
-
- def revision
- cached_attribute(:revision) || read_attribute(:revision)
- end
-
- def platform
- cached_attribute(:platform) || read_attribute(:platform)
- end
-
- def architecture
- cached_attribute(:architecture) || read_attribute(:architecture)
- end
-
- def contacted_at
- cached_attribute(:contacted_at) || read_attribute(:contacted_at)
- end
-
def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
end
@@ -201,10 +180,6 @@ module Ci
"runner:build_queue:#{self.token}"
end
- def cache_attribute_key(key)
- "runner:info:#{self.id}:#{key}"
- end
-
def persist_cached_data?
# Use a random threshold to prevent beating DB updates.
# It generates a distribution between [40m, 80m].
@@ -216,21 +191,6 @@ module Ci
(Time.now - real_contacted_at) >= contacted_at_max_age
end
- def cached_attribute(key)
- @cached_attributes = {}
- @cached_attributes[key] ||= Gitlab::Redis::SharedState.with do |redis|
- redis.get(cache_attribute_key(key))
- end
- end
-
- def cache_attributes(values)
- Gitlab::Redis::SharedState.with do |redis|
- values.each do |key, value|
- redis.set(cache_attribute_key(key), value, ex: 24.hours)
- end
- end
- end
-
def tag_constraints
unless has_tags? || run_untagged?
errors.add(:tags_list,
diff --git a/app/models/concerns/attribute_cacheable.rb b/app/models/concerns/attribute_cacheable.rb
new file mode 100644
index 00000000000..34617489ac5
--- /dev/null
+++ b/app/models/concerns/attribute_cacheable.rb
@@ -0,0 +1,31 @@
+module AttributeCacheable
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def redis_cached_attributes(*attributes)
+ attributes.each do |attribute|
+ define_method("#{attribute}") do
+ cached_attribute(attribute) || read_attribute(attribute)
+ end
+ end
+ end
+ end
+
+ def cache_attribute_key(key)
+ "#{self.class.name}:attributes:#{self.id}:#{key}"
+ end
+
+ def cached_attribute(key)
+ Gitlab::Redis::SharedState.with do |redis|
+ redis.get(cache_attribute_key(key))
+ end
+ end
+
+ def cache_attributes(values)
+ Gitlab::Redis::SharedState.with do |redis|
+ values.each do |key, value|
+ redis.set(cache_attribute_key(key), value, ex: 24.hours)
+ end
+ end
+ end
+end