I'd simplify this whole function something like:
```python
dimensions = None
dimensions_cache = None
if settings.THUMBNAIL_CACHE_DIMENSIONS:
    try:
        dimensions_cache = models.ThumbnailDimensions(
            thumbnail__storage_hash=storage_hash, thumbnail__name=file.name)
        dimensions = dimensions_cache.width, dimensions_cache.height
    except models.ThumbnailDimensions.DoesNotExist:
        dimensions_cache = None
if not dimensions:
    dimensions = get_image_dimensions(file, close=close)
if settings.THUMBNAIL_CACHE_DIMENSIONS and not dimensions_cache:
    dimensions_cache = models.ThumbnailDimensions(thumbnail=thumbnail)
    dimensions_cache.width, dimensions_cache.height = dimensions
    dimensions_cache.save()
return dimensions
```