class AsyncStorage::Repo

Attributes

resolver_class[R]

Public Class Methods

ack(klass:, args:, **options) click to toggle source

Store a fresh content into redis. This method is invoked by a background job.

@param options [Hash] List of options to be passed along the initializer @option [Class] klass The resolver class @option [Array] args An array with the resolver arguments @return [Hash, NilClass] the result from class resolver

# File lib/async_storage/repo.rb, line 25
def self.ack(klass:, args:, **options)
  new(klass, **options).refresh!(*args)
end
new(resolver_class, **options) click to toggle source

@param resolver_class [Class] A class with the call method @param options [Hash] A hash with config @option expires_in [Nil, Integer] Time in seconds @raise [ArgumentError] When the resolver_class does not respond with `call' instance method

# File lib/async_storage/repo.rb, line 13
def initialize(resolver_class, **options)
  validate_resolver_class!(resolver_class)
  @resolver_class = resolver_class
  @options = options
end

Public Instance Methods

alloc(*args) click to toggle source

Build an Allocator instance

@param [*Array] list of parameters to be forwaded to the resolver#call

# File lib/async_storage/repo.rb, line 95
def alloc(*args)
  Allocator.new(self, *args)
end
exist?(*args) click to toggle source

Check if a fresh value exist.

@return [Boolean] True or False according the object existence

# File lib/async_storage/repo.rb, line 74
def exist?(*args)
  alloc(*args).exist?
end
expires_in() click to toggle source
# File lib/async_storage/repo.rb, line 99
def expires_in
  @options[:expires_in] || AsyncStorage.config.expires_in
end
fresh?(*args) click to toggle source

Check if a fresh object exists into the storage

@return [Boolean] true/false according to the object existence and freshness

# File lib/async_storage/repo.rb, line 88
def fresh?(*args)
  alloc(*args).fresh?
end
get(*args) click to toggle source

Async get value with a given key

@return [Object, NilClass] Return both stale or fresh object. If does not exist async call the retriever and return nil

# File lib/async_storage/repo.rb, line 32
def get(*args)
  alloc(*args).get
end
get!(*args) click to toggle source

Sync get value with a given value

@return [Object] Return the result from resolver

# File lib/async_storage/repo.rb, line 39
def get!(*args)
  alloc(*args).get!
end
invalidate(*args) click to toggle source

Expire object the object with a given key. The stale object will not be removed

@return [Boolean] True or False according to the object existence

# File lib/async_storage/repo.rb, line 46
def invalidate(*args)
  alloc(*args).invalidate
end
invalidate!(*args) click to toggle source

Delete object with a given key.

@return [Boolean] True or False according to the object existence

# File lib/async_storage/repo.rb, line 53
def invalidate!(*args)
  alloc(*args).invalidate!
end
namespace() click to toggle source
# File lib/async_storage/repo.rb, line 103
def namespace
  @options[:namespace]
end
refresh(*args) click to toggle source

Invalidate object with the given key and update content according to the strategy

@return [Object, NilClass] Stale object or nil when it does not exist

# File lib/async_storage/repo.rb, line 60
def refresh(*args)
  alloc(*args).refresh
end
refresh!(*args) click to toggle source

Fetch data from resolver and store it into redis

@return [Object] Return the result from resolver

# File lib/async_storage/repo.rb, line 67
def refresh!(*args)
  alloc(*args).refresh!
end
stale?(*args) click to toggle source

Check if object with a given key is stale

@return [NilClass, Boolean] Return nil if the object does not exist or true/false according to the object freshness state

# File lib/async_storage/repo.rb, line 81
def stale?(*args)
  alloc(*args).stale?
end

Protected Instance Methods

validate_resolver_class!(klass) click to toggle source
# File lib/async_storage/repo.rb, line 109
def validate_resolver_class!(klass)
  unless klass.is_a?(Class)
    raise(ArgumentError, format('%<c>p is not a valid resolver class.', c: klass))
  end

  unless klass.instance_methods.include?(:call)
    raise(ArgumentError, format('%<c>p must have call instance method.', c: klass))
  end

  true
end