Commit 82e0b9c3 authored by Sean Arnold's avatar Sean Arnold Committed by Bob Van Landuyt

Add specs for reactive cache set cache spec

Rename file too
parent d2166488
# frozen_string_literal: true
# Interface to the Redis-backed cache store to keep track of complete cache keys
# for a ReactiveCache resource.
module Gitlab
class ReactiveCacheSetCache < Gitlab::SetCache
attr_reader :expires_in
def initialize(expires_in: 10.minutes)
@expires_in = expires_in
end
def clear_cache!(key)
with do |redis|
keys = read(key).map { |value| "#{cache_type}#{value}" }
keys << cache_key(key)
redis.pipelined do
keys.each_slice(1000) { |subset| redis.del(*subset) }
end
end
end
private
def cache_type
"#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:"
end
end
end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Interface to the Redis-backed cache store for keys that use a Redis set # Interface to the Redis-backed cache store for keys that use a Redis set
module Gitlab module Gitlab
class RepositorySetCache class RepositorySetCache < Gitlab::SetCache
attr_reader :repository, :namespace, :expires_in attr_reader :repository, :namespace, :expires_in
def initialize(repository, extra_namespace: nil, expires_in: 2.weeks) def initialize(repository, extra_namespace: nil, expires_in: 2.weeks)
...@@ -17,18 +17,6 @@ module Gitlab ...@@ -17,18 +17,6 @@ module Gitlab
"#{type}:#{namespace}:set" "#{type}:#{namespace}:set"
end end
def expire(key)
with { |redis| redis.del(cache_key(key)) }
end
def exist?(key)
with { |redis| redis.exists(cache_key(key)) }
end
def read(key)
with { |redis| redis.smembers(cache_key(key)) }
end
def write(key, value) def write(key, value)
full_key = cache_key(key) full_key = cache_key(key)
...@@ -54,15 +42,5 @@ module Gitlab ...@@ -54,15 +42,5 @@ module Gitlab
write(key, yield) write(key, yield)
end end
end end
def include?(key, value)
with { |redis| redis.sismember(cache_key(key), value) }
end
private
def with(&blk)
Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
end
end end
end end
# frozen_string_literal: true
# Interface to the Redis-backed cache store to keep track of complete cache keys
# for a ReactiveCache resource.
module Gitlab
class SetCache
attr_reader :expires_in
def initialize(expires_in: 2.weeks)
@expires_in = expires_in
end
def cache_key(key)
"#{key}:set"
end
def expire(key)
with { |redis| redis.del(cache_key(key)) }
end
def exist?(key)
with { |redis| redis.exists(cache_key(key)) }
end
def write(key, value)
with do |redis|
redis.pipelined do
redis.sadd(cache_key(key), value)
redis.expire(cache_key(key), expires_in)
end
end
value
end
def read(key)
with { |redis| redis.smembers(cache_key(key)) }
end
def include?(key, value)
with { |redis| redis.sismember(cache_key(key), value) }
end
def ttl(key)
with { |redis| redis.ttl(cache_key(key)) }
end
private
def with(&blk)
Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::ReactiveCacheSetCache, :clean_gitlab_redis_cache do
let_it_be(:project) { create(:project) }
let(:cache_prefix) { 'cache_prefix' }
let(:expires_in) { 10.minutes }
let(:cache) { described_class.new(expires_in: expires_in) }
describe '#cache_key' do
subject { cache.cache_key(cache_prefix) }
it 'includes the suffix' do
expect(subject).to eq "#{cache_prefix}:set"
end
end
describe '#read' do
subject { cache.read(cache_prefix) }
it { is_expected.to be_empty }
context 'after item added' do
before do
cache.write(cache_prefix, 'test_item')
end
it { is_expected.to contain_exactly('test_item') }
end
end
describe '#write' do
it 'writes the value to the cache' do
cache.write(cache_prefix, 'test_item')
expect(cache.read(cache_prefix)).to contain_exactly('test_item')
end
it 'sets the expiry of the set' do
cache.write(cache_prefix, 'test_item')
expect(cache.ttl(cache_prefix)).to be_within(1).of(expires_in.seconds)
end
end
describe '#clear_cache!', :use_clean_rails_redis_caching do
it 'deletes the cached items' do
# Cached key and value
Rails.cache.write('test_item', 'test_value')
# Add key to set
cache.write(cache_prefix, 'test_item')
expect(cache.read(cache_prefix)).to contain_exactly('test_item')
cache.clear_cache!(cache_prefix)
expect(cache.read(cache_prefix)).to be_empty
end
end
describe '#include?' do
subject { cache.include?(cache_prefix, 'test_item') }
it { is_expected.to be(false) }
context 'item added' do
before do
cache.write(cache_prefix, 'test_item')
end
it { is_expected.to be(true) }
end
end
end
...@@ -21,6 +21,21 @@ RSpec.configure do |config| ...@@ -21,6 +21,21 @@ RSpec.configure do |config|
ActionController::Base.cache_store = caching_store ActionController::Base.cache_store = caching_store
end end
config.around(:each, :use_clean_rails_redis_caching) do |example|
original_null_store = Rails.cache
caching_config_hash = Gitlab::Redis::Cache.params
caching_config_hash[:namespace] = Gitlab::Redis::Cache::CACHE_NAMESPACE
Rails.cache = ActiveSupport::Cache::RedisCacheStore.new(caching_config_hash)
redis_cache_cleanup!
example.run
redis_cache_cleanup!
Rails.cache = original_null_store
end
config.around(:each, :use_sql_query_cache) do |example| config.around(:each, :use_sql_query_cache) do |example|
ActiveRecord::Base.cache do ActiveRecord::Base.cache do
example.run example.run
......
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