Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/any_cache/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ def build(driver)
when ActiveSupportDalliStore.supported_driver?(driver) then ActiveSupportDalliStore.new(driver)
when Delegator.supported_driver?(driver) then Delegator.new(driver)
else
raise AnyCache::UnsupportedDriverError
# Explicit check because NullStore isn't loaded by default
if defined?(::AnyCache::Adapters::NullStore) && ::AnyCache::Adapters::NullStore.supported_driver?(driver)
NullStore.new(driver)
else
raise AnyCache::UnsupportedDriverError
end
end
end
# rubocop:enable Metrics/LineLength, Metrics/AbcSize
Expand Down
191 changes: 191 additions & 0 deletions lib/any_cache/adapters/null_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# frozen_string_literal: true

module AnyCache::Adapters
# @api private
# @since 0.1.0
class NullStore
# @since 0.1.0
extend Forwardable
# @since 0.4.0
include AnyCache::Dumper::InterfaceAccessMixin

class << self
# @param driver [Object]
# @return [Boolean]
#
# @api private
# @since 0.1.0
def supported_driver?(driver)
driver.nil?
end
end

# @return [Object]
#
# @api private
# @since 0.1.0
attr_reader :driver

# @param driver [Object]
# @return [void]
#
# @api private
# @since 0.1.0
def initialize(driver)
@driver = driver
end

# @param key [String]
# @param options [Hash]
# @return [Object]
#
# @api private
# @since 0.1.0
def read(key, **options)
# no-op
end

# @param keys [Array<String>]
# @param options [Hash]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
# no-op
end

# @param key [String]
# @param value [Object]
# @param options [Hash]
# @return [void]
#
# @api private
# @sinc 0.1.0
def write(key, value, **options)
# no-op
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
# no-op
end

# @param key [String]
# @param options [Hash]
# @param fallback [Proc]
# @return [Object]
#
# @api private
# @since 0.2.0
def fetch(key, **options, &fallback)
# no-op
end

# @param keys [Array<String>]
# @param options [Hash]
# @param fallback [Proc]
# @return [Hash]
#
# @api private
# @since 0.3.0
def fetch_multi(*keys, **options, &fallback)
# no-op
end

# @param key [String]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.1.0
def delete(key, **options)
# no-op
end

# @param pattern [Regexp, String, Object]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def delete_matched(pattern, **options)
# no-op
end

# @param key [String]
# @param value [Integer, Float]
# @param options [Hash]
# @return [Integer, Float]
#
# @api private
# @sinc 0.1.0
def increment(key, value, **options)
value
end

# @param key [String]
# @param value [Integer, Float]
# @param options [Hash]
# @return [Integer, Float]
#
# @api private
# @since 0.1.0
def decrement(key, value, **options)
value
end

# @param key [String]
# @option expires_in [Integer]
# @return [void]
#
# @api private
# @since 0.1.0
def expire(key, expires_in:)
# no-op
end

# @param key [String]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.1.0
def persist(key, **options)
# no-op
end

# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.1.0
def clear(**options)
# no-op
end

# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.4.0
def cleanup(**options)
# no-op
end

# @param key [String]
# @param options [Hash]
# @return [Boolean]
#
# @api private
# @since 0.2.0
def exist?(key, **options)
false
end
end
end
17 changes: 17 additions & 0 deletions spec/lib/any_cache/adapters/null_store_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "spec_helper"

describe "::AnyCache::Adapters::NullStore" do
it "has a null store if it's been explicitly loaded" do
load "any_cache/adapters/null_store.rb"

expect(AnyCache.build(nil).adapter).to be_instance_of(AnyCache::Adapters::NullStore)
end

it "doesn't load unless it's been explicitly loaded" do
AnyCache::Adapters.send(:remove_const, :NullStore) if AnyCache::Adapters.constants.include?(:NullStore)

expect { AnyCache.build(nil).adapter }.to raise_error(AnyCache::UnsupportedDriverError)
end
end