Index: pyatspi/accessible.py
===================================================================
--- pyatspi/accessible.py	(revision 965)
+++ pyatspi/accessible.py	(working copy)
@@ -45,13 +45,13 @@
 import constants
 import utils
 import registry
+import weakref
 
-_ACCESSIBLE_CACHE = {}
+_ACCESSIBLE_CACHE = weakref.WeakValueDictionary()
 _CACHE_LEVEL = None
 
-class _PropertyCache(object):
-  '''Fixed-size bag class for holding cached values.'''
-  __slots__ = ('name', 'description', 'rolename')
+class _PropertyCache:
+  pass
 
 def getCacheLevel():
   '''
@@ -115,6 +115,35 @@
   except KeyError:
     return
 
+def _getAndCache(acc, value_name, get_method):
+    if _CACHE_LEVEL != constants.CACHE_PROPERTIES:
+      return get_method()
+    
+    cache = _ACCESSIBLE_CACHE
+    h = hash(acc)
+
+    try:
+      pc = acc._property_cache
+    except AttributeError:
+      try:
+        pc = cache[h]
+      except KeyError:
+        # no cached info for this object yet
+        pc = _PropertyCache()
+        cache[h] = pc
+    
+    try:
+      value = getattr(pc, value_name)
+    except AttributeError:
+      # no cached name for this object yet
+      print 'caching', value_name
+      value = get_method()
+      setattr(pc, value_name, value)
+    
+    acc._property_cache = pc
+    return value
+  
+
 def _makeQuery(interface):
   '''
   Builds a function querying to a specific interface and returns it.
@@ -327,7 +356,7 @@
   @type SLOTS: tuple
   '''
   SLOTTED_CLASSES = {}
-  SLOTS = ('_icache', 'user_data')
+  SLOTS = ('_icache', '_property_cache', 'user_data')
   
   def __new__(cls):
     '''
@@ -369,10 +398,6 @@
     we're caching properties. 
     '''
     try:
-      del _ACCESSIBLE_CACHE[hash(self)]
-    except Exception:
-      pass
-    try:
       self.unref()
     except Exception:
       pass
@@ -445,27 +470,21 @@
     @return: Name of the accessible
     @rtype: string
     '''
-    if _CACHE_LEVEL != constants.CACHE_PROPERTIES:
-      return self._get_name()
-    
-    cache = _ACCESSIBLE_CACHE
-    h = hash(self)
-    try:
-      return cache[h].name
-    except KeyError:
-      # no cached info for this object yet
-      name = self._get_name()
-      pc = _PropertyCache()
-      pc.name = name
-      cache[h] = pc
-      return name
-    except AttributeError:
-      # no cached name for this object yet
-      name = self._get_name()
-      cache[h].name = name
-      return name
-    
+    return _getAndCache(self, 'name', self._get_name)
+
   name = property(_get_name, Accessibility.Accessible._set_name)
+
+  def _get_parent(self):
+    '''
+    Gets the parent of the accessible from the cache if it is available, 
+    otherwise, fetches it remotely.
+    
+    @return: Parent of the accessible
+    @rtype: Accessibility.Accessible
+    '''
+    return _getAndCache(self, 'parent', self._get_parent)
+
+  parent = property(_get_parent)
   
   def getRoleName(self):
     '''
@@ -475,26 +494,18 @@
     @return: Role name of the accessible
     @rtype: string
     '''
-    if _CACHE_LEVEL != constants.CACHE_PROPERTIES:
-      return self._mix_getRoleName()
+    return _getAndCache(self, 'rolename', self._mix_getRoleName)
 
-    cache = _ACCESSIBLE_CACHE
-    h = hash(self)
-    try:
-      return cache[h].rolename
-    except KeyError, e:
-      # no cached info for this object yet
-      rolename = self._mix_getRoleName()
-      pc = _PropertyCache()
-      pc.rolename = rolename
-      cache[h] = pc
-      return rolename
-    except AttributeError, e:
-      # no cached name for this object yet
-      rolename = self._mix_getRoleName()
-      cache[h].rolename = rolename
-      return rolename
-  
+  def getRole(self):
+    '''
+    Gets the role of the accessible from the cache if it is 
+    available, otherwise, fetches it remotely.
+    
+    @return: Role of the accessible
+    @rtype: Accessibility.Role
+    '''
+    return _getAndCache(self, 'role', self._mix_getRole)
+
   def _get_description(self):
     '''    
     Gets the description of the accessible from the cache if it is available,
@@ -503,25 +514,7 @@
     @return: Description of the accessible
     @rtype: string
     '''
-    if _CACHE_LEVEL != constants.CACHE_PROPERTIES:
-      return self._get_description()
-
-    cache = _ACCESSIBLE_CACHE
-    h = hash(self)
-    try:
-      return cache[h].description
-    except KeyError:
-      # no cached info for this object yet
-      description = self._get_description()
-      pc = _PropertyCache()
-      pc.description = description
-      cache[h] = pc
-      return description
-    except AttributeError:
-      # no cached name for this object yet
-      description = self._get_description()
-      cache[h].description = description
-      return description
+    return _getAndCache(self, 'description', self._get_description)
     
   description = property(_get_description, 
                          Accessibility.Accessible._set_description)
@@ -595,7 +588,7 @@
 map(_mixExceptions, [Accessibility.StateSet])
 # 3. mix the new functions
 _mixClass(Accessibility.Accessible, _AccessibleMixin,
-          ['_get_name', '_get_description'])
+          ['_get_name', '_get_description', '_get_parent'])
 # 4. mix queryInterface convenience methods
 _mixInterfaces(Accessibility.Accessible, constants.ALL_INTERFACES)
 # 5. mix Relation class
