Repository: microsoft/qlib
File: qlib/data/cache.py (Python)

Code before:

    def popitem(self, last=True):
        k, v = self.od.popitem(last=last)
        self._size -= 1

        return k, v

    def pop(self, key):
        v = self.od.pop(key)
        self._size -= 1

        return v


class MemCacheSizeofUnit(MemCacheUnit):
    def __init__(self, size_limit=0):
        super().__init__(size_limit=size_limit, limit_type="sizeof")

    def _adjust_size(self, key, value):
        if key in self.od:
            self._size = self._size - sys.getsizeof(self.od[key]) + sys.getsizeof(value)
        else:
            self._size += sys.getsizeof(value)

    def popitem(self, last=True):
        k, v = self.od.popitem(last=last)
        self._size -= sys.getsizeof(v)

        return k, v

    def pop(self, key):
        v = self.od.pop(key)
        self._size -= sys.getsizeof(v)

        return v


class MemCache:
    """Memory cache."""

    def __init__(self, mem_cache_size_limit=None, limit_type="length"):
        """

        Parameters
        ----------
        mem_cache_size_limit: cache max size.
        limit_type: length or sizeof; length(call fun: len), size(call fun: sys.getsizeof).
        """

        size_limit = C.mem_cache_size_limit if mem_cache_size_limit is None else mem_cache_size_limit


Review comment (performance):
```python
if limit_type == "length":
    klass = MemCacheLengthUnit
elif limit_type == "sizeof":
    klass = MemCacheSizeofUnit
else:
    raise ValueError(f"limit_type must be length or sizeof, your limit_type is {limit_type}")

self.__calendar_mem_cache = klass(size_limit)
self.__instrument_mem_cache = klass(size_limit)
self.__feature_mem_cache = klass(size_limit)
```

This will make the code simpler

Code after:
        return 1


class MemCacheSizeofUnit(MemCacheUnit):
    def __init__(self, size_limit=0):
        super().__init__(size_limit=size_limit)

    def _get_value_size(self, value):
        return sys.getsizeof(value)


class MemCache:
    """Memory cache."""

    def __init__(self, mem_cache_size_limit=None, limit_type="length"):
        """

        Parameters
        ----------
        mem_cache_size_limit: cache max size.
        limit_type: length or sizeof; length(call fun: len), size(call fun: sys.getsizeof).
        """

        size_limit = C.mem_cache_size_limit if mem_cache_size_limit is None else mem_cache_size_limit

        if limit_type == "length":
            klass = MemCacheLengthUnit
        elif limit_type == "sizeof":
            klass = MemCacheSizeofUnit
        else:
            raise ValueError(f"limit_type must be length or sizeof, your limit_type is {limit_type}")

        self.__calendar_mem_cache = klass(size_limit)
        self.__instrument_mem_cache = klass(size_limit)
        self.__feature_mem_cache = klass(size_limit)

    def __getitem__(self, key):
        if key == "c":
            return self.__calendar_mem_cache
        elif key == "i":
            return self.__instrument_mem_cache
        elif key == "f":
            return self.__feature_mem_cache
        else:
            raise KeyError("Unknown memcache unit")

    def clear(self):
        self.__calendar_mem_cache.clear()
        self.__instrument_mem_cache.clear()
        self.__feature_mem_cache.clear()