Skip to content

Commit

Permalink
Implement generic caching system to avoid duplicate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonfelipesp committed Dec 27, 2023
1 parent b367b36 commit 379437b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
26 changes: 26 additions & 0 deletions netbox_proxbox/backend/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Any

class Cache:
def __init__(self):
self.cache: dict = {}

def get(self, key: str):
result = self.cache.get(key)
if result is not None:
return result

def set(self, key: str, value: Any):
self.cache[key] = value

def delete(self, key: str):
try:
self.cache.pop(key)
except KeyError:
pass

cache = Cache()





2 changes: 1 addition & 1 deletion netbox_proxbox/backend/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setup_logger():
console_handler = logging.StreamHandler()

# Log all messages in the console
console_handler.setLevel(logging.DEBUG)
console_handler.setLevel(logging.ERROR)

# Create a formatter with colors
formatter = ColorizedFormatter('%(name)s [%(asctime)s] [%(levelname)-8s] %(module)s: %(message)s')
Expand Down
21 changes: 16 additions & 5 deletions netbox_proxbox/backend/routes/netbox/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from netbox_proxbox.backend.logging import logger

from netbox_proxbox.backend.cache import cache

import asyncio

class NetboxBase:
Expand Down Expand Up @@ -81,7 +83,6 @@ def __init__(


# New Implementantion of "default_dict" and "default_extra_fields".
base_dict = None
async def get_base_dict(self):
"This method MUST be overwritten by the child class."
pass
Expand All @@ -103,7 +104,16 @@ async def get(
self,
**kwargs
):
self.base_dict = await self.get_base_dict()
self.base_dict = cache.get(self.endpoint)
if self.base_dict is None:
self.base_dict = await self.get_base_dict()
cache.set(self.endpoint, self.base_dict)


# if self.base_dict is None:
# await self.get_base_dict()

#base_dict = await self.get_base_dict()

print(kwargs)
logger.info(f"[GET] Getting '{self.object_name}' from Netbox.")
Expand Down Expand Up @@ -272,9 +282,10 @@ async def post(
self,
data: dict = None,
):
self.base_dict = await self.get_base_dict()
print(self.base_dict)
print(data)
self.base_dict = cache.get(self.endpoint)
if self.base_dict is None:
self.base_dict = await self.get_base_dict()
cache.set(self.endpoint, self.base_dict)

if data:
logger.info(f"[POST] Creating '{self.object_name}' object on Netbox.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cluster(NetboxBase):
app: str = "virtualization"
endpoint: str = "clusters"
object_name: str = "Cluster"


async def get_base_dict(self):
type = await ClusterType(nb = self.nb).get()
Expand Down
11 changes: 3 additions & 8 deletions netbox_proxbox/backend/routes/proxbox/clusters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class VirtualMachineStatus(Enum):

containers = []

from netbox_proxbox.backend.cache import cache

print("CACHE start:", cache.cache)

for px in pxs:
virtual_machines = px.session.cluster.resources.get(type="vm")
Expand Down Expand Up @@ -198,12 +200,5 @@ class VirtualMachineStatus(Enum):
}
})

return result







print("CACHE end:", cache.cache)
return result

0 comments on commit 379437b

Please sign in to comment.