Exemplo n.º 1
0
class TCPServer:
    def __init__(
            self, 
            host='127.0.0.1', 
            port=4000, 
            maxSize=3, 
            expirationTimeInSeconds=300):
        
        self.host = host
        self.port = int(port)
        self.maxSize = int(maxSize)
        self.expirationTimeInSeconds = int(expirationTimeInSeconds)
        self.database = LRUCache(
                maxSize=self.maxSize,
                expirationTimeInSeconds=self.expirationTimeInSeconds
            )

        print("Starting database with {} memory capacity and expiration time of {} seconds".format(self.maxSize, self.expirationTimeInSeconds))

        self.origin = (self.host, self.port)
        print('Starting server at address {} and port {}'.format(self.host,self.port))
        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self.tcp.bind(self.origin)
        self.tcp.listen(10)       
        
        self.listen()

    def listen(self):       
        while True:
            try:
                self.conn, self.client = self.tcp.accept()                    
                print ('IP address {} connected!'.format(self.client[0]))
                while True:
                    msg = self.conn.recv(1024)        
                    if not msg: 
                        break
                    self.save(msg)                                        
                print('Connected closed for {}'.format(self.client[0]))        
            except:
                print('Socket error')

    def closeConnection(self):
        self.conn.close()

    def save(self, data):
        try:
            data = pickle.loads(data)
            key = data['key']
            value = data['value']
            self.database.set(key, value)            
            print('****************************')
            self.show()
            print('****************************')
        except Err:
            print('Could not save data into cache')

    def show(self):
        self.database.show()
Exemplo n.º 2
0
def test_peek():
    cache = LRUCache(3)
    for i in range(3):
        cache[i] = i
    assert list(cache._cache) == [0, 1, 2]
    assert cache.peek(0) == 0
    assert list(cache._cache) == [0, 1, 2]
Exemplo n.º 3
0
 def test_length_returns_0(self):
     """
     Returns 0 if no values have been added
     """
     cache = LRUCache(10)
     result = cache.length()
     self.assertEqual(result, 0)
Exemplo n.º 4
0
    def __init__(self,
                 texture_config, spawner, map_generator, storage_mgr,
                 chunk_title_count=16, chunk_tile_size=2., chunk_count=9,
                 ):
        """
        :param chunk_title_count: chunk中的tile的数量
        :param chunk_tile_size: tile在世界中的尺寸
        :return:
        """
        self._tile_not_found_exception = Exception('tile not found')

        self._chunk_count = chunk_count
        self._storage_mgr = storage_mgr
        self._chunk_tile_count = chunk_title_count
        self._chunk_tile_size = chunk_tile_size
        self._chunks = {}
        self._chunk_size = self._chunk_tile_count * self._chunk_tile_size  # chunk在世界中的尺寸
        self._center_chunk_id = (0, 0)
        self._generator = map_generator
        self._spawner = spawner
        self._cache = LRUCache(32)  # TODO 根据机器内存大小动态设置一个合理的值。
        self._async_loader = async_loader.AsyncLoader()
        self._async_loader.start()
        self._yielders = []
        self._processing_chunk_keys = []  # 当前正在加载的ChunkIDs(加载分为很多帧进行的)
        self._ground_geom_util = GroundGeomUtil(self._chunk_tile_size, self._chunk_tile_count, map_generator, texture_config)

        from collections import Counter
        self._unload_counter = Counter()
        self._counter_threshold = 0 if G.debug else 1
Exemplo n.º 5
0
class CacheTests(unittest.TestCase):
    def setUp(self):
        self.cache = LRUCache(3)

    # def test_cache_overwrite_appropriately(self):
    #     self.cache.set('item1', 'a')
    #     self.cache.set('item2', 'b')
    #     self.cache.set('item3', 'c')

    #     self.cache.set('item2', 'z')

    #     self.assertEqual(self.cache.get('item1'), 'a')
    #     self.assertEqual(self.cache.get('item2'), 'z')

    def test_cache_insertion_and_retrieval(self):
        self.cache.set('item1', 'a')
        self.cache.set('item2', 'b')
        self.cache.set('item3', 'c')

        self.assertEqual(self.cache.get('item1'), 'a')
        self.cache.set('item4', 'd')

        self.assertEqual(self.cache.get('item1'), 'a')
        self.assertEqual(self.cache.get('item3'), 'c')
        self.assertEqual(self.cache.get('item4'), 'd')
        self.assertIsNone(self.cache.get('item2'))
