Module: Familia::DataType::DatabaseCommands

Included in:
Familia::DataType
Defined in:
lib/familia/data_type/database_commands.rb

Overview

Must be included in all DataType classes to provide Valkey/Redis commands. The class must have a dbkey method.

Instance Method Summary collapse

Instance Method Details

#current_expirationObject



49
50
51
# File 'lib/familia/data_type/database_commands.rb', line 49

def current_expiration
  dbclient.ttl dbkey
end

#delete!Object Also known as: clear

Deletes the entire dbkey

We return the dbclient.del command's return value instead of a friendly boolean b/c that logic doesn't work inside of a transaction. The return value in that case is a Redis::Future which based on the name indicates that the commend hasn't even run yet.



32
33
34
35
# File 'lib/familia/data_type/database_commands.rb', line 32

def delete!
  Familia.trace :DELETE!, nil, self.class.uri if Familia.debug?
   dbclient.del dbkey
end

#echo(*args) ⇒ Object



65
66
67
# File 'lib/familia/data_type/database_commands.rb', line 65

def echo(*args)
  dbclient.echo "[#{self.class}] #{args.join(' ')} (#{opts&.fetch(:class, '<no opts>')})"
end

#exists?Boolean

EXISTS returns an Integer match count (0 for a missing key). The prior dbclient.exists(dbkey) && !size.zero? never short-circuited, since 0 is truthy in Ruby -- so existence was actually decided by the size check, which some scalar types (see StringKey#char_count) computed from a never-nil display string and so got wrong for a missing key. Familia.positive? coerces the count to a real boolean, and passes a Redis::Future through untouched inside a pipeline/transaction.

Returns:

  • (Boolean)


45
46
47
# File 'lib/familia/data_type/database_commands.rb', line 45

def exists?
  Familia.positive?(dbclient.exists(dbkey))
end

#expire(sec) ⇒ Object



53
54
55
# File 'lib/familia/data_type/database_commands.rb', line 53

def expire(sec)
  dbclient.expire dbkey, sec.to_i
end

#expireat(unixtime) ⇒ Object



57
58
59
# File 'lib/familia/data_type/database_commands.rb', line 57

def expireat(unixtime)
  dbclient.expireat dbkey, unixtime
end

#move(logical_database) ⇒ Object



10
11
12
# File 'lib/familia/data_type/database_commands.rb', line 10

def move(logical_database)
  dbclient.move dbkey, logical_database
end

#persistObject



61
62
63
# File 'lib/familia/data_type/database_commands.rb', line 61

def persist
  dbclient.persist dbkey
end

#rename(newkey) ⇒ Object



14
15
16
# File 'lib/familia/data_type/database_commands.rb', line 14

def rename(newkey)
  dbclient.rename dbkey, newkey
end

#renamenx(newkey) ⇒ Object



18
19
20
# File 'lib/familia/data_type/database_commands.rb', line 18

def renamenx(newkey)
  dbclient.renamenx dbkey, newkey
end

#typeObject



22
23
24
# File 'lib/familia/data_type/database_commands.rb', line 22

def type
  dbclient.type dbkey
end