Skip to content

Commit

Permalink
Merge pull request #27 from bendog/master
Browse files Browse the repository at this point in the history
fixed cached delete bug and added more tests
  • Loading branch information
bendog authored Sep 17, 2019
2 parents ad82826 + 055ec74 commit 03e5f95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 14 additions & 2 deletions nodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def load(self, index, metainfo=False, default=None):

# If cache enabled, check local filestore for bytes
cache_hit = False
cache_path = None
if self.cache:
base_cache_path = self._get_base_cache_path()
cache_path = os.path.join(base_cache_path, real_index)
Expand All @@ -137,13 +138,16 @@ def load(self, index, metainfo=False, default=None):
return default

# Store the cache result
if self.cache:
if self.cache and cache_path:

if not os.path.exists(cache_path):
open(cache_path, 'w+').close()

with open(cache_path, "wb") as in_file:
in_file.write(serialized.encode(self.encoding))
if type(serialized) is bytes:
in_file.write(serialized)
else:
in_file.write(serialized.encode(self.encoding))

logging.debug("Wrote to cache file: " + cache_path)

Expand All @@ -167,6 +171,14 @@ def delete(self, index):
# First, calculate the real index
real_index = self._format_index_value(index)

# handle delete from cache
if self.cache:
base_cache_path = self._get_base_cache_path()
cache_path = os.path.join(base_cache_path, real_index)
# Cache hit!
if os.path.isfile(cache_path):
os.remove(cache_path)

# Next, get the bytes (if any)
serialized_s3 = self.s3.Object(self.bucket, self.prefix + real_index)
result = serialized_s3.delete()
Expand Down
14 changes: 13 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def test_nodb_aws_profile_name(self):

@moto.mock_s3
def test_nodb_cache(self):
nodb = NoDB('dummy')
bucket_name = 'dummy'
nodb = NoDB(bucket_name)
self._create_mock_bucket(bucket_name)
nodb.index = "Name"
nodb.cache = True

Expand All @@ -128,6 +130,16 @@ def test_nodb_cache(self):
self.assertEqual(loaded, jeff)
loaded = nodb.load("Jeff", default="Booty")
self.assertEqual(loaded, jeff)
# test the cached item is deleted
nodb.delete('Jeff')
loaded = nodb.load("Jeff")
self.assertIsNone(loaded)
# test read from bucket when cache enabled
# remove cached file
nodb.save(jeff)
if os.path.isfile(cache_path):
os.remove(cache_path)
nodb.load('Jeff')

bcp = nodb._get_base_cache_path()

Expand Down

0 comments on commit 03e5f95

Please sign in to comment.