Class: Crawlr::Visits
- Inherits:
-
Object
- Object
- Crawlr::Visits
- Defined in:
- lib/crawlr/visits.rb
Overview
Thread-safe visit tracking system for URL deduplication and history management.
The Visits class maintains a record of visited URLs to prevent duplicate requests during scraping sessions. It uses concurrent data structures to ensure thread safety in parallel scraping environments and implements memory management through configurable visit limits with automatic cache reset when limits are reached.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Checks if the visit tracking system is empty.
-
#initialize(config) ⇒ Visits
constructor
Initializes a new Visits tracker with the given configuration.
-
#new?(url) ⇒ Boolean
Determines if a URL is new (not previously visited).
-
#register(url) ⇒ Boolean
Registers a URL as visited in the tracking system.
-
#stats ⇒ Hash<Symbol, Integer>
Returns statistics about the visit tracking system.
Constructor Details
#initialize(config) ⇒ Visits
Initializes a new Visits tracker with the given configuration
Creates a thread-safe concurrent map for storing visited URLs and configures behavior based on the provided settings for revisiting and memory management.
77 78 79 80 |
# File 'lib/crawlr/visits.rb', line 77 def initialize(config) @config = config @visited = Concurrent::Map.new end |
Instance Method Details
#blank? ⇒ Boolean
Checks if the visit tracking system is empty
Useful for determining if this is the first URL being processed or if the cache has been recently cleared. Can be used to apply different behavior for initial requests (like skipping delays).
110 111 112 |
# File 'lib/crawlr/visits.rb', line 110 def blank? @visited.keys.empty? end |
#new?(url) ⇒ Boolean
Determines if a URL is new (not previously visited)
This method implements the core visit deduplication logic including:
- Automatic cache reset when maximum visit limit is reached
- Configurable URL revisiting behavior
- Thread-safe duplicate detection
- Logging for debugging and monitoring
The method performs memory management by clearing the visited cache when the configured maximum is reached, preventing unbounded memory growth during long-running scraping sessions.
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/crawlr/visits.rb', line 175 def new?(url) # Reset if max visited reached if @visited.size >= @config.max_visited Crawlr.logger.warn "Reached max visited URLs limit (#{@config.max_visited}). Resetting visited cache." @visited.clear end return true if @config.allow_url_revisit return true unless @visited.key?(url) Crawlr.logger.debug "Already visited #{url}; Skipping" false end |
#register(url) ⇒ Boolean
Registers a URL as visited in the tracking system
Marks the given URL as visited by storing it in the concurrent map. This method is thread-safe and can be called from multiple threads simultaneously without risk of data corruption.
94 95 96 |
# File 'lib/crawlr/visits.rb', line 94 def register(url) @visited[url] = true end |
#stats ⇒ Hash<Symbol, Integer>
Returns statistics about the visit tracking system
Provides metrics about the current state of visit tracking including the number of URLs currently stored and the configured maximum limit. Useful for monitoring memory usage and debugging scraping behavior.
131 132 133 134 135 136 |
# File 'lib/crawlr/visits.rb', line 131 def stats { visited_count: @visited.size, max_visited: @config.max_visited } end |