class Redstruct::Factory
Main interface of the gem; this class should be used to build all Redstruct
objects, even when deserializing them.
Attributes
@return [Redstruct::ConnectionProxy] connection proxy used to execute commands
@return [String] namespace used to prefix the keys of all objects created by this factory
Public Class Methods
@param [Redstruct::ConnectionProxy] connection connection to use for all objects built by the factory @param [String] namespace optional; all objects built from the factory will have their keys prefixed with this @raise [ArgumentError] raised if connection is not nil, and not a Redstruct::ConnectionProxy
@return [Redstruct::Factory]
# File lib/redstruct/factory.rb, line 30 def initialize(connection: nil, namespace: nil) namespace ||= Redstruct.config.default_namespace connection ||= Redstruct::ConnectionProxy.new(Redstruct.config.default_connection) raise ArgumentError, 'connection should be a Redstruct::ConnectionProxy' unless connection.is_a?(Redstruct::ConnectionProxy) @connection = connection @namespace = namespace.to_s end
Public Instance Methods
Deletes all keys created by the factory. By defaults will iterate at most of 500 million keys @param [Hash] options accepts the options as given in each @see Redstruct::Factory#each
# File lib/redstruct/factory.rb, line 60 def delete(options = {}) return each({ match: '*', count: 500, max_iterations: 1_000_000, batch_size: 500 }.merge(options)) do |keys| @connection.del(*keys) end end
Returns a factory for a sub namespace. @example Given a factory `f` with namespace fact:first
f.factory('second') # => Redstruct::Factory: namespace: <"fact:first:second">>
@return [Factory] namespaced factory
# File lib/redstruct/factory.rb, line 72 def factory(namespace) return self.class.new(connection: @connection, namespace: prefix(namespace)) end
@!visibility private
# File lib/redstruct/factory.rb, line 120 def inspectable_attributes return { namespace: @namespace, connection: @connection } end
Creates a lock for the given resource within this factory @see Redstruct::Lock#new @return [Redstruct::Lock] lock for the given resource within this factory
# File lib/redstruct/factory.rb, line 86 def lock(resource, **options) return Redstruct::Lock.new(resource, factory: self, **options) end
Returns a namespaced version of the key (unless already namespaced) @param [String] key the key to namespace @return [String] namespaced version of the key (or the key itself if already namespaced)
# File lib/redstruct/factory.rb, line 43 def prefix(key) prefixed = key prefixed = "#{@namespace}:#{key}" unless @namespace.empty? || key.start_with?("#{@namespace}:") return prefixed end
Creates using this factory's connection @see Redstruct::Script#new @return [Redstruct::Script] script sharing the factory connection
# File lib/redstruct/factory.rb, line 79 def script(script, **options) return Redstruct::Script.new(script: script, connection: @connection, **options) end
Use redis-rb scan_each method to iterate over particular keys @ @return [Enumerator] base enumerator to iterate of the namespaced keys
# File lib/redstruct/factory.rb, line 53 def to_enum(match: '*', count: 10) return @connection.scan_each(match: prefix(match), count: count) end