Class: Neo4j::Driver::Internal::HomeDbCache

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/driver/internal/home_db_cache.rb

Overview

Driver-level cache mapping a user identity to their most recently resolved home database (Optimization:HomeDatabaseCache). It lets a routed session skip home-database discovery by optimistically guessing the cached database — safe only when every pooled connection advertised server-side routing (ssr.enabled), so the server transparently re-routes a wrong guess. Bounded, least-recently-used eviction.

Constant Summary collapse

DEFAULT_MAX_SIZE =
10_000

Instance Method Summary collapse

Constructor Details

#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ HomeDbCache

Returns a new instance of HomeDbCache.



15
16
17
18
19
20
21
22
# File 'lib/neo4j/driver/internal/home_db_cache.rb', line 15

def initialize(max_size: DEFAULT_MAX_SIZE)
  @max_size = max_size
  # Insertion order doubles as LRU recency: a Ruby Hash preserves it, so
  # #get re-inserts the touched key at the end and #set evicts from the
  # front once the cache is full.
  @entries = {}
  @mutex = Mutex.new
end

Instance Method Details

#compute_key(imp_user, auth) ⇒ Object

The cache key for an identity: the impersonated user when set, else the per-session auth token (driver-level auth is nil, so every default-auth session shares one entry — the cache is bound to the identity, not the rotating token). Matches the Java driver: a basic-auth principal is NOT folded into the impersonated-user space (Optimization:HomeDbCacheBasicPrincipalIsImpersonatedUser stays off).



30
# File 'lib/neo4j/driver/internal/home_db_cache.rb', line 30

def compute_key(imp_user, auth) = imp_user || auth

#get(key) ⇒ Object

The cached database for this key, or nil on a miss. Touches recency.



33
34
35
36
37
38
39
40
41
# File 'lib/neo4j/driver/internal/home_db_cache.rb', line 33

def get(key)
  @mutex.synchronize do
    next nil unless @entries.key?(key)

    database = @entries.delete(key)
    @entries[key] = database
    database
  end
end

#set(key, database) ⇒ Object

Remember (or refresh) the resolved home database for this key. A nil database is ignored — there's nothing to guess next time.



45
46
47
48
49
50
51
52
53
# File 'lib/neo4j/driver/internal/home_db_cache.rb', line 45

def set(key, database)
  return if database.nil?

  @mutex.synchronize do
    @entries.delete(key)
    @entries[key] = database
    @entries.shift while @entries.size > @max_size
  end
end