eric6/E5Utilities/E5Cache.py

Sat, 31 Aug 2019 12:58:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Aug 2019 12:58:11 +0200
branch
without_py2_and_pyqt4
changeset 7192
a22eee00b052
parent 6942
2602857055c5
child 7229
53054eb5b15a
permissions
-rw-r--r--

Started removing runtime support for Python2 and PyQt4.

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
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
3 # Copyright (c) 2017 - 2019 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 from __future__ import unicode_literals
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
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
12 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
13
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 class E5Cache(object):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 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
18
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 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
20 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
21 cache.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self, size=100):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @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
28 @type int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 assert size >= 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.__size = size
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 # internal objects
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 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
36 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
37 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
38 self.__hits = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__misses = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 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
41 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
42
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 = 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
44 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
45 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
46
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 def __moveLast(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 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
50
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @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
52 @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
53 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__keyList.remove(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.__keyList.append(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 def __adjustToSize(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 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
60 """
6755
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
61 if self.__size:
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
62 removeList, self.__keyList = \
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
63 self.__keyList[:-self.__size], self.__keyList[-self.__size:]
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
64 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
65 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
66 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
67 else:
009812744917 Editor: added an option to disable the caching of completion results.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
68 self.reset()
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 def getSize(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
5903
7e7002215f9e Little optimisation for E5Cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5888
diff changeset
72 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
73
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @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
75 @rtype int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 return self.__size
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 def setSize(self, newSize):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
5903
7e7002215f9e Little optimisation for E5Cache.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5888
diff changeset
81 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
82
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @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
84 cache
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 @type int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 assert newSize >= 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__size = newSize
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 self.__adjustToSize()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
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
92 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
93 """
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 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
95
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 @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
97 @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
98 """
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 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
100
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 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
102 """
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 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
104
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 @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
106 @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
107 """
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 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
109 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
110 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
111 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
112 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
113 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
114 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
115
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 def get(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 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
119
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 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
121
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @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
123 @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
124 @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
125 present
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 @rtype object or None
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 self.__hits += 1
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 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
131 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
132 return self.__store[key]
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 else:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 self.__misses += 1
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 return None
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 def add(self, key, item):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 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
140
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 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
142 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
143
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @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
145 @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
146 @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
147 @type object
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 self.__moveLast(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 else:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.__keyList.append(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 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
154 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
155
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.__adjustToSize()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 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
159
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 def remove(self, key):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 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
163
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 @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
165 @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
166 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 if key in self.__store:
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 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
169 del self.__accesStore[key]
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 self.__keyList.remove(key)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 def clear(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 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
175 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 self.__keyList = []
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 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
178 self.__accesStore = {}
5888
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 def reset(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 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
183
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 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
185 value as well.
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 self.clear()
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 self.__hits = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 self.__misses = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 self.__maxsize = 0
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 def length(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 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
195
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 @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
197 @rtype int
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 return len(self.__keyList)
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 def info(self):
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 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
204
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @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
206 @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
207 """
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 return {
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 "hits": self.__hits,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 "misses": self.__misses,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 "maxsize": self.__maxsize,
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 "currsize": self.length(),
f23f3d2b7516 Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 }
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
214
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 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
216 """
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 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
218 """
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 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
220 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
221
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 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
223 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
224 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
225 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
226 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
227 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
228
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
229 self.__cacheTimer.start()

eric ide

mercurial