Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions cachier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@


def _max_workers():
try:
return int(os.environ[MAX_WORKERS_ENVAR_NAME])
except KeyError:
os.environ[MAX_WORKERS_ENVAR_NAME] = str(DEFAULT_MAX_WORKERS)
return DEFAULT_MAX_WORKERS
return int(os.environ.get(MAX_WORKERS_ENVAR_NAME, DEFAULT_MAX_WORKERS))


def _set_max_workers(max_workers):
Expand All @@ -49,13 +45,9 @@ def _set_max_workers(max_workers):


def _get_executor(reset=False):
if reset:
_get_executor.executor = ThreadPoolExecutor(_max_workers())
try:
return _get_executor.executor
except AttributeError:
if reset or not hasattr(_get_executor, "executor"):
_get_executor.executor = ThreadPoolExecutor(_max_workers())
return _get_executor.executor
return _get_executor.executor


def _function_thread(core, key, func, args, kwds):
Expand Down Expand Up @@ -350,10 +342,7 @@ def clear_being_calculated():

def cache_dpath():
"""Returns the path to the cache dir, if exists; None if not."""
try:
return core.cache_dir
except AttributeError:
return None
return getattr(core, "cache_dir", None)

def precache_value(*args, value_to_cache, **kwds):
"""Add an initial value to the cache.
Expand Down
21 changes: 7 additions & 14 deletions cachier/cores/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Copyright (c) 2016, Shay Palachy <shaypal5@gmail.com>
import os
import pickle # for local caching
from contextlib import suppress
from datetime import datetime
import threading

Expand Down Expand Up @@ -148,19 +149,12 @@ def _get_cache(self):

def _get_cache_by_key(self, key=None, hash=None):
fpath = self._cache_fpath()
if hash is None:
fpath += f"_{key}"
else:
fpath += f"_{hash}"
fpath += f"_{key}" if hash is None else f"_{hash}"
try:
with portalocker.Lock(fpath, mode="rb") as cache_file:
try:
res = pickle.load(cache_file)
except EOFError: # pragma: no cover
res = None
except FileNotFoundError:
res = None
return res
return pickle.load(cache_file)
except (FileNotFoundError, EOFError):
return None

def _clear_all_cache_files(self):
fpath = self._cache_fpath()
Expand Down Expand Up @@ -253,11 +247,10 @@ def mark_entry_not_calculated(self, key):
self.mark_entry_not_calculated_separate_files(key)
with self.lock:
cache = self._get_cache()
try:
# that's ok, we don't need an entry in that case
with suppress(KeyError):
cache[key]["being_calculated"] = False
self._save_cache(cache)
except KeyError:
pass # that's ok, we don't need an entry in that case

def wait_on_entry_calc(self, key):
if self.separate_files:
Expand Down
16 changes: 4 additions & 12 deletions tests/test_core_lookup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Testing a few basic cachier interfaces."""
import pytest

from cachier import cachier, get_default_params
from cachier.core import MissingMongetter
Expand All @@ -21,31 +22,22 @@ def test_get_default_params():
)


def test_bad_name():
def test_bad_name(name="nope"):
"""Test that the appropriate exception is thrown when an invalid backend is
given."""

name = "nope"
try:
with pytest.raises(ValueError, match=f"specified an invalid core: {name}"):

@cachier(backend=name)
def dummy_func():
pass
except ValueError as e:
assert name in e.args[0]
else:
assert False


def test_missing_mongetter():
"""Test that the appropriate exception is thrown when forgetting to specify
the mongetter."""
try:
with pytest.raises(MissingMongetter):

@cachier(backend="mongo", mongetter=None)
def dummy_func():
pass
except MissingMongetter:
assert True
else:
assert False