Exemplo n.º 6
0
 def test_put_update(self):
     capacity = 1
     cache = LRUCache(capacity)
     cache.put(1, 3)
     cache.put(1, 5)
     value = cache.get(1)
     self.assertEqual(5, value)
Exemplo n.º 7
0
    def __init__(
            self, 
            host='127.0.0.1', 
            port=4000, 
            maxSize=3, 
            expirationTimeInSeconds=300):
        
        self.host = host
        self.port = int(port)
        self.maxSize = int(maxSize)
        self.expirationTimeInSeconds = int(expirationTimeInSeconds)
        self.database = LRUCache(
                maxSize=self.maxSize,
                expirationTimeInSeconds=self.expirationTimeInSeconds
            )

        print("Starting database with {} memory capacity and expiration time of {} seconds".format(self.maxSize, self.expirationTimeInSeconds))

        self.origin = (self.host, self.port)
        print('Starting server at address {} and port {}'.format(self.host,self.port))
        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self.tcp.bind(self.origin)
        self.tcp.listen(10)       
        
        self.listen()
Exemplo n.º 8
0
 def test_returns_false_if_not_deleted(self):
     """
     Returns False if the value is not deleted
     or cannot be found in the cache
     """
     cache = LRUCache(10)
     result = cache.delete_value(1)
     self.assertEqual(result, False)
Exemplo n.º 9
0
 def test_returns_true_if_value_found(self):
     """
     Returns True if the value is found in the cache
     """
     cache = LRUCache(100)
     cache.add_value("test")
     result = cache.access_value("test")
     self.assertEqual(result, True)
def test_lru_cache_2():
    c = LRUCache(10)
    c.set('a', 3)
    c.set('b', 13)
    c.set('c', 23)
    c.set('a', 33)
    assert c.get('a') == 33
    assert c.get('b') == 13
    assert c.get('c') == 23
Exemplo n.º 11
0
 def test_returns_true_if_deleted(self):
     """
     Returns True if the value has been deleted
     from the cache
     """
     cache = LRUCache(100)
     cache.add_value(1)
     result = cache.delete_value(1)
     self.assertEqual(result, True)
Exemplo n.º 12
0
 def test_returns_false_if_value_not_found(self):
     """
     Returns False if the value is not found in the
     cache
     """
     cache = LRUCache(100)
     cache.add_value("test")
     result = cache.access_value("this value cannot be found")
     self.assertEqual(result, False)
Exemplo n.º 13
0
 def test_removes_value_at_back_of_cache(self):
     """
     Removes a value at the back of the cache if the max
     size has been reached
     """
     cache = LRUCache(5)
     for i in range(0, 6):
         cache.add_value(i)
     result = cache._back.get_value()
     self.assertEqual(result, 1)
Exemplo n.º 14
0
    def test_put_should_add_new_and_evict_tail(self):
        lru_cache = LRUCache(3)
        lru_cache.put(1, "abc")
        lru_cache.put(2, "edf")
        lru_cache.put(3, "ghi")

        lru_cache.put(4, "jkl")

        self.assertIsNone(lru_cache.get(1), "Failed")
        self.assertEqual(lru_cache.get(4), "jkl", "Failed")
Exemplo n.º 15
0
 def test_size(self):
     """
     @brief      Test size method of the LRUCache object
     
     @param      self  The object
     """
     cache = LRUCache(max_size=3)
     cache['a'] = 1
     cache['b'] = "random"
     self.assertEqual(cache.size(), 2)
Exemplo n.º 16
0
def test_get_data():
    empty_cache = LRUCache()
    assert empty_cache.get("BUM") == "BUM_MY_TEST_DATA"
    assert empty_cache._size == 1
    assert empty_cache._head == empty_cache._dict["BUM"]

    assert empty_cache.get("BUM2") == "BUM2_MY_TEST_DATA"
    assert empty_cache._size == 2
    assert empty_cache._head == empty_cache._dict["BUM2"]
    assert empty_cache._tail == empty_cache._dict["BUM"]
Exemplo n.º 17
0
    def test_whenInsertedValue_shouldGetValue(self):
        # with
        cache = LRUCache(1)

        #when
        cache.insert("key1", "value1")
        value = cache.get("key1")

        # then
        assert value == "value1"
def test_lru_cache_max_size():
    c = LRUCache(1)
    c.set('a', 3)
    c.set('b', 33)
    assert c.get('b') == 33
    with pytest.raises(ValueError):
        c.get('a')
