简体   繁体   English

使用Flask时缓存的JSON文件

[英]JSON file cached when using flask

I'm trying to do a visualization app in js + python. 我正在尝试在js + python中做一个可视化应用程序。 My app works like this: 我的应用程序如下所示:

  1. in browser I have a textbox where I introduce an url 在浏览器中,我有一个文本框,其中介绍了一个网址

  2. I send that url to python using Flask 我使用Flask将该网址发送到python

  3. in python I take that url, I process it, and I create a JSON file 在python中,我使用该网址,对其进行处理,然后创建一个JSON文件

  4. in js I take that JSON file and I display the result in browser 在js中,我使用该JSON文件,并在浏览器中显示结果

The problem I encounter: I introduce the url in browser, click submit, and it shows me the result, this is ok. 我遇到的问题:我在浏览器中引入了url,单击Submit,它向我显示了结果,没关系。 But the next time when I introduce another url, it shows me the same result as the previous, if I introduce another url, still the same result. 但是下一次当我引入另一个URL时,它显示的结果与上一个相同,如果我引入另一个URL,则结果仍然相同。 So the JSON file is somehow cached somewhere. 因此,JSON文件以某种方式缓存在某处。 To resolve this I have to delete everytime the old JSON file and rename the new one in python code. 为了解决这个问题,我必须每次删除旧的JSON文件,然后用python代码重命名新的JSON文件。

My question is, where should I put this JSON file to prevent caching? 我的问题是,应将这个JSON文件放在哪里以防止缓存? Or is there another way? 还是有另一种方法?

I have this hierarchy of files: 我有这个文件层次结构:

project
   |--- static
           |--- file.js
           |--- file.json
   |--- templates
           |--- file.html
   |--- file.py

This is what I am trying to do: http://whichlight.github.io/reddit-network-vis/ 这就是我想要做的: http : //whichlight.github.io/reddit-network-vis/

You have one file file.json catering to multiple urls. 您有一个满足多个URL的文件file.json This can only work if you are only dealing with one user at a time. 仅当您一次仅与一个用户打交道时,这才起作用。 I assume you're trying to cache the result, and for that you would need a different file for each url. 我假设您正在尝试缓存结果,因此每个URL需要一个不同的文件。 A slightly better way would be to use an in-memory database like redis for that. 更好的方法是使用内存数据库,例如redis

But if all you want to do is busting the cache, then instead of calling file.json in your ajax call, use file.json?<randomstring> like file.json?t=<timestamp> 但是,如果您只想清除缓存,则不要在ajax调用中调用file.json ,而应使用file.json?<randomstring>就像file.json?t=<timestamp>

$.ajax({
  dataType: "json",
  url: url + "?" + (new Date()).getTime(),
  data: data,
  success: success
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM