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 = SecureRandom.hex(32)

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

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

.delete(key) ⇒ Integer

Delete an attribute package by key

Parameters:

  • key (String)

    the key of the attribute package

Returns:

  • (Integer)

    the number of keys deleted



38
39
40
41
42
# File 'lib/sidekiq/attr_package.rb', line 38

def delete(key)
  redis.del(key)
rescue => e
  raise AttrPackageError.new('delete', 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
# 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)
rescue => e
  raise AttrPackageError.new('find', e.message)
end

.redisObject (private)



46
47
48
# File 'lib/sidekiq/attr_package.rb', line 46

def redis
  @redis ||= Redis::Namespace.new(REDIS_NAMESPACE, redis: $redis) # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
end