Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching 'static' compute results #24

Open
Peder2911 opened this issue Dec 8, 2021 · 4 comments
Open

Caching 'static' compute results #24

Peder2911 opened this issue Dec 8, 2021 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@Peder2911
Copy link
Contributor

Some transforms require computations that can be cached to increase performance and decrease the number of database requests. An idea would be to cache these results in the directory. We'd need to handle invalidation, since the computations are not perpetually valid.

@Peder2911 Peder2911 added the enhancement New feature or request label Dec 8, 2021
@Peder2911
Copy link
Contributor Author

For starters, I'd write a class like this:

class CompCache():
   _ROOT_DIR = "~/.views"

   @property
   def _root_dir(self):
      return os.path.expanduser(self._ROOT_DIR)

   def __init__(self, cache_name: str = "transform_library"):
      self._name = cache_name
      self._ensure_folder_struct()

   def _ensure_folder_struct(self):
      try:
         os.makedirs(self._path(""))
      except FileExistsError:
         pass

   def _path(self, name):
      os.path.join(self._root_dir, self._name, name)

   def set(self, name, object):
      with open(self._path(name), "wb") as f:
         pickle.dump(object, f)

   def get(self, name):
      with open(self._path(name), "rb") as f:
         return pickle.load(f)

This does not do any sort of invalidation / timestamping.

@mihaicroicu
Copy link
Contributor

Use diskcache instead! Much better, handles everything you need out of the box, has a FIFO queue for storage size handling, invalidates everything at a given time, and serializes into pickles. And serializes at both object and function level using decorators. BTW, os.makedirs has an exist_ok flag so you don't need that custom method.

@Peder2911
Copy link
Contributor Author

Good idea!

@Peder2911
Copy link
Contributor Author

Although it seems a bit overkill maybe? If we have time to learn it, I'm sure it'd be worth the investment, but this is a pretty simple file-caching case, so we don't need a lot of bells and whistles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants