# Path: crosscompute/macros/iterable.py
# class LRUDict(OrderedDict):
#     # https://gist.github.com/davesteele/44793cd0348f59f8fadd49d7799bd306
# 
#     def __init__(self, *args, maximum_length: int, **kwargs):
#         assert maximum_length > 0
#         self.maximum_length = maximum_length
#         super().__init__(*args, **kwargs)
# 
#     def __setitem__(self, key, value):
#         super().__setitem__(key, value)
#         super().move_to_end(key)
#         while len(self) > self.maximum_length:
#             super().__delitem__(next(iter(self)))
# 
#     def __getitem__(self, key):
#         value = super().__getitem__(key)
#         super().move_to_end(key)
#         return value


from os.path import getmtime
from .iterable import LRUDict



class FileCache(LRUDict):

    def __init__(self, *args, load_file_data, maximum_length: int, **kwargs):
        super().__init__(*args, maximum_length=maximum_length, **kwargs)
        self._load_file_data = load_file_data

    def __getitem__(self, path):
        if path in self:
            file_time, file_data = super().__getitem__(path)
            if getmtime(path) == file_time:
                return file_data
        file_data = self._load_file_data(path)

# Path: crosscompute/macros/iterable.py
# class LRUDict(OrderedDict):
#     # https://gist.github.com/davesteele/44793cd0348f59f8fadd49d7799bd306
# 
#     def __init__(self, *args, maximum_length: int, **kwargs):
#         assert maximum_length > 0
#         self.maximum_length = maximum_length
#         super().__init__(*args, **kwargs)
# 
#     def __setitem__(self, key, value):
#         super().__setitem__(key, value)
#         super().move_to_end(key)
#         while len(self) > self.maximum_length:
#             super().__delitem__(next(iter(self)))
# 
#     def __getitem__(self, key):
#         value = super().__getitem__(key)
#         super().move_to_end(key)
#         return value


# Path: crosscompute/macros/disk.py
from os.path import getmtime
from .iterable import LRUDict




class FileCache(LRUDict):

    def __init__(self, *args, load_file_data, maximum_length: int, **kwargs):
        super().__init__(*args, maximum_length=maximum_length, **kwargs)
        self._load_file_data = load_file_data

    def __getitem__(self, path):
        if path in self:
            file_time, file_data = super().__getitem__(path)
            if getmtime(path) == file_time:
                return file_data
        file_data = self._load_file_data(path)

        self.__setitem__(path, file_data)