root / logilab.pylintinstaller / logilab / common / cache.py

Revision 202:d67e86292521, 3.0 kB (checked in by tziade@…, 9 months ago)

added logilab.pylintinstaller

Line 
1# This program is free software; you can redistribute it and/or modify it under
2# the terms of the GNU General Public License as published by the Free Software
3# Foundation; either version 2 of the License, or (at your option) any later
4# version.
5#
6# This program is distributed in the hope that it will be useful, but WITHOUT
7# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9#
10# You should have received a copy of the GNU General Public License along with
11# this program; if not, write to the Free Software Foundation, Inc.,
12# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
13""" Copyright (c) 2002-2003 LOGILAB S.A. (Paris, FRANCE).
14 http://www.logilab.fr/ -- mailto:contact@logilab.fr
15
16 Cache module, with a least recently used algorithm for the management of the
17 deletion of entries.
18"""
19
20_marker = object()
21
22class Cache:
23    """ a dictionnary like cache
24
25    inv:
26        len(self._usage) <= self.size
27        len(self.data) <= self.size
28    """
29   
30    def __init__(self, size=100):
31        self.data = {}
32        self.size = size
33        self._usage = []
34
35    def __repr__(self):
36        return repr(self.data)
37
38    def __len__(self):
39        return len(self.data)
40
41    def _update_usage(self, key):
42        # Special case : cache's size = 0 !
43        if self.size <= 0:
44            return
45       
46        if not self._usage:
47            self._usage.append(key)
48       
49        if self._usage[-1] != key:
50            try:
51                self._usage.remove(key)
52            except ValueError:
53                # we are inserting a new key
54                # check the size of the dictionnary
55                # and remove the oldest item in the cache
56                if self.size and len(self._usage) >= self.size:
57                    del self.data[self._usage[0]]
58                    del self._usage[0]
59            self._usage.append(key)
60
61           
62    def __getitem__(self, key):
63        value = self.data[key]
64        self._update_usage(key)
65        return value
66   
67    def __setitem__(self, key, item):
68        # Just make sure that size > 0 before inserting a new item in the cache
69        if self.size > 0:
70            self.data[key] = item
71        self._update_usage(key)
72       
73    def __delitem__(self, key):
74        # If size <= 0, then we don't have anything to do
75        # XXX FIXME : Should we let the 'del' raise a KeyError ?
76        if self.size > 0:
77            del self.data[key]
78            self._usage.remove(key)
79       
80    def pop(self, value, default=_marker):
81        if value in self.data:
82            self._usage.remove(value)
83        if default is _marker:
84            return self.data.pop(value)
85        return self.data.pop(value, default)
86   
87    def clear(self):
88        self.data.clear()
89        self._usage = []
90
91    def keys(self):
92        return self.data.keys()
93
94    def items(self):
95        return self.data.items()
96
97    def values(self):
98        return self.data.values()
99
100    def has_key(self, key):
101        return self.data.has_key(key)
102   
Note: See TracBrowser for help on using the browser.