This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
hrafn/avcache.py

41 lines
1.1 KiB
Python

from threading import RLock
class AvCache:
"""
Store a cache of avatar images we've already downloaded.
This cache will be accessed by a number of threads, so it includes
a lock as well.
"""
class __impl:
""" Implementation of the singleton interface """
def __init__(self):
self.lock = RLock()
self.map = {}
# storage for the instance reference
__instance = None
def __init__(self):
""" Create singleton instance """
# Check whether we already have an instance
if AvCache.__instance is None:
# Create and remember instance
AvCache.__instance = AvCache.__impl()
# Store instance reference as the only member in the handle
self.__dict__['_AvCache__instance'] = AvCache.__instance
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
# end class AvCache