Exemplo n.º 19
0
    def test_reset_should_clear(self):
        lru_cache = LRUCache(3)
        lru_cache.put("a", "abc")
        lru_cache.put("b", "edf")
        lru_cache.put("c", "ghi")

        self.assertEqual(len(lru_cache._get_cache()), 3, "Failed")

        lru_cache.reset()

        self.assertEqual(len(lru_cache._get_cache()), 0, "Failed")
Exemplo n.º 20
0
 def test_adds_value_to_front_of_cache(self):
     """
     Adds a new value to the front of the cache
     """
     cache = LRUCache(100)
     cache.add_value(1)
     cache.add_value(2)
     front_value = cache._front.get_value()
     back_value = cache._back.get_value()
     self.assertEqual(front_value, 2)
     self.assertEqual(back_value, 1)
Exemplo n.º 21
0
    def test_get_should_make_it_top(self):
        lru_cache = LRUCache(3)
        lru_cache.put(1, "abc")
        lru_cache.put(2, "edf")
        lru_cache.put(3, "ghi")

        self.assertEqual(lru_cache._get_head_key(), 3, "Failed")

        lru_cache.get(1)

        self.assertEqual(lru_cache._get_head_key(), 1, "Failed")
Exemplo n.º 22
0
 def test_get_expired(self):
     capacity = 2
     cache = LRUCache(capacity)
     cache.put(1, 3)
     cache.put(2, 5)
     cache.get(1)
     cache.put(3, 6)
     value = cache.get(2)
     self.assertEqual(-1, value)
Exemplo n.º 23
0
    def test_del_should_do_no_op_when_key_not_found(self):

        lru_cache = LRUCache(3)
        lru_cache.put(1, "abc")
        lru_cache.put(2, "edf")
        lru_cache.put(3, "ghi")
        lru_cache.delete(4)

        self.assertEqual(lru_cache._get_head_value(), "ghi", "Failed")
Exemplo n.º 24
0
    def test_cache(self):
        test_actions = ["LRUCache", "put", "put", "put", "put", "put", "get", "put", "get", "get", "put", "get", "put",
                        "put", "put",
                        "get", "put", "get", "get", "get", "get", "put", "put", "get", "get", "get", "put", "put",
                        "get",
                        "put", "get",
                        "put", "get", "get", "get", "put", "put", "put", "get", "put", "get", "get", "put", "put",
                        "get",
                        "put", "put",
                        "put", "put", "get", "put", "put", "get", "put", "put", "get", "put", "put", "put", "put",
                        "put",
                        "get", "put",
                        "put", "get", "put", "get", "get", "get", "put", "get", "get", "put", "put", "put", "put",
                        "get",
                        "put", "put",
                        "put", "put", "get", "get", "get", "put", "put", "put", "get", "put", "put", "put", "get",
                        "put",
                        "put", "put",
                        "get", "get", "get", "put", "put", "put", "put", "get", "put", "put", "put", "put", "put",
                        "put",
                        "put"]
        test_input = [[10], [10, 13], [3, 17], [6, 11], [10, 5], [9, 10], [13], [2, 19], [2], [3], [5, 25], [8],
                      [9, 22], [5, 5],
                      [1, 30], [11], [9, 12], [7], [5], [8], [9], [4, 30], [9, 3], [9], [10], [10], [6, 14], [3, 1],
                      [3], [10, 11],
                      [8], [2, 14], [1], [5], [4], [11, 4], [12, 24], [5, 18], [13], [7, 23], [8], [12], [3, 27],
                      [2, 12], [5],
                      [2, 9], [13, 4], [8, 18], [1, 7], [6], [9, 29], [8, 21], [5], [6, 30], [1, 12], [10], [4, 15],
                      [7, 22],
                      [11, 26], [8, 17], [9, 29], [5], [3, 4], [11, 30], [12], [4, 29], [3], [9], [6], [3, 4], [1],
                      [10], [3, 29],
                      [10, 28], [1, 20], [11, 13], [3], [3, 12], [3, 8], [10, 9], [3, 26], [8], [7], [5], [13, 17],
                      [2, 27],
                      [11, 15], [12], [9, 19], [2, 15], [3, 16], [1], [12, 17], [9, 1], [6, 19], [4], [5], [5], [8, 1],
                      [11, 7],
                      [5, 2], [9, 28], [1], [2, 2], [7, 4], [4, 22], [7, 24], [9, 26], [13, 28], [11, 26]]

        lru = None
        result = []
        for i in range(len(test_actions)):
            if test_actions[i] is "LRUCache":
                lru = LRUCache(test_input[i][0])
                result.append(None)
            elif test_actions[i] is "get":
                result.append(lru.get(test_input[i][0]))
            elif test_actions[i] is "put":
                lru.put(test_input[i][0], test_input[i][1])
                result.append(None)

        self.assertListEqual([None, None, None, 1, None, -1, None, -1, 3, 4], result)
