Class: Farce::Unsafe::LRUMap

Inherits:
Abstract::LRUMap show all
Includes:
Farce::Unshareable
Defined in:
lib/farce/unsafe/lru_map.rb

Overview

Note:

Instances of this class are not thread-safe and should not be shared concurrently. This does not just apply to Ractors, but also Threads and possibly Fibers.

Only use them if you are absolutely certain of this and they do not leave a tightly controlled scope. They can provide a memory or performance advantage over their thread-safe counterparts, so they might be useful for hot path optimizations.

Concurrent access, especially modifications, may corrupt the internal state of the object.

An LRU map for caches whose caller provides exclusive access to composed operations. Individual storage operations retain the engine backend's integrity guard. Cache loading does not coordinate competing callers, including callers loading the same key.

Instance Method Summary collapse

Methods included from Farce::Unshareable

#initialize, #ractor_shareable?

Methods inherited from Abstract::BoundedMap

#[], #clear, #compare_keys_by_identity?, #compare_values_by_identity?, #delete, #each, #each_key, #each_value, #fetch, #getkey, #initialize, #key?, #keys, #max_size, #max_size=, #prune, #shift, #size, #values

Methods included from Abstract::DuplicableMap

#compact, #compact_blank, #deep_dup, #duplicable?, #except, #flatten, #invert, #merge, #reject, #reverse_merge, #select, #slice, #stringify_keys, #symbolize_keys, #to_proc, #transform_keys, #transform_values, #with_indifferent_access

Methods inherited from Abstract::Map

#[], #as_extended_json, #as_json, #assert_valid_keys, #assoc, #bson_type, #clear, #compare_by_identity?, #compare_keys_by_identity?, #compare_values_by_identity?, #deconstruct_keys, #delete, #dig, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #fetch_values, #getkey, #has_key?, #key, #key?, #keys, #length, #rassoc, #shareable_keys?, #shareable_values?, #size, #store, #to_a, #to_bson, #to_bson_normalized_value, #to_cbor, #to_h, #to_hash, #to_json, #to_msgpack, #to_query, #to_s, #value?, #values_at, #weak_keys?, #weak_values?

Instance Method Details

#[]=(key, value) ⇒ BasicObject?

This method is abstract.

Look up a key without waiting for atomic-update access.

Parameters:

  • key (BasicObject) —

    The key to look up.

Returns:

  • (BasicObject, nil) —

    The associated value, or nil if the key is absent.



24
25
26
27
# File 'lib/farce/unsafe/lru_map.rb', line 24

def []=(key, value)
  internal_map[prepare_key(key)] = value
  value
end

#store_if_absent(key) ⇒ BasicObject

Read an existing value or construct and store a value for an absent key. Competing callers can run the loader concurrently, including for equal keys. The caller must provide exclusive access when a single initialization is required.

Parameters:

  • key (BasicObject) —

    The key to retrieve or initialize.

Yield Returns:

  • (BasicObject) —

    The value to store.

Returns:

  • (BasicObject) —

    The existing or newly stored value.

Raises:

  • (LocalJumpError) —

    If no block is given.



# File 'lib/farce/unsafe/lru_map.rb', line 14