Ejemplo n.º 1
0
    def get(self, key):
        try:
            path = self._getFilePath(key, False)
            if not os.path.exists(path):
                return None

            f = open(path, 'rb')
            OSSpecific.lockFile(f, 'LOCK_SH')
            expiry = val = None
            try:
                expiry, val = pickle.load(f)
            finally:
                OSSpecific.lockFile(f, 'LOCK_UN')
                f.close()
            if expiry and time.time() > expiry:
                return None
        except (IOError, OSError):
            Logger.get('cache.files').exception('Error getting cached value')
            return None
        except (EOFError, pickle.UnpicklingError):
            Logger.get('cache.files').exception(
                'Cached information seems corrupted. Overwriting it.')
            return None

        return val
Ejemplo n.º 2
0
 def set(self, key, val, ttl=0):
     try:
         f = open(self._getFilePath(key), 'wb')
         OSSpecific.lockFile(f, 'LOCK_EX')
         try:
             expiry = int(time.time()) + ttl if ttl else None
             data = (expiry, val)
             pickle.dump(data, f)
         finally:
             OSSpecific.lockFile(f, 'LOCK_UN')
             f.close()
     except (IOError, OSError):
         Logger.get('cache.files').exception('Error setting value in cache')
         return 0
     return 1
Ejemplo n.º 3
0
 def set(self, key, val, ttl=0):
     try:
         f = open(self._getFilePath(key), 'wb')
         OSSpecific.lockFile(f, 'LOCK_EX')
         try:
             expiry = int(time.time()) + ttl if ttl else None
             data = (expiry, val)
             pickle.dump(data, f)
         finally:
             OSSpecific.lockFile(f, 'LOCK_UN')
             f.close()
     except (IOError, OSError):
         Logger.get('cache.files').exception('Error setting value in cache')
         return 0
     return 1
Ejemplo n.º 4
0
    def get(self, key):
        try:
            path = self._getFilePath(key, False)
            if not os.path.exists(path):
                return None

            f = open(path, 'rb')
            OSSpecific.lockFile(f, 'LOCK_SH')
            expiry = val = None
            try:
                expiry, val = pickle.load(f)
            finally:
                OSSpecific.lockFile(f, 'LOCK_UN')
                f.close()
            if expiry and time.time() > expiry:
                return None
        except (IOError, OSError):
            Logger.get('cache.files').exception('Error getting cached value')
            return None
        except (EOFError, pickle.UnpicklingError):
            Logger.get('cache.files').exception('Cached information seems corrupted. Overwriting it.')
            return None

        return val