[Zodb-checkins] CVS: Zope3/src/zope/interface - adapter.py:1.3
Jim Fulton
jim@zope.com
Mon, 20 Jan 2003 15:02:55 -0500
Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv8310
Modified Files:
adapter.py
Log Message:
Added a simpler adapter cache.
=== Zope3/src/zope/interface/adapter.py 1.2 => 1.3 ===
--- Zope3/src/zope/interface/adapter.py:1.2 Wed Dec 25 09:13:42 2002
+++ Zope3/src/zope/interface/adapter.py Mon Jan 20 15:02:52 2003
@@ -75,13 +75,34 @@
raise TypeError(
"The provide argument must be an interface (or None)")
+ # Invalidate our cache
+ self._v_cache = {}
+
self._registerAllProvided(require, provide, object, provide)
- def get(self, (ob_interface, provide), default=None, filter=None):
+ def get(self, ob_interface_provide, default=None, filter=None):
"""
Finds a registered component that provides the given interface.
Returns None if not found.
"""
+
+ if filter is None:
+ cache = getattr(self, '_v_cache', self)
+ if cache is self:
+ cache = self._v_cache = {}
+ key = `ob_interface_provide`
+ cached = cache.get(key, self)
+ if cached is self:
+ cached = self._uncached_get(ob_interface_provide,
+ default, filter)
+ cache[key] = cached
+ return cached
+
+ return self._uncached_get(ob_interface_provide,
+ default, filter)
+
+ def _uncached_get(self, (ob_interface, provide), default, filter):
+
for interface in _flatten(ob_interface, 1):
c = self._reg.get((interface, provide))
if c: