Skip to content

Commit

Permalink
Merge pull request #57 from tuffnatty/spec_cache
Browse files Browse the repository at this point in the history
Cache deserialized specs to speedup openapi directive reuse (rework #16)
  • Loading branch information
ikalnytskyi committed Oct 5, 2019
2 parents 3990512 + dd4bca0 commit 58774c4
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions sphinxcontrib/openapi/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
from __future__ import unicode_literals

import collections
try:
from functools import lru_cache as simple_cache
except ImportError:
def simple_cache():
def decorate(user_function):
cache = dict()

def wrapper(*args):
try:
result = cache[args]
except KeyError:
result = user_function(*args)
cache[args] = result
return result
return wrapper
return decorate

import io

from docutils import nodes
Expand Down Expand Up @@ -39,9 +56,16 @@ class _YamlOrderedLoader(yaml.SafeLoader):
)


def get_openapihttpdomain(options, abspath, encoding):
# Locally cache spec to speedup processing of same spec file in multiple
# openapi directives
@simple_cache()
def _get_spec(abspath, encoding):
with io.open(abspath, 'rt', encoding=encoding) as stream:
spec = yaml.load(stream, _YamlOrderedLoader)
return yaml.load(stream, _YamlOrderedLoader)


def get_openapihttpdomain(options, abspath, encoding):
spec = _get_spec(abspath, encoding)

# URI parameter is crucial for resolving relative references. So
# we need to set this option properly as it's used later down the
Expand Down

0 comments on commit 58774c4

Please sign in to comment.