def __init__(self, name, create=True, timeout=None, version=None): super(Cache, self).__init__(_Table, name, create=create, timeout=timeout, version=version) lock_name = '__lock__' self._restricted = set([lock_name]) self._tables = {} self._metadata = None self._start = persistent_cache_base.Now() self._lock_path = os.path.join(self.name, lock_name) self._lock = None self._persistent = False if not os.path.exists(self.name): if not create: raise exceptions.CacheNotFound( 'Persistent cache [{}] not found.'.format(self.name)) elif not os.path.exists(self._lock_path): raise exceptions.CacheInvalid( '[{}] is not a persistent cache.'.format(self.name)) else: # self.name exists and is a directory, and self._lock_path exists. self._persistent = True self._lock = files.FileLock(self._lock_path, timeout_secs=2) self._lock.Lock() try: self.InitializeMetadata() except exceptions.Error: # Make sure we clean up any dangling resources. self.Close(commit=False) raise
def Commit(self): """Commits all operations up to this point.""" if not self._lock: os.mkdir(self.name, 0700) self._persistent = True self._lock = files.FileLock(self._lock_path, timeout_secs=2) self._lock.Lock() # Update the changed tables. for table in list([x for x in self._tables.values() if x.changed]): table._Commit() # pylint: disable=protected-access if self._metadata.changed: self._metadata._Commit() # pylint: disable=protected-access