django-pipeline 清除了我在 Django 数据库缓存中的条目

django-pipeline wipes out my entries in Django Database Cache

我正在开发一个 Django 应用程序,它使用 django-pipeline 来处理浏览器的文件缓存问题(以及其他好处)。

STATIC_URL = '/static/'

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'bower'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
    'pipeline.finders.CachedFileFinder',
)

PIPELINE = {}
PIPELINE['DISABLE_WRAPPER'] = True
PIPELINE['JS_COMPRESSOR'] = 'pipeline.compressors.NoopCompressor'
PIPELINE['CSS_COMPRESSOR'] = 'pipeline.compressors.yuglify.YuglifyCompressor'

PIPELINE['COMPILERS'] = (
    'pipeline.compilers.sass.SASSCompiler',
    'pipeline.compilers.es6.ES6Compiler',
)

PIPELINE['JAVASCRIPT'] = {
...
}

PIPELINE['STYLESHEETS'] = {
...
}

PIPELINE['SASS_BINARY'] = 'C:\Ruby22-x64\bin\sass.bat'
PIPELINE['BABEL_BINARY'] = 'c:\Users\Foobar\node_modules\.bin\babel.cmd'

到目前为止一切顺利。最近我们决定使用 Django 的数据库缓存 (https://docs.djangoproject.com/en/1.9/topics/cache/#database-caching) 来缓存一些长 运行 的统计计算结果。

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'django_dbcache',
    }
}

我执行了 createcachetable 并创建了 table。我在这个 table 中放置没有到期日期的条目,因为我有自己的有效性检查,并且可以自己决定数据是否是最新的,或者是否需要重新计算。

然而,令我惊讶的是,当我为 pipeline 发出 collectstatic 时,它擦除 table 的内容并用它自己的 staticfiles:{md5code} 键填充它-值。 (在生产中,我看到了它没有消灭所有东西的情况)。但这使我的缓存方案无法正常工作。我似乎无法在管道文档中找到任何如何停止 pipeline 执行此操作的设置。 pipeline's cache entry values in the cache are pretty short, merely contain full path to generated files. These entries' 过期时间是几个小时。我不介意他们在那里,只是不要擦我的东西。


补充说明:我在 Windows 平台上(参见上面的管道设置),但同样的事情发生在 Linux 生产服务器上。


除了标记的答案:知道任何人都可以乱用默认缓存 + staticfiles 可以粗暴地清除它,最好将我们的和其他人分开:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'default-cache',
    },
    'staticfiles': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'static-files',
    },
    'my_dbcache': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'django_dbcache',
    }
}

为静态文件定义一个单独的缓存将解决这个问题。 Django 默认首先查找 "staticfiles" 缓存。 示例:

CACHES = {
'default': {
    'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
    'LOCATION': 'django_dbcache',
},
'staticfiles': {
    'BACKEND': "django.core.cache.backends.locmem.LocMemCache",
    'LOCATION': 'static-files',
}