def __init__(self, mode, max_dim): """ :param max_dim: max number of elements in the cache :param mode: used to differentiate between a cache used in a forward-proxy or in a reverse-proxy """ self.max_dimension = max_dim self.mode = mode self.cache = CoapLRUCache(max_dim)
def __init__(self, mode, max_dim, method_arg): """ :param max_dim: max number of elements in the cache :param mode: used to differentiate between a cache used in a forward-proxy or in a reverse-proxy """ print "Dimension of cache : " + str(max_dim) print "Method Number selected : " + str(method_arg) self.max_dimension = max_dim self.mode = mode if (method_arg == 0): self.cache = CoapLRUCache(max_dim) elif (method_arg == 1): self.cache = CoapLFUCache(max_dim) elif (method_arg == 2): self.cache = CoapRRCache(max_dim) elif (method_arg == 3): self.cache = CoapLIFOCache(max_dim) elif (method_arg == 4): self.cache = CoapFIFOCache(max_dim) elif (method_arg == 5): self.cache = CoapMFUCache(max_dim) elif (method_arg == 6): self.cache = CoapSLRUCache(max_dim)
class Cache(object): def __init__(self, mode, max_dim): """ :param max_dim: max number of elements in the cache :param mode: used to differentiate between a cache used in a forward-proxy or in a reverse-proxy """ self.max_dimension = max_dim self.mode = mode self.cache = CoapLRUCache(max_dim) def cache_add(self, request, response): """ checks for full cache and valid code before updating the cache :param request: :param response: :return: """ logger.debug("adding response to the cache") """ checking for valid code - """ code = response.code try: utils.check_code(code) except utils.InvalidResponseCode: # pragma no cover logger.error("Invalid response code") return """ return if max_age is 0 """ if response.max_age == 0: return """ Initialising new cache element based on the mode and updating the cache """ if self.mode == defines.FORWARD_PROXY: new_key = CacheKey(request) else: new_key = ReverseCacheKey(request) logger.debug("MaxAge = {maxage}".format(maxage=response.max_age)) new_element = CacheElement(new_key, response, request, response.max_age) self.cache.update(new_key, new_element) logger.debug( "Cache Size = {size}".format(size=self.cache.debug_print())) def search_related(self, request): logger.debug("Cache Search Request") if self.cache.is_empty() is True: logger.debug("Empty Cache") return None """ extracting everything from the cache """ result = [] items = self.cache.cache.items() for key, item in items: element = self.cache.get(item.key) logger.debug("Element : {elm}".format(elm=str(element))) if request.proxy_uri == element.uri: result.append(item) return result def search_response(self, request): """ creates a key from the request and searches the cache with it :param request: :return CacheElement: returns None if there's a cache miss """ logger.debug("Cache Search Response") if self.cache.is_empty() is True: logger.debug("Empty Cache") return None """ create a new cache key from the request """ if self.mode == defines.FORWARD_PROXY: search_key = CacheKey(request) else: search_key = ReverseCacheKey(request) response = self.cache.get(search_key) return response def validate(self, request, response): """ refreshes a resource when a validation response is received :param request: :param response: :return: """ element = self.search_response(request) if element is not None: element.cached_response.options = response.options element.freshness = True element.max_age = response.max_age element.creation_time = time.time() element.uri = request.proxy_uri def mark(self, element): """ marks the requested resource in the cache as not fresh :param element: :return: """ logger.debug("Element : {elm}".format(elm=str(element))) if element is not None: logger.debug("Mark as not fresh") element.freshness = False logger.debug(str(self.cache))
class Cache(object): def __init__(self, mode, max_dim): """ :param max_dim: max number of elements in the cache :param mode: used to differentiate between a cache used in a forward-proxy or in a reverse-proxy """ self.max_dimension = max_dim self.mode = mode self.cache = CoapLRUCache(max_dim) def cache_add(self, request, response): """ checks for full cache and valid code before updating the cache :param request: :param response: :return: """ logger.debug("adding response to the cache") """ checking for valid code - """ code = response.code try: utils.check_code(code) except utils.InvalidResponseCode: # pragma no cover logger.error("Invalid response code") return """ return if max_age is 0 """ if response.max_age == 0: return """ Initialising new cache element based on the mode and updating the cache """ if self.mode == defines.FORWARD_PROXY: new_key = CacheKey(request) else: new_key = ReverseCacheKey(request) logger.debug("MaxAge = {maxage}".format(maxage=response.max_age)) new_element = CacheElement(new_key, response, request, response.max_age) self.cache.update(new_key, new_element) logger.debug("Cache Size = {size}".format(size=self.cache.debug_print())) def search_related(self, request): logger.debug("Cache Search Request") if self.cache.is_empty() is True: logger.debug("Empty Cache") return None """ extracting everything from the cache """ result = [] items = self.cache.cache.items() for key, item in items: element = self.cache.get(item.key) logger.debug("Element : {elm}".format(elm=str(element))) if request.proxy_uri == element.uri: result.append(item) return result def search_response(self, request): """ creates a key from the request and searches the cache with it :param request: :return CacheElement: returns None if there's a cache miss """ logger.debug("Cache Search Response") if self.cache.is_empty() is True: logger.debug("Empty Cache") return None """ create a new cache key from the request """ if self.mode == defines.FORWARD_PROXY: search_key = CacheKey(request) else: search_key = ReverseCacheKey(request) response = self.cache.get(search_key) return response def validate(self, request, response): """ refreshes a resource when a validation response is received :param request: :param response: :return: """ element = self.search_response(request) if element is not None: element.cached_response.options = response.options element.freshness = True element.max_age = response.max_age element.creation_time = time.time() element.uri = request.proxy_uri def mark(self, element): """ marks the requested resource in the cache as not fresh :param element: :return: """ logger.debug("Element : {elm}".format(elm=str(element))) if element is not None: logger.debug("Mark as not fresh") element.freshness = False logger.debug(str(self.cache))
class Cache(object): def __init__(self, mode, max_dim): """ :param max_dim: max number of elements in the cache :param mode: used to differentiate between a cache used in a forward-proxy or in a reverse-proxy """ self.max_dimension = max_dim self.mode = mode self.cache = CoapLRUCache(max_dim) def cache_add(self, request, response): """ checks for full cache and valid code before updating the cache :param request: :param response: :return: """ print "adding response to the cache" """ checking for valid code - """ code = response.code try: utils.check_code(code) except utils.InvalidResponseCode: print "Invalid response code" return """ return if max_age is 0 """ if response.max_age == 0: return """ Initialising new cache element based on the mode and updating the cache """ if self.mode == defines.FORWARD_PROXY: new_key = CacheKey(request) else: new_key = ReverseCacheKey(request) print "max age = ", response.max_age new_element = CacheElement(new_key, response, response.max_age) self.cache.update(new_key, new_element) print "cache size = ", self.cache.debug_print() def search_response(self, request): """ creates a key from the request and searches the cache with it :param request: :return CacheElement: returns None if there's a cache miss """ print "searching response" if self.cache.is_empty() is True: print "empty cache" return None print "cache not empty" """ create a new cache key from the request """ if self.mode == defines.FORWARD_PROXY: search_key = CacheKey(request) else: search_key = ReverseCacheKey(request) response = self.cache.get(search_key) return response def validate(self, request, response): """ refreshes a resource when a validation response is received :param request: :param response: :return: """ element = self.search_response(request) if element is not None: element.cached_response.options = response.options element.freshness = True element.max_age = response.max_age element.creation_time = time.time() def mark(self, request): """ marks the requested resource in the cache as not fresh :param request: :return: """ element = self.search_response(request) if element is not None: element.freshness = False