Make the cache encode and decode not crash if something goes wrong
This commit is contained in:
		@@ -84,7 +84,10 @@ class FileSystemStorage:
 | 
				
			|||||||
            log.debug('%s not present in cache', key)
 | 
					            log.debug('%s not present in cache', key)
 | 
				
			||||||
        except OSError:
 | 
					        except OSError:
 | 
				
			||||||
            log.debug('Failed to read %s from cache:', key, exc_info=True)
 | 
					            log.debug('Failed to read %s from cache:', key, exc_info=True)
 | 
				
			||||||
            return None
 | 
					        except Exception:
 | 
				
			||||||
 | 
					            log.debug('Failed to decode %s from cache:', key, exc_info=True)
 | 
				
			||||||
 | 
					            log.debug('Removing %s entry', key)
 | 
				
			||||||
 | 
					            self._remove(directory, key)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _store(self, directory, key, value):
 | 
					    def _store(self, directory, key, value):
 | 
				
			||||||
        filename = os.path.join(directory, key.replace('/', '_'))
 | 
					        filename = os.path.join(directory, key.replace('/', '_'))
 | 
				
			||||||
@@ -96,6 +99,8 @@ class FileSystemStorage:
 | 
				
			|||||||
        except OSError:
 | 
					        except OSError:
 | 
				
			||||||
            log.debug('Failed to store %s to cache:', key, exc_info=True)
 | 
					            log.debug('Failed to store %s to cache:', key, exc_info=True)
 | 
				
			||||||
            return False
 | 
					            return False
 | 
				
			||||||
 | 
					        except Exception:
 | 
				
			||||||
 | 
					            log.debug('Failed to encode %s to cache:', key, exc_info=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _remove(self, directory, key):
 | 
					    def _remove(self, directory, key):
 | 
				
			||||||
        filename = os.path.join(directory, key.replace('/', '_'))
 | 
					        filename = os.path.join(directory, key.replace('/', '_'))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,9 +34,14 @@ class TestCacheClass(SlixTest):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def testFileSystemCache(self):
 | 
					    def testFileSystemCache(self):
 | 
				
			||||||
 | 
					        def failing_decode(value):
 | 
				
			||||||
 | 
					            if value == "failme":
 | 
				
			||||||
 | 
					                raise Exception("you failed")
 | 
				
			||||||
 | 
					            return value
 | 
				
			||||||
        with TemporaryDirectory() as tmpdir:
 | 
					        with TemporaryDirectory() as tmpdir:
 | 
				
			||||||
            cache = FileSystemCache(tmpdir, "test")
 | 
					            cache = FileSystemCache(tmpdir, "test", decode=failing_decode)
 | 
				
			||||||
            cache.store("test", "test_value")
 | 
					            cache.store("test", "test_value")
 | 
				
			||||||
 | 
					            cache.store("test2", "failme")
 | 
				
			||||||
            self.assertEqual(
 | 
					            self.assertEqual(
 | 
				
			||||||
                cache.retrieve("test"),
 | 
					                cache.retrieve("test"),
 | 
				
			||||||
                "test_value"
 | 
					                "test_value"
 | 
				
			||||||
@@ -47,6 +52,11 @@ class TestCacheClass(SlixTest):
 | 
				
			|||||||
                None
 | 
					                None
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self.assertEqual(
 | 
				
			||||||
 | 
					                cache.retrieve("test2"),
 | 
				
			||||||
 | 
					                None
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def testFileSystemPerJidCache(self):
 | 
					    def testFileSystemPerJidCache(self):
 | 
				
			||||||
        with TemporaryDirectory() as tmpdir:
 | 
					        with TemporaryDirectory() as tmpdir:
 | 
				
			||||||
            cache = FileSystemPerJidCache(tmpdir, "test")
 | 
					            cache = FileSystemPerJidCache(tmpdir, "test")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user