mirror of
https://github.com/wassname/cachier.git
synced 2026-09-10 11:50:23 +08:00
small optimization and a fix for python2.7 on travis
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ before_install:
|
||||
# coverage submission packages
|
||||
- pip install codecov
|
||||
install:
|
||||
- if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ]; then pip install pytest coverage pytest-cov; else pip install ".[test]"; fi
|
||||
- if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ]; then pip install pytest coverage pytest-cov .; else pip install ".[test]"; fi
|
||||
script: python -m pytest --cov=cachier
|
||||
# submit coverage
|
||||
after_success:
|
||||
|
||||
+18
-16
@@ -66,23 +66,25 @@ class _PickleCore(_BaseCore):
|
||||
self.cache = None
|
||||
self.reload = reload
|
||||
|
||||
def _get_cache_file_name(self):
|
||||
return '.{}.{}'.format(
|
||||
self.func.__module__, self.func.__name__) # pylint: disable=W0212
|
||||
def _cache_fname(self):
|
||||
if not hasattr(self, 'cache_fname'):
|
||||
self.cache_fname = '.{}.{}'.format(
|
||||
self.func.__module__, self.func.__name__)
|
||||
return self.cache_fname
|
||||
|
||||
def _get_cache_path(self):
|
||||
# print(EXPANDED_CACHIER_DIR)
|
||||
if not os.path.exists(EXPANDED_CACHIER_DIR):
|
||||
os.makedirs(EXPANDED_CACHIER_DIR)
|
||||
fpath = os.path.abspath(os.path.join(
|
||||
os.path.realpath(EXPANDED_CACHIER_DIR),
|
||||
self._get_cache_file_name()
|
||||
))
|
||||
# print(fpath)
|
||||
return fpath
|
||||
def _cache_fpath(self):
|
||||
if not hasattr(self, 'cache_fpath'):
|
||||
# print(EXPANDED_CACHIER_DIR)
|
||||
if not os.path.exists(EXPANDED_CACHIER_DIR):
|
||||
os.makedirs(EXPANDED_CACHIER_DIR)
|
||||
self.cache_fpath = os.path.abspath(os.path.join(
|
||||
os.path.realpath(EXPANDED_CACHIER_DIR),
|
||||
self._cache_fname()
|
||||
))
|
||||
return self.cache_fpath
|
||||
|
||||
def _reload_cache(self):
|
||||
fpath = self._get_cache_path()
|
||||
fpath = self._cache_fpath()
|
||||
try:
|
||||
with open(fpath, 'rb') as cache_file:
|
||||
fcntl.flock(cache_file, fcntl.LOCK_SH)
|
||||
@@ -101,7 +103,7 @@ class _PickleCore(_BaseCore):
|
||||
|
||||
def _save_cache(self, cache):
|
||||
self.cache = cache
|
||||
fpath = self._get_cache_path()
|
||||
fpath = self._cache_fpath()
|
||||
with open(fpath, 'wb') as cache_file:
|
||||
fcntl.flock(cache_file, fcntl.LOCK_EX)
|
||||
pickle.dump(cache, cache_file)
|
||||
@@ -155,7 +157,7 @@ class _PickleCore(_BaseCore):
|
||||
if not entry['being_calculated']:
|
||||
return entry['value']
|
||||
event_handler = _PickleCore.CacheChangeHandler(
|
||||
filename=self._get_cache_file_name(),
|
||||
filename=self._cache_fname(),
|
||||
core=self,
|
||||
key=key
|
||||
)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
from time import time
|
||||
|
||||
from cachier import cachier
|
||||
|
||||
|
||||
@cachier(next_time=True)
|
||||
def _test_int_pickling(int_1, int_2):
|
||||
"""Add the two given ints."""
|
||||
return int_1 + int_2
|
||||
|
||||
|
||||
def _test_int_pickling_compare(int_1, int_2):
|
||||
"""Add the two given ints."""
|
||||
return int_1 + int_2
|
||||
|
||||
|
||||
def test_pickle_speed():
|
||||
"""Test speeds"""
|
||||
print("Comparing speeds of decorated vs non-decorated functions...")
|
||||
num_of_vals = 100
|
||||
times = []
|
||||
for i in range(1, num_of_vals):
|
||||
tic = time()
|
||||
_test_int_pickling_compare(i, i + 1)
|
||||
toc = time()
|
||||
times.append(toc - tic)
|
||||
print(' - Non-decorated average = {:.8f}'.format(
|
||||
sum(times) / num_of_vals))
|
||||
|
||||
_test_int_pickling.clear_cache()
|
||||
times = []
|
||||
for i in range(1, num_of_vals):
|
||||
tic = time()
|
||||
_test_int_pickling(i, i + 1)
|
||||
toc = time()
|
||||
times.append(toc - tic)
|
||||
print(' - Decorated average = {:.8f}'.format(
|
||||
sum(times) / num_of_vals))
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_pickle_speed()
|
||||
@@ -23,41 +23,6 @@ from cachier import cachier
|
||||
|
||||
# Pickle core tests
|
||||
|
||||
@cachier(next_time=True)
|
||||
def _test_int_pickling(int_1, int_2):
|
||||
"""Add the two given ints."""
|
||||
return int_1 + int_2
|
||||
|
||||
|
||||
def _test_int_pickling_compare(int_1, int_2):
|
||||
"""Add the two given ints."""
|
||||
return int_1 + int_2
|
||||
|
||||
|
||||
def test_pickle_speed():
|
||||
"""Test speeds"""
|
||||
print(" * Comparing speeds of decorated vs non-decorated functions...")
|
||||
num_of_vals = 100
|
||||
times = []
|
||||
for i in range(1, num_of_vals):
|
||||
tic = time()
|
||||
_test_int_pickling_compare(i, i + 1)
|
||||
toc = time()
|
||||
times.append(toc - tic)
|
||||
print(' - Non-decorated average = {:.8f}'.format(
|
||||
sum(times) / num_of_vals))
|
||||
|
||||
_test_int_pickling.clear_cache()
|
||||
times = []
|
||||
for i in range(1, num_of_vals):
|
||||
tic = time()
|
||||
_test_int_pickling(i, i + 1)
|
||||
toc = time()
|
||||
times.append(toc - tic)
|
||||
print(' - Decorated average = {:.8f}'.format(
|
||||
sum(times) / num_of_vals))
|
||||
|
||||
|
||||
@cachier(next_time=False)
|
||||
def _takes_5_seconds(arg_1, arg_2):
|
||||
"""Some function."""
|
||||
|
||||
Reference in New Issue
Block a user