E5Utilities/E5Cache.py

changeset 5903
7e7002215f9e
parent 5888
f23f3d2b7516
child 5909
21d90a3abc7c
equal deleted inserted replaced
5902:fb4b68592b7c 5903:7e7002215f9e
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 12
13 # TODO: add timeout for cached entries
13 class E5Cache(object): 14 class E5Cache(object):
14 """ 15 """
15 Class implementing a LRU cache of a specific size. 16 Class implementing a LRU cache of a specific size.
16 17
17 If the maximum number of entries is exceeded, the least recently used item 18 If the maximum number of entries is exceeded, the least recently used item
48 49
49 def __adjustToSize(self): 50 def __adjustToSize(self):
50 """ 51 """
51 Private method to adjust the cache to its size. 52 Private method to adjust the cache to its size.
52 """ 53 """
53 while len(self.__keyList) > self.__size: 54 removeList, self.__keyList = \
54 key = self.__keyList.pop(0) 55 self.__keyList[:-self.__size], self.__keyList[-self.__size:]
56 for key in removeList:
55 del self.__store[key] 57 del self.__store[key]
56 58
57 def getSize(self): 59 def getSize(self):
58 """ 60 """
59 Public method to get the size of the cache. 61 Public method to get the maximum size of the cache.
60 62
61 @return maximum number of entries of the cache 63 @return maximum number of entries of the cache
62 @rtype int 64 @rtype int
63 """ 65 """
64 return self.__size 66 return self.__size
65 67
66 def setSize(self, newSize): 68 def setSize(self, newSize):
67 """ 69 """
68 Public method to change the size of the cache. 70 Public method to change the maximum size of the cache.
69 71
70 @param newSize maximum number of entries that may be stored in the 72 @param newSize maximum number of entries that may be stored in the
71 cache 73 cache
72 @type int 74 @type int
73 """ 75 """

eric ide

mercurial