File: cache.py

package info (click to toggle)
orange-widget-base 4.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: python: 16,556; javascript: 58; makefile: 6
file content (62 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
from collections import OrderedDict
from collections.abc import MutableMapping
from typing import NamedTuple


class CacheInfo(NamedTuple):
    misses: int
    hits: int
    maxsize: int
    currsize: int


class LRUCache(MutableMapping):
    __slots__ = ("__dict", "__maxlen", "__miss", "__hit")

    def __init__(self, maxlen=100):
        self.__dict = OrderedDict()
        self.__maxlen = maxlen
        self.__miss = 0
        self.__hit = 0

    def __setitem__(self, key, value):
        dict_ = self.__dict
        dict_[key] = value
        dict_.move_to_end(key)
        if len(dict_) > self.__maxlen:
            dict_.popitem(last=False)

    def __getitem__(self, key):
        dict_ = self.__dict
        try:
            r = dict_[key]
        except KeyError:
            self.__miss += 1
            raise
        else:
            self.__hit += 1
            dict_.move_to_end(key)
            return r

    def __delitem__(self, key):
        del self.__dict[key]

    def __contains__(self, key):
        return key in self.__dict

    def __delete__(self, key):
        del self.__dict[key]

    def __iter__(self):
        return iter(self.__dict)

    def __len__(self):
        return len(self.__dict)

    def cache_info(self):
        return CacheInfo(self.__miss, self.__hit,
                         self.__maxlen, len(self.__dict))

    def clear(self) -> None:
        self.__dict.clear()
        self.__hit = self.__miss = 0