-
-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract CSS selector cache into CSS::SelectorCache (#3226)
**What problem is this PR intended to solve?** This is part of a continuing track of work to separate out the concerns of CSS parser, CSS selector cache, and XPath expression translation into distinct components. In this PR, I've extracted a CSS::SelectorCache class providing functionality that was previously built directly into CSS::Parser (with an awkward API that I'm responsible for writing in 2008). - The methods `Parser.set_cache`, `Parser.cache_on?`, and `Parser.without_cache(&blk)` have been removed. - The cache is now injected by `CSS.xpath_for` (optionally, via a `cache:` keyword argument) instead of being built into the parser. - Documentation for `CSS.xpath_for` has also been updated and improved. **Have you included adequate test coverage?** Mostly an internal refactor, existing test coverage is sufficient except where I removed tests of the removed methods. **Does this change affect the behavior of either the C or the Java implementations?** N/A
- Loading branch information
Showing
7 changed files
with
97 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "thread" | ||
|
||
module Nokogiri | ||
module CSS | ||
module SelectorCache # :nodoc: | ||
@cache = {} | ||
@mutex = Mutex.new | ||
|
||
class << self | ||
# Retrieve the cached XPath expressions for the key | ||
def [](key) | ||
@mutex.synchronize { @cache[key] } | ||
end | ||
|
||
# Insert the XPath expressions `value` at the cache key | ||
def []=(key, value) | ||
@mutex.synchronize { @cache[key] = value } | ||
end | ||
|
||
# Clear the cache | ||
def clear_cache(create_new_object = false) | ||
@mutex.synchronize do | ||
if create_new_object | ||
@cache = {} | ||
else | ||
@cache.clear | ||
end | ||
end | ||
end | ||
|
||
# Construct a unique key cache key | ||
def key(selector:, visitor:) | ||
[selector, visitor.config] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.