Ejemplo n.º 1
0
class CoapLFUCache(Cache):
    def __init__(self, mode, max_dim):
        """
        Initialise an LFU cache for the Coap.

        :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
        """

        Cache.__init__(self, mode, max_dim)
        self.cache = LFUCache(maxsize=max_dim)

    def __str__(self):
        msg = []
        for e in list(self.cache.values()):
            msg.append(str(e))
        return ("Cache Size: {sz}\n" + "\n".join(msg))

    def debug_print(self):
        """
        :return: a debug printout for the current cache.
        """
        return ("size = %s\n%s" % (
            self.cache.currsize,
            '\n'.join([
                (   "element.max age %s\n"\
                    "element.uri %s\n"\
                    "element.freshness %s"  ) % (
                        element.max_age,
                        element.uri,
                        element.freshness )
                for key, element
                in list(self.cache.items())
            ])))
Ejemplo n.º 2
0
class TwitterListener(StreamListener):
    def __init__(self):
        self.cache = LFUCache(maxsize=50)
        self.cache2 = LFUCache(maxsize=50)
        Timer(interval=60, function=self.print_keys)
        Timer(interval=30, function=self.check_cached_words)

    def on_data(self, data):
        data_lst = json.loads(data)
        data_lst = data_lst.get('text', '').split()

        if self.cache.currsize == self.cache.maxsize:
            for key in list(self.cache.keys()):
                if self.cache[key] == 0:
                    del self.cache[key]

        for word in data_lst:
            if word in self.cache.keys():
                self.cache[word] += 1
            else:
                self.cache[word] = 1
            if self.cache[word] < 0:
                del self.cache[word]

        return True

    def print_keys(self):
        """
        print recent words and update the second cache every 60 seconds
        """
        print(list(self.cache.items()))
        self.cache2.update(self.cache)
        return True

    def check_cached_words(self):
        """
        Decrease score of word by 1 if the score does not change within
        60 seconds
        """
        for word in list(self.cache.keys()):
            if self.cache.get(word) == self.cache2.get(word):
                self.cache[word] -= 1
        return True
Ejemplo n.º 3
0
class CoapLFUCache(CoapCache):
    def __init__(self, max_dim):
        """

        :param max_dim:
        """
        print "Using LFU Cache with dimension : " + str(max_dim)
        self.cache = LFUCache(maxsize=max_dim)

    def update(self, key, element):
        """

        :param key:
        :param element:
        :return:
        """
        print "updating cache"
        print "key: ", key.hashkey
        print "element: ", element
        self.cache.update([(key.hashkey, element)])

    def get(self, key):
        """

        :param key:
        :return: CacheElement
        """
        try:
            response = self.cache[key.hashkey]
        except KeyError:
            print "problem here"
            response = None
        return response

    def is_full(self):
        """
        :return:
        """
        if self.cache.currsize == self.cache.maxsize:
            return True
        return False

    def is_empty(self):
        """

        :return:
        """

        if self.cache.currsize == 0:
            return True
        return False

    def debug_print(self):
        """

        :return:
        """
        print "size = ", self.cache.currsize
        list = self.cache.items()
        for key, element in list:
            print "element.max age ", element.max_age
            print "element.uri", element.uri
            print "element.freshness ", element.freshness