Class: Familia::Lock

Inherits:
StringKey show all
Defined in:
lib/familia/data_type/types/lock.rb

Instance Attribute Summary collapse

Attributes included from Settings

#current_key_version, #default_expiration, #delim, #encryption_keys, #encryption_personalization, #logical_database, #prefix, #suffix, #transaction_mode

Instance Method Summary collapse

Methods included from Features::Autoloader

autoload_files, included, normalize_to_config_name

Methods included from DataType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value

Methods included from DataType::DatabaseCommands

#current_expiration, #delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #type

Methods included from DataType::Connection

#dbclient, #dbkey, #direct_access

Methods included from Settings

#configure, #default_suffix, #pipeline_mode, #pipeline_mode=

Methods included from Base

add_feature, #as_json, #expired?, #expires?, find_feature, #generate_id, #to_json, #to_s, #ttl, #update_expiration, #uuid

Constructor Details

#initialize(*args) ⇒ Lock

Returns a new instance of Lock.



5
6
7
8
# File 'lib/familia/data_type/types/lock.rb', line 5

def initialize(*args)
  super
  @opts[:default] = nil
end

Instance Attribute Details

#features_enabledObject (readonly) Originally defined in module Features

Returns the value of attribute features_enabled.

#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

#parentObject Originally defined in module DataType::ClassMethods

Returns the value of attribute parent.

#prefixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute prefix.

#suffixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute suffix.

#uri(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute uri.

Instance Method Details

#acquire(token = SecureRandom.uuid, ttl: 10) ⇒ String, false

Acquire a lock with optional TTL

Parameters:

  • token (String) (defaults to: SecureRandom.uuid)

    Unique token to identify lock holder (auto-generated if nil)

  • ttl (Integer, nil) (defaults to: 10)

    Time-to-live in seconds. nil = no expiration, <=0 rejected

Returns:

  • (String, false)

    Returns token if acquired successfully, false otherwise



14
15
16
17
18
19
20
21
22
# File 'lib/familia/data_type/types/lock.rb', line 14

def acquire(token = SecureRandom.uuid, ttl: 10)
  success = setnx(token)
  # Handle both integer (1/0) and boolean (true/false) return values
  return false unless [1, true].include?(success)
  return del && false if ttl&.<=(0)
  return del && false if ttl&.positive? && !expire(ttl)

  token
end

#force_unlock!Object



38
39
40
# File 'lib/familia/data_type/types/lock.rb', line 38

def force_unlock!
  del
end

#held_by?(token) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/familia/data_type/types/lock.rb', line 34

def held_by?(token)
  value == token
end

#locked?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/familia/data_type/types/lock.rb', line 30

def locked?
  !value.nil?
end

#release(token) ⇒ Object



24
25
26
27
28
# File 'lib/familia/data_type/types/lock.rb', line 24

def release(token)
  # Lua script to atomically check token and delete
  script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"
  dbclient.eval(script, [dbkey], [token]) == 1
end