Attempt to apply lru_cache but without using staticmethod.

This commit is contained in:
Mike Auty
2019-05-09 01:43:00 +01:00
parent a87d3c0dad
commit 488b9be2ba
+7 -13
View File
@@ -150,6 +150,10 @@ class FileLayer(interfaces.layers.DataLayerInterface):
def read(self, offset: int, length: int, pad: bool = False) -> bytes:
"""Reads from the file at offset for length"""
return self._read(offset, length, pad)
@functools.lru_cache(maxsize = 512)
def _read(self, offset: int, length: int, pad: bool = False) -> bytes:
if not self.is_valid(offset, length):
invalid_address = offset
if self.minimum_address < offset <= self.maximum_address:
@@ -157,20 +161,10 @@ class FileLayer(interfaces.layers.DataLayerInterface):
raise exceptions.InvalidAddressException(self.name, invalid_address,
"Offset outside of the buffer boundaries")
return self._read(self._lock, self._file, self.name, offset, length, pad)
@staticmethod
# @functools.lru_cache(maxsize = 512)
def _read(lock: Union[DummyLock, threading.Lock],
file_object: IO[Any],
name: str,
offset: int,
length: int,
pad: bool = False) -> bytes:
# TODO: implement locking for multi-threading
with lock:
file_object.seek(offset)
data = file_object.read(length)
with self._lock:
self._file.seek(offset)
data = self._file.read(length)
if len(data) < length:
if pad: