Commit 6f5b68c5 authored by Douwe Maan's avatar Douwe Maan

Merge branch '1983-geo-enabled-cache' into 'master'

Cache Gitlab::Geo queries

Closes #1983

See merge request !1507
parents 1b88d48d f2f0d6da
...@@ -22,7 +22,9 @@ class GeoNode < ActiveRecord::Base ...@@ -22,7 +22,9 @@ class GeoNode < ActiveRecord::Base
after_initialize :build_dependents after_initialize :build_dependents
after_save :refresh_bulk_notify_worker_status after_save :refresh_bulk_notify_worker_status
after_save :expire_cache!
after_destroy :refresh_bulk_notify_worker_status after_destroy :refresh_bulk_notify_worker_status
after_destroy :expire_cache!
before_validation :update_dependents_attributes before_validation :update_dependents_attributes
before_validation :ensure_access_keys! before_validation :ensure_access_keys!
...@@ -160,4 +162,8 @@ class GeoNode < ActiveRecord::Base ...@@ -160,4 +162,8 @@ class GeoNode < ActiveRecord::Base
self.system_hook.push_events = true self.system_hook.push_events = true
self.system_hook.tag_push_events = true self.system_hook.tag_push_events = true
end end
def expire_cache!
Gitlab::Geo.expire_cache!
end
end end
---
title: Cache Gitlab::Geo queries
merge_request: 1507
author:
...@@ -2,6 +2,16 @@ module Gitlab ...@@ -2,6 +2,16 @@ module Gitlab
module Geo module Geo
OauthApplicationUndefinedError = Class.new(StandardError) OauthApplicationUndefinedError = Class.new(StandardError)
CACHE_KEYS = %i[
geo_primary_node
geo_secondary_nodes
geo_node_enabled
geo_node_primary
geo_node_secondary
geo_primary_ssh_path_prefix
geo_oauth_application
].freeze
def self.current_node def self.current_node
self.cache_value(:geo_node_current) do self.cache_value(:geo_node_current) do
GeoNode.find_by(host: Gitlab.config.gitlab.host, GeoNode.find_by(host: Gitlab.config.gitlab.host,
...@@ -80,7 +90,19 @@ module Gitlab ...@@ -80,7 +90,19 @@ module Gitlab
def self.cache_value(key, &block) def self.cache_value(key, &block)
return yield unless RequestStore.active? return yield unless RequestStore.active?
RequestStore.fetch(key) { yield } # We need a short expire time as we can't manually expire on a secondary node
RequestStore.fetch(key) { Rails.cache.fetch(key, expires_in: 15.seconds) { yield } }
end
def self.expire_cache!
return true unless RequestStore.active?
CACHE_KEYS.each do |key|
Rails.cache.delete(key)
RequestStore.delete(key)
end
true
end end
def self.generate_access_keys def self.generate_access_keys
......
...@@ -95,6 +95,22 @@ describe GeoNode, type: :model do ...@@ -95,6 +95,22 @@ describe GeoNode, type: :model do
end end
end end
context 'cache expiration' do
let(:new_node) { FactoryGirl.build(:geo_node) }
it 'expires cache when saved' do
expect(new_node).to receive(:expire_cache!)
new_node.save!
end
it 'expires cache when removed' do
expect(node).to receive(:expire_cache!) # 1 for creation 1 for deletion
node.destroy
end
end
describe '#uri' do describe '#uri' do
context 'when all fields are filled' do context 'when all fields are filled' do
it 'returns an URI object' do it 'returns an URI object' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment