Skip to content
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

Draft: Speed improvements #659

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pynetbox/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, api, name):
self.api = api
self.name = name
self._setmodel()
self._cached_endpoints = {}

models = {
"dcim": dcim,
Expand All @@ -63,7 +64,11 @@ def __setstate__(self, d):
self._setmodel()

def __getattr__(self, name):
return Endpoint(self.api, self, name, model=self.model)
if name not in self._cached_endpoints:
self._cached_endpoints[name] = Endpoint(
self.api, self, name, model=self.model
)
return self._cached_endpoints[name]

def config(self):
"""Returns config response from app
Expand Down Expand Up @@ -103,6 +108,7 @@ class PluginsApp:

def __init__(self, api):
self.api = api
self._cached_apps = {}

def __getstate__(self):
return self.__dict__
Expand All @@ -111,7 +117,11 @@ def __setstate__(self, d):
self.__dict__.update(d)

def __getattr__(self, name):
return App(self.api, "plugins/{}".format(name.replace("_", "-")))
if name not in self._cached_apps:
self._cached_apps[name] = App(
self.api, "plugins/{}".format(name.replace("_", "-"))
)
return self._cached_apps[name]

def installed_plugins(self):
"""Returns raw response with installed plugins
Expand Down
45 changes: 40 additions & 5 deletions pynetbox/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@
RESERVED_KWARGS = ()


class CachedRecordRegistry:
"""
A cache for Record objects.
"""

def __init__(self):
self._cache = {}
self._hit = 0
self._miss = 0

def get(self, object_type, key):
"""
Retrieves a record from the cache
"""
if not (object_cache := self._cache.get(object_type)):
return None
if object := object_cache.get(key, None):
self._hit += 1
return object
self._miss += 1
return None

def set(self, object_type, key, value):
"""
Stores a record in the cache
"""
if object_type not in self._cache:
self._cache[object_type] = {}
self._cache[object_type][key] = value


class Endpoint:
"""Represent actions available on endpoints in the Netbox API.

Expand All @@ -42,8 +73,8 @@ class Endpoint:
"""

def __init__(self, api, app, name, model=None):
self.return_obj = self._lookup_ret_obj(name, model)
self.name = name.replace("_", "-")
self.return_obj = self._lookup_ret_obj(model)
self.api = api
self.base_url = api.base_url
self.token = api.token
Expand All @@ -53,8 +84,12 @@ def __init__(self, api, app, name, model=None):
endpoint=self.name,
)
self._choices = None
self._init_cache()

def _lookup_ret_obj(self, name, model):
def _init_cache(self):
self._cache = CachedRecordRegistry()

def _lookup_ret_obj(self, model):
"""Loads unique Response objects.

This method loads a unique response object for an endpoint if
Expand All @@ -67,7 +102,7 @@ def _lookup_ret_obj(self, name, model):
:Returns: Record (obj)
"""
if model:
name = name.title().replace("_", "")
name = self.name.title().replace("-", "")
ret = getattr(model, name, Record)
else:
ret = Record
Expand Down Expand Up @@ -636,8 +671,8 @@ def count(self, *args, **kwargs):

if any(i in RESERVED_KWARGS for i in kwargs):
raise ValueError(
"A reserved {} kwarg was passed. Please remove it "
"try again.".format(RESERVED_KWARGS)
"A reserved kwarg was passed ({}). Please remove it "
"and try again.".format(RESERVED_KWARGS)
)

ret = Request(
Expand Down
Loading
Loading