Lots of duplicate code in clearFrontendCache(), clearBackendCache(), clearInstallCache() and clearAppCache().
Adding a function clearCache($application, $foldersToClear); would solve this.