Exemplo n.º 25
0
 def test_lru_cache_generic(self):
     lru = LRUCache(2)
     self.assertEqual(lru.put(1, 1), None)
     self.assertEqual(lru.put(2, 2), None)
     self.assertEqual(lru.get(1), 1)
     self.assertEqual(lru.put(3, 3), None)
     self.assertEqual(lru.get(2), -1)
     self.assertEqual(lru.put(4, 4), None)
     self.assertEqual(lru.get(1), -1)
     self.assertEqual(lru.get(3), 3)
     self.assertEqual(lru.get(4), 4)
Exemplo n.º 26
0
    def test_whenInsertedValueOverLimit_shouldOnlyReturnLastInsertedValue(
            self):
        # with
        cache = LRUCache(1)

        # when
        cache.insert("key1", "value1")
        cache.insert("key2", "value2")

        value1 = cache.get("key1")
        value2 = cache.get("key2")

        # then
        assert not value1
        assert value2 == "value2"
 def setup_cache(self, typ, capacity):
     if (typ == 'lru'):
         self.cache = LRUCache(capacity)
     if (typ == 'fifo'):
         self.cache = FIFOCache(capacity)
     if (typ == 'random'):
         self.cache = RandomCache(capacity)
Exemplo n.º 28
0
 def test_records_max_size(self):
     """
     Records the correct max size of the cache
     """
     cache = LRUCache(10)
     result = cache._max_size
     self.assertEqual(result, 10)
 def setUp(self):
     self.capacity = 5000000
     self.__num_over_capacity = 500002
     self.lru_cache = LRUCache(self.capacity)
     
     for i in xrange(1, self.capacity + self.__num_over_capacity):
         self.lru_cache.set('user' + str(i), 'user_number_' + str(i))
Exemplo n.º 30
0
def test_setitem_wrap():
    cache = LRUCache(1)
    cache['test'] = 1
    assert cache['test'] == 1
    cache['test2'] = 4
    with pytest.raises(KeyError):
        assert cache['test'] == 1
Exemplo n.º 31
0
    def setUp(self):
        self.cache_size_max = 15000
        self.num_urls = 15

        self.base_url = "https://placeholdit.imgix.net/~text?txt=image"

        self.cache_dir = '/tmp/cache'
        self.lru = LRUCache(self.cache_size_max, self.cache_dir)
Exemplo n.º 32
0
def test_cache(cache_size, max_items, zipf_param, item_count, method):
    all_items = [i for i in range(int(max_items / 10))]
    Z = Accu_Zipfian(max_items, zipf_param)
    if method == "LRU":
        cache = LRUCache(cache_size)
    elif method == "Hyper":
        cache = HyperbolicCache(cache_size, 64)
    elif method == "LFU":
        cache = LFUCache(cache_size)

    cnt_hits = 0
    cnt_miss = 0
    for i in range(0, item_count):
        if i % 100 == 0:
            next_key = random.choice(all_items)
        else:
            next_key = 1 + Z.get_next(np.random.random())[0]
            #if next_key in all_items:
            #    all_items.remove(next_key)
        ret_val = cache.get(next_key)
        if ret_val == 1:
            #found in cache
            cnt_hits += 1
        else:
            #cache-miss
            cnt_miss += 1
            cache.put(next_key, 0, 0)
    print(method, "Miss rate:", 100.0 * cnt_miss / (cnt_hits + cnt_miss))
Exemplo n.º 33
0
 def test_basic(self):
     """
     @brief      Test getitem/setitem of the LRUCache object
     
     @param      self  The object
     """
     cache = LRUCache(max_size=3)
     cache['a'] = 1
     self.assertEqual(cache['a'], 1)
Exemplo n.º 34
0
def test_remove_node(create_full_cache):
    tmp_cache = create_full_cache
    assert tmp_cache._tail == tmp_cache._dict[1]
    tmp_cache.remove_last_node()
    assert tmp_cache._dict[2] == tmp_cache._tail
    assert tmp_cache._dict.get(1, "NODATA") == "NODATA"

    tmp2_cache = LRUCache()
    tmp2_cache.set(20)
    tmp2_cache.set(21)
    tmp2_cache.remove_last_node()
    assert tmp2_cache._head == tmp2_cache._dict[21]
    assert tmp2_cache._tail == None
    tmp2_cache.remove_last_node()
    assert tmp2_cache._head == None
    assert tmp2_cache._tail == None
