blob: 55521f96b8012c650e8067e376f9deaf0ffb04e9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
# Limited-size dictionary-like class to use for caches
#
class Cache(object):
def __init__(self, maxSize=256):
self._maxSize = maxSize
self._size = 0
self._chopSize = maxSize // 10
self._chopSize = self._chopSize or 1
self._cache = {}
self._usage = {}
def __contains__(self, k):
return k in self._cache
def __getitem__(self, k):
self._usage[k] += 1
return self._cache[k]
def __len__(self):
return self._size
def __setitem__(self, k, v):
if self._size >= self._maxSize:
usageKeys = sorted(
self._usage, key=lambda x, d=self._usage: d[x])
for _k in usageKeys[:self._chopSize]:
del self._cache[_k]
del self._usage[_k]
self._size -= self._chopSize
if k not in self._cache:
self._size += 1
self._usage[k] = 0
self._cache[k] = v
def __delitem__(self, k):
del self._cache[k]
del self._usage[k]
self._size -= 1
|