-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from SvenMarcus/feature/file-cache
Cache workspaces based on last write time
- Loading branch information
Showing
5 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from datetime import datetime | ||
import functools | ||
from pathlib import Path | ||
from typing import Callable, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def path_cache(fn: Callable[[Path], T]) -> Callable[[Path], T]: | ||
pathcache: dict[Path, tuple[T, float]] = {} | ||
|
||
def cache(path: Path) -> T: | ||
new_value = fn(path) | ||
pathcache[path] = new_value, path.stat().st_mtime | ||
return new_value | ||
|
||
@functools.wraps(fn) | ||
def wrapper(path: Path) -> T: | ||
if path not in pathcache: | ||
return cache(path) | ||
|
||
value, saved_timestamp = pathcache[path] | ||
last_modified = path.stat().st_mtime | ||
|
||
if last_modified > saved_timestamp: | ||
return cache(path) | ||
|
||
return value | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
import pytest | ||
|
||
from ocrdbrowser._cache import path_cache | ||
|
||
TMPDIR = Path(__file__).parent / "tmpdir" | ||
TMPFILE = TMPDIR / "tmp.txt" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def cleandir() -> Iterator[None]: | ||
TMPDIR.mkdir() | ||
|
||
yield | ||
|
||
TMPFILE.unlink(missing_ok=True) | ||
TMPDIR.rmdir() | ||
|
||
|
||
def test__path_cache_decorator__returns_the_same_result_without_calling_func_again() -> ( | ||
None | ||
): | ||
call_count = 0 | ||
|
||
@path_cache | ||
def fn(path: str | Path) -> int: | ||
nonlocal call_count | ||
call_count += 1 | ||
return call_count | ||
|
||
first = fn(TMPDIR) | ||
second = fn(TMPDIR) | ||
|
||
assert call_count == 1 | ||
assert first == second | ||
|
||
|
||
def test__when_cached_path_changes__calls_func_again() -> None: | ||
call_count = 0 | ||
|
||
@path_cache | ||
def fn(path: str | Path) -> int: | ||
nonlocal call_count | ||
call_count += 1 | ||
return call_count | ||
|
||
fn(TMPDIR) | ||
TMPFILE.touch() | ||
|
||
fn(TMPDIR) | ||
|
||
assert call_count == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters