Module: Redstruct::Factory::Creation

Included in:
Redstruct::Factory
Defined in:
lib/redstruct/factory/creation.rb

Overview

Module to hold all the factory creation methods.

Instance Method Summary collapse

Instance Method Details

#counter(key, **options) ⇒ Redstruct::Types::Counter

Builds a Redis counter struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



60
61
62
# File 'lib/redstruct/factory/creation.rb', line 60

def counter(key, **options)
  return create(Redstruct::Types::Counter, key, **options)
end

#factory(namespace) ⇒ Factory

Returns a factory with an isolated namespace.

Examples:

Given a factory ‘f` with namespace fact:first

f.factory('second') # => Redstruct::Factory: namespace: <"fact:first:second">, script_cache: <[]>

Returns:



85
86
87
# File 'lib/redstruct/factory/creation.rb', line 85

def factory(namespace)
  return self.class.new(connection: @connection, namespace: isolate(namespace))
end

#hash(key, **options) ⇒ Redstruct::Types::Hash

Builds a Redis hash struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



46
47
48
# File 'lib/redstruct/factory/creation.rb', line 46

def hash(key, **options)
  return create(Redstruct::Types::Hash, key, **options)
end

#list(key, **options) ⇒ Redstruct::Types::List

Builds a Redis list struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



25
26
27
# File 'lib/redstruct/factory/creation.rb', line 25

def list(key, **options)
  return create(Redstruct::Types::List, key, **options)
end

#lock(key, **options) ⇒ Redstruct::Hls::Lock

Builds a Redis backed lock from the key

Parameters:

  • key (::String)

    base key to use

Returns:



53
54
55
# File 'lib/redstruct/factory/creation.rb', line 53

def lock(key, **options)
  return create(Redstruct::Hls::Lock, key, **options)
end

#queue(key) ⇒ Redstruct::Hls::Queue

Builds a Redis backed queue from the key

Parameters:

  • key (::String)

    base key to use

Returns:



67
68
69
# File 'lib/redstruct/factory/creation.rb', line 67

def queue(key)
  return create(Redstruct::Hls::Queue, key)
end

#script(id, script) ⇒ Object

TODO:

The script cache is actually based on the database you will connect to. Therefore, it might be smarter to move it to the connection used?

Caveat: if the script with the given ID exists in the cache, we don’t bother updating it. So if the script actually changed since the first call, the one sent during the first call will



74
75
76
77
78
79
# File 'lib/redstruct/factory/creation.rb', line 74

def script(id, script)
  return @script_cache.synchronize do
    @script_cache[id] = Redstruct::Types::Script.new(key: id, script: script, factory: self) if @script_cache[id].nil?
    @script_cache[id]
  end
end

#set(key, **options) ⇒ Redstruct::Types::Set

Builds a Redis set struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



32
33
34
# File 'lib/redstruct/factory/creation.rb', line 32

def set(key, **options)
  return create(Redstruct::Types::Set, key, **options)
end

#sorted_set(key, **options) ⇒ Redstruct::Types::SortedSet

Builds a Redis sorted set (zset) struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



39
40
41
# File 'lib/redstruct/factory/creation.rb', line 39

def sorted_set(key, **options)
  return create(Redstruct::Types::SortedSet, key, **options)
end

#string(key, **options) ⇒ Redstruct::Types::String

Builds a Redis string struct from the key

Parameters:

  • key (::String)

    base key to use

Returns:



18
19
20
# File 'lib/redstruct/factory/creation.rb', line 18

def string(key, **options)
  return create(Redstruct::Types::String, key, **options)
end

#struct(key, **options) ⇒ Redstruct::Types::Struct

Builds a struct with the given key (namespaced) and sharing the factory connection Building a struct is only really useful if you plan on making only basic operations, such as delete, expire, etc. It is however recommended to always build your objects in the same way, e.g. if it’s a lock, use Factory#lock

Parameters:

  • key (::String)

    base key to use

Returns:



11
12
13
# File 'lib/redstruct/factory/creation.rb', line 11

def struct(key, **options)
  return create(Redstruct::Types::Struct, key, **options)
end