Exemplo n.º 35
0
from lru_cache import LRUCache

cache_size = 5

lru = LRUCache(cache_size)
lru.cache("a", 1)
lru.cache("b", 2)
lru.cache("c", 3)
lru.cache("d", 4)
lru.cache("e", 5)
lru.cache("f", 6)
lru.cache("g", 7)
lru.cache("h", 8)
lru.cache("i", 9)
lru.cache("j", 10)
lru.cache("k", 11)
lru.cache("c", 3)


print lru.cache("l", 12)
Exemplo n.º 36
0
from lru_cache import LRUCache
import random
lru = LRUCache(5)
for i in xrange(10):
	lru.set(i%8,random.randint(1,100))
for i in xrange(10):
	print lru.get(i%8)
Exemplo n.º 37
0
from lru_cache import LRUCache

my_cache = LRUCache(5)
my_cache.set("key1", "bar")
my_cache.set("key2", "bat")
my_cache.print_cache

print my_cache.get("key1")

my_cache.set("key3", "bad")
my_cache.set("key4", "ban")
my_cache.set("key5", "bay")

my_cache.print_cache()

print my_cache.get("key4")

my_cache.set("key6", "bla")

my_cache.print_cache()

print my_cache.get("key2")
class LRUCacheScaleTests(unittest.TestCase):
    def setUp(self):
        self.capacity = 5000000
        self.__num_over_capacity = 500002
        self.lru_cache = LRUCache(self.capacity)
        
        for i in xrange(1, self.capacity + self.__num_over_capacity):
            self.lru_cache.set('user' + str(i), 'user_number_' + str(i))

    def tearDown(self):
        self.lru_cache = None

    @raises(KeyError)
    def test_set_and_get(self):
        """
        lru element should be user2 and user1 sould be removed.
        """
        self.assertEqual(self.lru_cache.get_lru_el(), self.lru_cache._cache_dict['user' + str(self.__num_over_capacity)])
        self.lru_cache.get('user' + str(self.__num_over_capacity - 1))

    def test_update(self):
        """
        test update
        """
        self.lru_cache.set('user' + str(self.__num_over_capacity + self.capacity/2), 'ANON_USER')
        self.assertTrue(self.lru_cache.get('user' + str(self.__num_over_capacity + self.capacity/2)) == 'ANON_USER')

        self.lru_cache.set('user' + str(self.__num_over_capacity), 'USER' + str(self.__num_over_capacity))
        self.assertEqual(self.lru_cache.get_lru_el(), self.lru_cache._cache_dict['user' + str(self.__num_over_capacity + 1)])
Exemplo n.º 39
0
 def setUp(self):
     self.lru_cache = LRUCache(3)
     self.lru_cache.set('user1', 'timur')
     self.lru_cache.set('user2', 'ogden')
     self.lru_cache.set('user3', 'francis')
     self.lru_cache.set('user4', 'amra')
Exemplo n.º 40
0
class LRUCacheTests(unittest.TestCase):
    def setUp(self):
        self.lru_cache = LRUCache(3)
        self.lru_cache.set('user1', 'timur')
        self.lru_cache.set('user2', 'ogden')
        self.lru_cache.set('user3', 'francis')
        self.lru_cache.set('user4', 'amra')

    def tearDown(self):
        self.lru_cache = None

    @raises(KeyError)
    def test_key_error(self):
        """
        First element should be removed from cache
        """
        self.lru_cache.get('user1')


    def test_get(self):
        """
        Checks to see if 3 most recent elements are in the cache
        """
        self.assertTrue(self.lru_cache.get('user2') == 'ogden')
        self.assertTrue(self.lru_cache.get('user3') == 'francis')
        self.assertTrue(self.lru_cache.get('user4') == 'amra')

    @raises(KeyError)
    def test_lru_get_and_set(self):
        """
        Performs a few get and set operations: user3 should be the least recently used element and should raise a key error. 
        user2, user 4 and user5 should still be in the cache
        """
        self.lru_cache.get('user2')
        self.lru_cache.set('user5', 'tom')
        
        self.assertTrue(self.lru_cache.get('user2') == 'ogden')
        self.assertTrue(self.lru_cache.get('user4') == 'amra')
        self.assertTrue(self.lru_cache.get('user5') == 'tom')

        self.lru_cache.get('user3')

    @raises(KeyError)
    def test_update(self):
        """
        Updates user2 before adding new element. user4 should be the lru element and user3 should be removed from cache.
        """
        self.lru_cache.set('user2', 'Ogden')
        self.lru_cache.set('user5', 'tom')

        self.assertTrue(self.lru_cache.get('user2') == 'Ogden')
        self.assertFalse(self.lru_cache.get('user2') == 'ogden')

        self.assertTrue(self.lru_cache.get_lru_el() == self.lru_cache._cache_dict['user4'])
        self.lru_cache.get('user3')
