From 391429f2ea44117ee1c6c8699018aca26140a603 Mon Sep 17 00:00:00 2001 From: Baris Metin Date: Mon, 11 Apr 2011 17:59:40 -0400 Subject: [PATCH] activate cache_utils only when enabled. --- PLC/API.py | 22 ++++++++++++++++++++++ PLC/Methods/GetSlivers.py | 18 +----------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/PLC/API.py b/PLC/API.py index 308fa11a..359e955d 100644 --- a/PLC/API.py +++ b/PLC/API.py @@ -7,6 +7,7 @@ # Copyright (C) 2004-2006 The Trustees of Princeton University # +import os import sys import traceback import string @@ -153,6 +154,27 @@ class PLCAPI: apply_ratelimit_aspect() + # Enable Caching. Only for GetSlivers for the moment. + # TODO: we may consider to do this in an aspect like the ones above. + try: + if self.config.PLC_GETSLIVERS_CACHE: + getslivers_cache = true + except AttributeError: + getslivers_cache = false + + if getslivers_cache: + os.environ['DJANGO_SETTINGS_MODULE']='plc_django_settings' + from cache_utils.decorators import cached + from PLC.Methods.GetSlivers import GetSlivers + + @cached(7200) + def cacheable_call(cls, auth, node_id_or_hostname): + return cls.raw_call(auth, node_id_or_hostname) + + GetSlivers.call = cacheable_call + + + def callable(self, method): """ Return a new instance of the specified method. diff --git a/PLC/Methods/GetSlivers.py b/PLC/Methods/GetSlivers.py index 68259096..18b5fbf1 100644 --- a/PLC/Methods/GetSlivers.py +++ b/PLC/Methods/GetSlivers.py @@ -22,11 +22,6 @@ from PLC.Methods.GetSliceFamily import GetSliceFamily from PLC.Accessors.Accessors_standard import * -# Caching -import os -os.environ['DJANGO_SETTINGS_MODULE']='plc_django_settings' -from cache_utils.decorators import cached - # XXX used to check if slice expiration time is sane MAXINT = 2L**31-1 @@ -210,20 +205,9 @@ class GetSlivers(Method): } def call(self, auth, node_id_or_hostname = None): - try: - cache_opt = self.api.config.PLC_GETSLIVERS_CACHE - except AttributeError: - cache_opt = False - - if (cache_opt): - return self.cacheable_call(auth, node_id_or_hostname) - else: - return self.raw_call(auth, node_id_or_hostname) - - @cached(7200) - def cacheable_call(self, auth, node_id_or_hostname): return self.raw_call(auth, node_id_or_hostname) + def raw_call(self, auth, node_id_or_hostname): timestamp = int(time.time()) -- 2.47.0