本文介绍了使用Django的page_cache装饰器时如何清除整个缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的网站,我在其中使用 page_cache 装饰器。
我有一个cronjob,用于检查新数据并对其进行处理(如果有)。
(使用crontab执行的管理命令运行)

I've got a pretty simple site where I'm using the page_cache decorator.I have a cronjob that checks for new data and processes it if it's available. (This is run using management commands executed with crontab)

我想在处理新数据时清除所有页面缓存。

I want to then clear all the page caches when new data is processed.

我在这里查看文档:

并找到了 cache .clear(),这似乎是我想要的。
我已经向数据处理部分添加了一个标志,并在发现新数据时执行 cache.clear()

and found cache.clear(), which seems to be what I want.I've added a flag to the data processing portion and execute cache.clear() when new data is found.

但是,命令运行后,不会清除高速缓存。
(我已经清除了缓存的浏览器,并检查了它是否不是浏览器)

However, after the command runs the cache isn't cleared.(I've cleared the browser cached and checked to be sure it's not the browser)

是否 cache.clear()无法清除所有缓存的页面?

Does cache.clear() not work in to clear all cached pages?

我正在使用 DatabaseCache ,所以我想我可以手动进入并清除缓存表,但是还有更好的方法吗?

I'm using the DatabaseCache, so I guess I could go in and clear the cache table manually, but is there a better way?

推荐答案

SQLite数据库缓存存在此问题- clear()方法无法清除缓存,尽管它可以与MySQL数据库缓存一起正常工作。从[表]删除后,似乎SQLite缓存需要调用 django.db.transation.commit_unless_managed() c>语句正在运行。

I've had this problem with an SQLite database cache - the clear() method doesn't clear the cache although it works fine with a MySQL database cache. It seems that a SQLite cache needs a call to django.db.transation.commit_unless_managed() after the DELETE from [table] statement is run.

自从正式支持作为1.3的一部分被添加到内核之前,我一直在使用多个缓存,因此对几个缓存调用进行了包装-包括 clear()-因此我能够覆盖此方法并包括 commit_unless_managed()。我想我应该把它记录为一个错误。

I have been using multiple caches since before official support was added into core as part of 1.3 and so have a wrapper round several of the cache calls - including clear() - so I was able to override this method and include the commit_unless_managed(). I think I should probably log it as a bug.

这是我用来刷新内存缓存的代码的概要(<$ c $中的默认缓存c> django.core.cache )和存储在 settings.DATABASES ['cache_database'的 cache_table 中的数据库缓存中'] 数据库。

Here's the outline of the code I'm using to flush a memcache cache (the default cache in django.core.cache) and a database cache stored in the cache_table of the settings.DATABASES['cache_database'] database.

from django.db import connections, transaction
from django.core.cache import cache # This is the memcache cache.

def flush():
    # This works as advertised on the memcached cache:
    cache.clear()
    # This manually purges the SQLite cache:
    cursor = connections['cache_database'].cursor()
    cursor.execute('DELETE FROM cache_table')
    transaction.commit_unless_managed(using='cache_database')

而不是像我那样懒惰和硬编码,应该很容易从设置中获取值.CACHES django.db.router

Rather than being lazy and hard coding it the way I have it should be pretty easy to get the values from settings.CACHES and django.db.router.

这篇关于使用Django的page_cache装饰器时如何清除整个缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:53