Exemplo n.º 41
0
class TestLRUCache(unittest.TestCase):

    def setUp(self):
        self.cache_size_max = 15000
        self.num_urls = 15

        self.base_url = "https://placeholdit.imgix.net/~text?txt=image"

        self.cache_dir = '/tmp/cache'
        self.lru = LRUCache(self.cache_size_max, self.cache_dir)

    def test_lru_cache(self):
        url_count = 0
        out = []
        while url_count < self.num_urls:
            url = "{}{}".format(self.base_url, url_count)
            out.append(self.lru.get(url))
            url_count += 1
        expected = [
            '~text?txt=image11',
            '~text?txt=image12',
            '~text?txt=image13',
            '~text?txt=image14',
        ]
        self.assertEquals(os.listdir(self.cache_dir),
                          expected)
        expected_log = [
            'https://placeholdit.imgix.net/~text?txt=image0 DOWNLOADED 3535',
            'https://placeholdit.imgix.net/~text?txt=image1 DOWNLOADED 3440',
            'https://placeholdit.imgix.net/~text?txt=image2 DOWNLOADED 3538',
            'https://placeholdit.imgix.net/~text?txt=image3 DOWNLOADED 3537',
            'https://placeholdit.imgix.net/~text?txt=image4 DOWNLOADED 3488',
            'https://placeholdit.imgix.net/~text?txt=image5 DOWNLOADED 3511',
            'https://placeholdit.imgix.net/~text?txt=image6 DOWNLOADED 3515',
            'https://placeholdit.imgix.net/~text?txt=image7 DOWNLOADED 3499',
            'https://placeholdit.imgix.net/~text?txt=image8 DOWNLOADED 3522',
            'https://placeholdit.imgix.net/~text?txt=image9 DOWNLOADED 3511',
            'https://placeholdit.imgix.net/~text?txt=image10 DOWNLOADED 3578',
            'https://placeholdit.imgix.net/~text?txt=image11 DOWNLOADED 3459',
            'https://placeholdit.imgix.net/~text?txt=image12 DOWNLOADED 3573',
            'https://placeholdit.imgix.net/~text?txt=image13 DOWNLOADED 3580',
            'https://placeholdit.imgix.net/~text?txt=image14 DOWNLOADED 3526',
        ]
        self.assertEquals(out, expected_log)

    def test_lru_cache_in_cache(self):
        url_count = 0
        url_suffix = [1, 2, 3, 4, 5, 2, 1]
        out = []
        while url_count < len(url_suffix):
            url = "{}{}".format(self.base_url, url_suffix[url_count])
            out.append(self.lru.get(url))
            url_count += 1
        expected = [
            '~text?txt=image1',
            '~text?txt=image2',
            '~text?txt=image4',
            '~text?txt=image5',
        ]
        self.assertEquals(os.listdir(self.cache_dir),
                          expected)
        expected_log = [
            'https://placeholdit.imgix.net/~text?txt=image1 DOWNLOADED 3440',
            'https://placeholdit.imgix.net/~text?txt=image2 DOWNLOADED 3538',
            'https://placeholdit.imgix.net/~text?txt=image3 DOWNLOADED 3537',
            'https://placeholdit.imgix.net/~text?txt=image4 DOWNLOADED 3488',
            'https://placeholdit.imgix.net/~text?txt=image5 DOWNLOADED 3511',
            'https://placeholdit.imgix.net/~text?txt=image2 IN_CACHE 3538',
            'https://placeholdit.imgix.net/~text?txt=image1 DOWNLOADED 3440']
        self.assertEquals(out, expected_log)
Exemplo n.º 42
0
def create_full_cache():
    my_cache = LRUCache()
    test_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
    for num in test_list:
        my_cache.set(num)
    return my_cache