forked from asciidoctor/asciidoctor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves asciidoctor#1507 introduce an in-memory cache (Node.js)
- Loading branch information
1 parent
8b555d3
commit 2a3878a
Showing
13 changed files
with
476 additions
and
51 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
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 reset = () => { | ||
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 reset | ||
`reset()` | ||
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.