Example #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('FileCache').exception('Error getting cached value')
            return None
        except (EOFError, pickle.UnpicklingError):
            Logger.get('FileCache').exception(
                'Cached information seems corrupted. Overwriting it.')
            return None

        return val
Example #2
0
 def getCachePage( self ):
     if os.path.isfile(self.getFilePath()):
         fp = open(self.getFilePath(),"r")
         OSSpecific.lockFile(fp, 'LOCK_SH')
         page = fp.read()
         return page
     return ""
Example #3
0
 def set(self, key, val, ttl=0):
     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()
     return 1
Example #4
0
 def lockCache( self, file, flag=True):
     global fp
     if flag:
         fp = open(file,"a")
         OSSpecific.lockFile(fp, 'LOCK_EX')
     else:
         if not fp:
             return
         OSSpecific.lockFile(fp, 'LOCK_UN')
         fp.close()
         fp = None
Example #5
0
 def save(self, path, name, data):
     fsPath = os.path.join(self._dir, path)
     if not os.path.exists(fsPath):
         os.makedirs(fsPath)
     filePath = os.path.join(fsPath, name)
     f = open(filePath, 'wb')
     OSSpecific.lockFile(f, 'LOCK_EX')
     try:
         pickle.dump(data, f)
     finally:
         OSSpecific.lockFile(f, 'LOCK_UN')
         f.close()
Example #6
0
 def save(self, path, name, data):
     fsPath = os.path.join(self._dir, path)
     if not os.path.exists(fsPath):
         os.makedirs(fsPath)
     filePath = os.path.join(fsPath, name)
     f = open(filePath, 'wb')
     OSSpecific.lockFile(f, 'LOCK_EX')
     try:
         pickle.dump(data, f)
     finally:
         OSSpecific.lockFile(f, 'LOCK_UN')
         f.close()
Example #7
0
 def load(self, path, name, default=None):
     filePath = os.path.join(self._dir, path, name)
     if not os.path.exists(filePath):
         return default, None
     f = open(filePath, 'rb')
     OSSpecific.lockFile(f, 'LOCK_SH')
     try:
         obj = pickle.load(f)
         mtime = os.path.getmtime(filePath)
     finally:
         OSSpecific.lockFile(f, 'LOCK_UN')
         f.close()
     return obj, mtime
Example #8
0
 def load(self, path, name, default=None):
     filePath = os.path.join(self._dir, path, name)
     if not os.path.exists(filePath):
         return default, None
     f = open(filePath, 'rb')
     OSSpecific.lockFile(f, 'LOCK_SH')
     try:
         obj = pickle.load(f)
         mtime = os.path.getmtime(filePath)
     finally:
         OSSpecific.lockFile(f, 'LOCK_UN')
         f.close()
     return obj, mtime
Example #9
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('FileCache').exception('Error setting value in cache')
         return 0
     return 1
Example #10
0
 def get(self, key):
     path = self._getFilePath(key)
     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
     return val
Example #11
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('FileCache').exception('Error setting value in cache')
         return 0
     return 1
Example #12
0
    def get(self, key):
        try:
            path = self._getFilePath(key)
            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('FileCache').exception('Error getting cached value')
            return None
        except (EOFError, pickle.UnpicklingError):
            Logger.get('FileCache').exception('Cached information seems corrupted. Overwriting it.')
            return None

        return val