(cls, key)
| 38 | cls._weak_cache = weakref.WeakValueDictionary() |
| 39 | |
| 40 | def __new__(cls, key): |
| 41 | instance = cls._weak_cache.get(key, None) |
| 42 | if instance is None: |
| 43 | instance = cls._weak_cache.setdefault(key, cls._new_instance(key)) |
| 44 | instance._from_cache = True |
| 45 | |
| 46 | # Update the "strong" cache |
| 47 | cls._strong_cache[key] = cls._strong_cache.pop(key, instance) |
| 48 | |
| 49 | if len(cls._strong_cache) > cls._strong_cache_size: |
| 50 | try: |
| 51 | cls._strong_cache.popitem(last=False) |
| 52 | except KeyError: |
| 53 | # another thread may have already emptied the cache |
| 54 | pass |
| 55 | |
| 56 | return instance |
| 57 | |
| 58 | @classmethod |
| 59 | def no_cache(cls, key): |
no test coverage detected