123456789_123456789_123456789_123456789_123456789_

Module: Redis::Commands::Bless

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/redis/commands/bless.rb

Overview

The BLESS command family (Redis 8.12): per-key protection flags that shield a key from memory-pressure actions. The only flag so far is NO-EVICT, which excludes the key from the eviction policy.

Flags are passed to the server verbatim: no client-side validation or normalization is done, so spell them the way the server does ("NO-EVICT"). An unknown flag is rejected by the server with a ::Redis::CommandError.

Instance Method Summary

Instance Method Details

#bless_clear(key, flag) ⇒ Boolean

Remove a protection flag from a key.

Examples:

redis.bless_clear("foo", "NO-EVICT")
  # => true
redis.bless_clear("foo", "NO-EVICT")
  # => false

Parameters:

  • key (String)
  • flag (String) —

    the protection flag, e.g. "NO-EVICT"

Returns:

  • (Boolean) —

    whether the key's flag set changed (false if the flag was already clear)

[ GitHub ]

  
# File 'lib/redis/commands/bless.rb', line 57

def bless_clear(key, flag)
  send_command([:bless, "CLEAR", key, flag], &Boolify)
end

#bless_get(key) ⇒ Array<String>

Return the active protection flags of a key.

Examples:

redis.set("foo", "bar")
redis.bless_set("foo", "NO-EVICT")
redis.bless_get("foo")
  # => ["NO-EVICT"]

Parameters:

  • key (String)

Returns:

  • (Array<String>) —

    the key's active flags (e.g. "NO-EVICT"); empty when none are set

[ GitHub ]

  
# File 'lib/redis/commands/bless.rb', line 25

def bless_get(key)
  send_command([:bless, "GET", key])
end

#bless_scan(cursor, flag, count: nil) ⇒ Array(String, Array<String>)

Note:

On Redis::Cluster the driver walks the nodes one by one, as it does for SCAN, encoding the node index into the returned cursor, so a full iteration covers every shard. Inside pipelined/multi the command is sent to a single node instead and only that node's keys are returned.

Incrementally iterate the keys of the current database that carry the given protection flag. Like SCAN, a full iteration is complete when the returned cursor is "0".

Examples:

Retrieve the first batch of keys blessed with NO-EVICT

redis.bless_scan(0, "NO-EVICT")
  # => ["17", ["key:1", "key:2"]]

Hint the batch size

redis.bless_scan(17, "NO-EVICT", count: 100)
  # => ["0", ["key:3"]]

Parameters:

  • cursor (String, Integer) —

    the cursor of the iteration

  • flag (String) —

    the protection flag, e.g. "NO-EVICT"

  • options (Hash)

Returns:

  • (Array(String, Array<String>)) —

    the next cursor and the keys found

[ GitHub ]

  
# File 'lib/redis/commands/bless.rb', line 83

def bless_scan(cursor, flag, count: nil)
  args = [:bless, "SCAN", cursor, flag]
  args << "COUNT" << Integer(count) if count

  send_command(args)
end

#bless_scan_each(flag, **options, &block) ⇒ Enumerator

Iterate every key of the current database that carries the given protection flag, driving #bless_scan until the cursor wraps around.

Examples:

Collect all keys blessed with NO-EVICT (with possible duplicates)

redis.bless_scan_each("NO-EVICT").to_a
  # => ["key:1", "key:2", "key:3"]

Execute a block for each blessed key

redis.bless_scan_each("NO-EVICT", count: 100) { |key| puts key }

Parameters:

  • flag (String) —

    the protection flag, e.g. "NO-EVICT"

  • options (Hash)

Options Hash (**options):

  • :count (Integer) —

    return about this many keys per call

Returns:

  • (Enumerator) —

    an enumerator over all found keys when no block is given

[ GitHub ]

  
# File 'lib/redis/commands/bless.rb', line 103

def bless_scan_each(flag, **options, &block)
  return to_enum(:bless_scan_each, flag, **options) unless block_given?

  cursor = 0
  loop do
    cursor, keys = bless_scan(cursor, flag, **options)
    keys.each(&block)
    break if cursor == "0"
  end
end

#bless_set(key, flag) ⇒ Boolean

Add a protection flag to a key.

Examples:

redis.bless_set("foo", "NO-EVICT")
  # => true
redis.bless_set("foo", "NO-EVICT")
  # => false

Parameters:

  • key (String)
  • flag (String) —

    the protection flag, e.g. "NO-EVICT"

Returns:

  • (Boolean) —

    whether the key's flag set changed (false if the flag was already set)

[ GitHub ]

  
# File 'lib/redis/commands/bless.rb', line 41

def bless_set(key, flag)
  send_command([:bless, "SET", key, flag], &Boolify)
end