Class: Sidekiq::AttrPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/attr_package.rb

Constant Summary collapse

REDIS_NAMESPACE =
'sidekiq_attr_package'

Class Method Summary collapse

Class Method Details

.create(expires_in: 7.days, **attrs) ⇒ String

Create a new attribute package

Parameters:

  • expires_in (Integer) (defaults to: 7.days)

    the expiration time in days

  • attrs (Hash)

    the attributes to be stored

Returns:

  • (String)

    the key of the stored attributes



12
13
14
15
16
17
18
19
20
21
# File 'lib/sidekiq/attr_package.rb', line 12

def create(expires_in: 7.days, **attrs)
  json_attrs = attrs.to_json
  key = Digest::SHA256.hexdigest(json_attrs)

  redis.set(key, json_attrs, ex: expires_in)

  key
rescue => e
  raise AttrPackageError.new('create', e.message)
end

.find(key) ⇒ Hash?

Find an attribute package by key

Parameters:

  • key (String)

    the key of the attribute package

Returns:

  • (Hash, nil)

    the found attributes, or nil if not found



26
27
28
29
30
31
32
33
34
35
# File 'lib/sidekiq/attr_package.rb', line 26

def find(key)
  json_value = redis.get(key)
  return nil unless json_value

  JSON.parse(json_value, symbolize_names: true).tap do
    redis.del(key)
  end
rescue => e
  raise AttrPackageError.new('find', e.message)
end