eric6/E5Utilities/E5Cache.py

Sun, 02 Feb 2020 11:04:32 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 02 Feb 2020 11:04:32 +0100
changeset 7380
c99320e859ca
parent 7360
9190402e4505
child 7628
f904d0eef264
permissions
-rw-r--r--

Fixed an issue related to showing the application name in the menu bar of Gnome desktop under Wayland.

5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7253
diff changeset
3 # Copyright (c) 2017 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing classes used for caching objects.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
11 from PyQt5.QtCore import QDateTime, QTimer
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
12
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 class E5Cache(object):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 Class implementing a LRU cache of a specific size.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 If the maximum number of entries is exceeded, the least recently used item
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 is removed from the cache. A cache hit moves the entry to the front of the
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 def __init__(self, size=100):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 Constructor
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @param size maximum number of entries that may be stored in the cache
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @type int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 assert size >= 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.__size = size
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 # internal objects
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.__keyList = []
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
35 self.__store = {} # stores the cache entries
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
36 self.__accesStore = {} # stores the last access date and times
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 self.__hits = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.__misses = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__maxsize = 0
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
40 self.__maxCacheTime = 0 # 0 seconds means aging is disabled
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
41
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
42 self.__cacheTimer = QTimer()
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
43 self.__cacheTimer.setSingleShot(True)
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
44 self.__cacheTimer.timeout.connect(self.__pruneCache)
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 def __moveLast(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 Private method to move a cached item to the MRU position.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @param key key of the item to be retrieved
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @type any hashable type that can be used as a dict key
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__keyList.remove(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__keyList.append(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 def __adjustToSize(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 Private method to adjust the cache to its size.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 """
6755
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
60 if self.__size:
7253
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
61 removeList = self.__keyList[:-self.__size]
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
62 self.__keyList = self.__keyList[-self.__size:]
6755
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
63 for key in removeList:
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
64 del self.__store[key]
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
65 del self.__accesStore[key]
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
66 else:
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
67 self.reset()
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 def getSize(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
5903
7e7002215f9e Little optimisation for E5Cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5888
diff changeset
71 Public method to get the maximum size of the cache.
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 @return maximum number of entries of the cache
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @rtype int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 return self.__size
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 def setSize(self, newSize):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
5903
7e7002215f9e Little optimisation for E5Cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5888
diff changeset
80 Public method to change the maximum size of the cache.
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @param newSize maximum number of entries that may be stored in the
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 cache
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @type int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 assert newSize >= 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__size = newSize
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__adjustToSize()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
91 def getMaximumCacheTime(self):
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
92 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
93 Public method to get the maximum time entries may exist in the cache.
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
94
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
95 @return maximum cache time in seconds
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
96 @rtype int
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
97 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
98 return self.__maxCacheTime
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
99
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
100 def setMaximumCacheTime(self, time):
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
101 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
102 Public method to set the maximum time entries may exist in the cache.
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
103
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
104 @param time maximum cache time in seconds
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
105 @type int
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
106 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
107 if time != self.__maxCacheTime:
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
108 self.__cacheTimer.stop()
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
109 self.__pruneCache()
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
110 self.__maxCacheTime = time
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
111 if self.__maxCacheTime > 0:
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
112 self.__cacheTimer.setInterval(self.__maxCacheTime * 1000)
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
113 self.__cacheTimer.start()
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
114
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 def get(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 Public method to get an entry from the cache given its key.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 If the key is present in the cache, it is moved to the MRU position.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 @param key key of the item to be retrieved
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @type any hashable type that can be used as a dict key
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 @return cached item for the given key or None, if the key is not
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 present
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 @rtype object or None
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 self.__hits += 1
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 self.__moveLast(key)
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
130 self.__accesStore[key] = QDateTime.currentDateTimeUtc()
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 return self.__store[key]
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 else:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 self.__misses += 1
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 return None
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 def add(self, key, item):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 Public method to add an item to the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 If the key is already in use, the cached item is replaced by the new
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 one given and is moved to the MRU position
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 @param key key of the item to be retrieved
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @type any hashable type that can be used as a dict key
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 @param item item to be cached under the given key
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 @type object
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.__moveLast(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 else:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.__keyList.append(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.__store[key] = item
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
153 self.__accesStore[key] = QDateTime.currentDateTimeUtc()
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.__adjustToSize()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 self.__maxsize = max(self.__maxsize, len(self.__keyList))
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 def remove(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 Public method to remove an item from the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 @param key key of the item to be retrieved
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 @type any hashable type that can be used as a dict key
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 del self.__store[key]
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
168 del self.__accesStore[key]
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 self.__keyList.remove(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 def clear(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 Public method to clear the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 self.__keyList = []
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 self.__store = {}
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
177 self.__accesStore = {}
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 def reset(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 Public method to reset the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 This is like clear() but sets the various counters to their initial
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 value as well.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 self.clear()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 self.__hits = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 self.__misses = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 self.__maxsize = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 def length(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 Public method to get the current length of the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 @return current length of the cache
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 @rtype int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 return len(self.__keyList)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 def info(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 Public method to get some information about the cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 @return dictionary containing the cache info
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @rtype dict (with keys "hits", "misses", "maxsize", "currsize")
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 return {
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 "hits": self.__hits,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 "misses": self.__misses,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 "maxsize": self.__maxsize,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 "currsize": self.length(),
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 }
5909
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
213
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
214 def __pruneCache(self):
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
215 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
216 Private slot to prune outdated cache entries and restart the timer.
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
217 """
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
218 if self.__maxCacheTime > 0:
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
219 current = QDateTime.currentDateTimeUtc()
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
220
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
221 keysToBeDeleted = []
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
222 for key, lastAccessTime in self.__accesStore.items():
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
223 if lastAccessTime.secsTo(current) > self.__maxCacheTime:
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
224 keysToBeDeleted.append(key)
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
225 for key in keysToBeDeleted:
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
226 self.remove(key)
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
227
21d90a3abc7c Added a timeout configuration value to the completions cache after which completions will be removed from the cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5903
diff changeset
228 self.__cacheTimer.start()

eric ide

mercurial