-
-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resolves #1507 introduce an in-memory cache (Node.js) #1523
Open
ggrossetie
wants to merge
2
commits into
asciidoctor:main
Choose a base branch
from
ggrossetie:issue-1507-content-uri-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions
35
packages/core/lib/asciidoctor/js/asciidoctor_ext/node/helpers.rb
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,35 @@ | ||
module Asciidoctor | ||
# Internal: Except where noted, a module that contains internal helper functions. | ||
module Helpers | ||
module_function | ||
|
||
# preserve the original require_library method | ||
alias original_require_library require_library | ||
|
||
# Public: Require the specified library using Kernel#require. | ||
# | ||
# Attempts to load the library specified in the first argument using the | ||
# Kernel#require. Rescues the LoadError if the library is not available and | ||
# passes a message to Kernel#raise if on_failure is :abort or Kernel#warn if | ||
# on_failure is :warn to communicate to the user that processing is being | ||
# aborted or functionality is disabled, respectively. If a gem_name is | ||
# specified, the message communicates that a required gem is not available. | ||
# | ||
# name - the String name of the library to require. | ||
# gem_name - a Boolean that indicates whether this library is provided by a RubyGem, | ||
# or the String name of the RubyGem if it differs from the library name | ||
# (default: true) | ||
# on_failure - a Symbol that indicates how to handle a load failure (:abort, :warn, :ignore) (default: :abort) | ||
# | ||
# Returns The [Boolean] return value of Kernel#require if the library can be loaded. | ||
# Otherwise, if on_failure is :abort, Kernel#raise is called with an appropriate message. | ||
# Otherwise, if on_failure is :warn, Kernel#warn is called with an appropriate message and nil returned. | ||
# Otherwise, nil is returned. | ||
def require_library name, gem_name = true, on_failure = :abort | ||
if name == 'open-uri/cached' | ||
`return Opal.Asciidoctor.Cache.enable()` | ||
end | ||
original_require_library name, gem_name, on_failure | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# copied from https://github.com/tigris/open-uri-cached/blob/master/lib/open-uri/cached.rb | ||
module OpenURI | ||
class << self | ||
# preserve the original open_uri method | ||
alias original_open_uri open_uri | ||
def cache_open_uri(uri, *rest, &block) | ||
response = Cache.get(uri.to_s) || | ||
Cache.set(uri.to_s, original_open_uri(uri, *rest)) | ||
|
||
if block_given? | ||
begin | ||
yield response | ||
ensure | ||
response.close | ||
end | ||
else | ||
response | ||
end | ||
end | ||
# replace the existing open_uri method | ||
alias open_uri cache_open_uri | ||
end | ||
|
||
class Cache | ||
class << self | ||
|
||
%x{ | ||
// largely inspired by https://github.com/isaacs/node-lru-cache/blob/master/index.js | ||
let cache = new Map() | ||
let max = 16000000 // bytes | ||
let length = 0 | ||
let lruList = [] | ||
|
||
class Entry { | ||
constructor (key, value, length) { | ||
this.key = key | ||
this.value = value | ||
this.length = length | ||
} | ||
} | ||
|
||
const trim = () => { | ||
while (length > max) { | ||
pop() | ||
} | ||
} | ||
|
||
const clear = () => { | ||
cache = new Map() | ||
length = 0 | ||
lruList = [] | ||
} | ||
|
||
const pop = () => { | ||
const leastRecentEntry = lruList.pop() | ||
if (leastRecentEntry) { | ||
length -= leastRecentEntry.length | ||
cache.delete(leastRecentEntry.key) | ||
} | ||
} | ||
|
||
const del = (entry) => { | ||
if (entry) { | ||
length -= entry.length | ||
cache.delete(entry.key) | ||
const entryIndex = lruList.indexOf(entry) | ||
if (entryIndex > -1) { | ||
lruList.splice(entryIndex, 1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
## | ||
# Retrieve file content and meta data from cache | ||
# @param [String] key | ||
# @return [StringIO] | ||
def get(key) | ||
%x{ | ||
const cacheKey = crypto.createHash('sha256').update(key).digest('hex') | ||
if (cache.has(cacheKey)) { | ||
const entry = cache.get(cacheKey) | ||
const io = Opal.$$$('::', 'StringIO').$new() | ||
io['$<<'](entry.value) | ||
io.$rewind() | ||
return io | ||
} | ||
} | ||
|
||
nil | ||
end | ||
|
||
# Cache file content | ||
# @param [String] key | ||
# URL of content to be cached | ||
# @param [StringIO] value | ||
# value to be cached, typically StringIO returned from `original_open_uri` | ||
# @return [StringIO] | ||
# Returns value | ||
def set(key, value) | ||
%x{ | ||
const cacheKey = crypto.createHash('sha256').update(key).digest('hex') | ||
const contents = value.string | ||
const len = contents.length | ||
if (cache.has(cacheKey)) { | ||
if (len > max) { | ||
// oversized object, dispose the current entry. | ||
del(cache.get(cacheKey)) | ||
return value | ||
} | ||
// update current entry | ||
const entry = cache.get(cacheKey) | ||
// remove existing entry in the LRU list (unless the entry is already the head). | ||
const listIndex = lruList.indexOf(entry) | ||
if (listIndex > 0) { | ||
lruList.splice(listIndex, 1) | ||
lruList.unshift(entry) | ||
} | ||
entry.value = value | ||
length += len - entry.length | ||
entry.length = len | ||
trim() | ||
return value | ||
} | ||
|
||
const entry = new Entry(cacheKey, value, len) | ||
// oversized objects fall out of cache automatically. | ||
if (entry.length > max) { | ||
return value | ||
} | ||
|
||
length += entry.length | ||
lruList.unshift(entry) | ||
cache.set(cacheKey, entry) | ||
trim() | ||
return value | ||
} | ||
end | ||
|
||
def max=(maxLength) | ||
%x{ | ||
if (typeof maxLength !== 'number' || maxLength < 0) { | ||
throw new TypeError('max must be a non-negative number') | ||
} | ||
|
||
max = maxLength || Infinity | ||
trim() | ||
} | ||
end | ||
|
||
def clear | ||
`clear()` | ||
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mojavelinux I'm a bit concerned about this hack, could we find a better "hook" in Asciidoctor core?