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

start adding caching #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 28 additions & 5 deletions cyanite.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def metrics(self):


class CyaniteReader(object):
__slots__ = ('path',)
__slots__ = ('path', 'cache',)

def __init__(self, path):
def __init__(self, path, cache):
self.path = path
self.cache = cache

def fetch(self, start_time, end_time):
data = requests.get(urls.metrics, params={'path': self.path,
Expand All @@ -76,8 +77,10 @@ def get_intervals(self):

class CyaniteFinder(object):
__fetch_multi__ = 'cyanite'
__slots__ = ('cache',)

def __init__(self, config=None):

global urls
global urllength
if config is not None:
Expand All @@ -95,13 +98,33 @@ def __init__(self, config=None):
urllength = getattr(settings, 'CYANITE_URL_LENGTH', urllength)
urls = URLs(urls)

try:
from graphite_api.app import app
self.cache = app.cache
except:
try:
from django.core.cache import cache
self.cache = cache
except:
self.cache = None

def find_nodes(self, query):
paths = requests.get(urls.paths,
params={'query': query.pattern}).json()
paths = None
key_name = "%s_nodes" % query.pattern

if self.cache:
paths = self.cache.get(key_name)

if paths is None:
paths = requests.get(urls.paths,
params={'query': query.pattern}).json()
if self.cache:
self.cache.add(key_name, paths, timeout=300)

for path in paths:
if path['leaf']:
yield CyaniteLeafNode(path['path'],
CyaniteReader(path['path']))
CyaniteReader(path['path'], self.cache))
else:
yield BranchNode(path['path'])

Expand Down