 	# Reset Collections
 	# next(err)
 	resetCollections: (next) ->
+		# Prepare
+		database = @getDatabase()
+
+		# Update the cached database
+		@databaseCache = new FilesCollection(database.models)
+
 		# Perform a complete clean of our collections
-		@getDatabase().reset([])
+		database.reset([])
 		@getBlock('meta').reset([]).add([
 			'<meta http-equiv="X-Powered-By" content="DocPad"/>'
 		])
 		@getBlock('scripts').reset([])
 		@getBlock('styles').reset([])
+
+		# Reset caches
+		@filesByUrl = {}
+		@filesBySelector = {}
+		@filesByRelativePath = {}
 
 		# Perform any plugin extensions to what we just cleaned
 		# and forward

	serverMiddleware404: (req,res,next) =>
		# Prepare
		docpad = @
		database = docpad.getDatabase()

		# Check
		return res.send(500)  unless database

	# Get Files (will use live collections)
	getFiles: (query,sorting,paging) ->
		key = JSON.stringify({query,sorting,paging})
		files = @getCollection(key)
		unless files
			files = @getDatabase().findAllLive(query,sorting,paging)
			@setCollection(key, files)
		return files

	# Get another file's model based on a relative path
	getFile: (query,sorting,paging) ->
		file = @getDatabase().findOne(query,sorting,paging)
		return file

	# Get Files At Path
	getFilesAtPath: (path,sorting,paging) ->
		query = $or: [{relativePath: $startsWith: path}, {fullPath: $startsWith: path}]
		files = @getFiles(query,sorting,paging)
		return files

	# Get another file's model based on a relative path
	getFileAtPath: (path,sorting,paging) ->
		file = @getDatabase().fuzzyFindOne(path,sorting,paging)
		return file

	# Get a file by its url
	getFileByUrl: (url,opts={}) ->
		opts.collection ?= @getDatabase()
		file = opts.collection.get(@filesByUrl[url])
		return file

	# Get a file by its selector
	getFileBySelector: (selector,opts={}) ->
		opts.collection ?= @getDatabase()
		file = opts.collection.get(@filesBySelector[selector])
		unless file
			file = opts.collection.fuzzyFindOne(selector)
			if file
				@filesBySelector[selector] = file.id
		return file


	# ---------------------------------
