Skip to content

Commit

Permalink
completed FilesystemCache class with corresponding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanliyah committed Jun 1, 2021
1 parent 559bab5 commit b686f94
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
31 changes: 31 additions & 0 deletions jax/experimental/compilation_cache/compilation_cache_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from absl.testing import absltest
import jax.test_util as jtu
import file_system_cache as fsc

class CompilationClassTest(jtu.JaxTestCase):

def test_new_directory_and_path(self):
fileSystemCacheInstance = fsc.FileSystemCache("/tmp/new_cache_directory")
self.assertEqual(fileSystemCacheInstance.get("nonExistantFile"), None)

def test_new_directory_existing_path(self):
fileSystemCacheInstance = fsc.FileSystemCache("/tmp/new_cache_directory")
fileSystemCacheInstance.put("foo", b"bar")
self.assertEqual(fileSystemCacheInstance.get("foo"), b"bar")

if __name__ == "__main__":
absltest.main(testLoader=jtu.JaxTestLoader())
46 changes: 46 additions & 0 deletions jax/experimental/compilation_cache/file_system_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
## Core functionality:
##create my own directory in experimental folder in jax
##file for this class then seperate file for tests


from typing import Optional
import os
import tempfile

class FileSystemCache:

def __init__(self, path: str):
"""Sets up a cache at 'path'. Cached values may already be present."""
if not os.path.isdir(path):
print("HERE\n")
os.mkdir(path)
self.path = path

def get(self, key: str) -> Optional[bytes]:
"""Returns None if 'key' isn't present."""
path_to_key = os.path.join(self.path, key)
if(os.path.exists(path_to_key)):
with open(path_to_key, "rb") as file:
file_contents = file.read()
return file_contents
else:
return None

def put(self, key: str, value: bytes):
"""Adds new cache entry, possibly evicting older entries."""
with open(os.path.join(self.path, key), "wb") as file:
file.write(value)

0 comments on commit b686f94

Please sign in to comment.