禁止浏览器缓存- make sure web page is not cached

本文介绍了一种确保浏览器不缓存网页内容的方法,通过设置HTTP响应头来实现不同编程语言下的缓存禁止策略。
部署运行你感兴趣的模型镜像

如何禁止浏览器缓存,网上搜到的解决方法都测试无效。

基本上全都是

Cache-Control: no-cache
Pragma: no-cache
Expires: 0

Google了一下,找到了解决方法。

设置response header 的效果就是 返回的时候一定是重新请求页面的

Using PHP:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.

Using Java Servlet, or Node.js:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setHeader("Expires", "0"); // Proxies.

Using ASP.NET:

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.

Using ASP:

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.
Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.
Response.addHeader "Expires", "0" ' Proxies.

Using Ruby on Rails, or Python on Flask:

response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
response.headers["Pragma"] = "no-cache" # HTTP 1.0.
response.headers["Expires"] = "0" # Proxies.

Using Google Go:

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.
responseWriter.Header().Set("Expires", "0") // Proxies.

Using Apache .htaccess file:

<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>

Using HTML4:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

HTML meta tags vs HTTP response headers

根据SO上面的说法,

如果通过HTTP访问,HTTP response headers 是优先于 meta tags 的。但

是,第一次打开是通过HTTP访问的,而返回的时候是从本地读取的。


我在自己尝试的时候,发现这两个都需要设置 才能清除页面表单记录。Google浏览器 和 IE11测试通过,页面的记录消除。其他的浏览器未测试。(推测是因为上面的原因)
如果多次测试发现 页面表单的记录还在。

但是可以保证,只要写了HTTP response headers 返回的时候一定会重新请求。

部分代码如下
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setHeader("Expires", "0"); // Proxies.
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>index.jsp</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" >
	<meta http-equiv="expires" content="0" >

传送门: StackOverFlow


应用

在防止表单重复提交的时候非常有用。

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.9

TensorFlow-v2.9

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

--------------------------------------------------------------------------- HTTPError Traceback (most recent call last) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\utils\_http.py:402, in hf_raise_for_status(response, endpoint_name) 401 try: --> 402 response.raise_for_status() 403 except HTTPError as e: File f:\Programmer\python\MyAI\.venv\Lib\site-packages\requests\models.py:1026, in Response.raise_for_status(self) 1025 if http_error_msg: -> 1026 raise HTTPError(http_error_msg, response=self) HTTPError: 404 Client Error: Not Found for url: https://hf-mirror.com/Langboat/mengzi-t5-small/resolve/main/tokenizer_config.json The above exception was the direct cause of the following exception: RepositoryNotFoundError Traceback (most recent call last) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\transformers\utils\hub.py:398, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs) 396 try: 397 # Load from URL or cache if already cached --> 398 resolved_file = hf_hub_download( 399 path_or_repo_id, 400 filename, 401 subfolder=None if len(subfolder) == 0 else subfolder, 402 repo_type=repo_type, 403 revision=revision, 404 cache_dir=cache_dir, 405 user_agent=user_agent, 406 force_download=force_download, 407 proxies=proxies, 408 resume_download=resume_download, 409 token=token, 410 local_files_only=local_files_only, 411 ) 412 except GatedRepoError as e: File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\utils\_validators.py:114, in validate_hf_hub_args.<locals>._inner_fn(*args, **kwargs) 112 kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.__name__, has_token=has_token, kwargs=kwargs) --> 114 return fn(*args, **kwargs) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:1007, in hf_hub_download(repo_id, filename, subfolder, repo_type, revision, library_name, library_version, cache_dir, local_dir, user_agent, force_download, proxies, etag_timeout, token, local_files_only, headers, endpoint, resume_download, force_filename, local_dir_use_symlinks) 1006 else: -> 1007 return _hf_hub_download_to_cache_dir( 1008 # Destination 1009 cache_dir=cache_dir, 1010 # File info 1011 repo_id=repo_id, 1012 filename=filename, 1013 repo_type=repo_type, 1014 revision=revision, 1015 # HTTP info 1016 endpoint=endpoint, 1017 etag_timeout=etag_timeout, 1018 headers=hf_headers, 1019 proxies=proxies, 1020 token=token, 1021 # Additional options 1022 local_files_only=local_files_only, 1023 force_download=force_download, 1024 ) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:1114, in _hf_hub_download_to_cache_dir(cache_dir, repo_id, filename, repo_type, revision, endpoint, etag_timeout, headers, proxies, token, local_files_only, force_download) 1113 # Otherwise, raise appropriate error -> 1114 _raise_on_head_call_error(head_call_error, force_download, local_files_only) 1116 # From now on, etag, commit_hash, url and size are not None. File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:1655, in _raise_on_head_call_error(head_call_error, force_download, local_files_only) 1650 elif isinstance(head_call_error, (RepositoryNotFoundError, GatedRepoError)) or ( 1651 isinstance(head_call_error, HfHubHTTPError) and head_call_error.response.status_code == 401 1652 ): 1653 # Repo not found or gated => let's raise the actual error 1654 # Unauthorized => likely a token issue => let's raise the actual error -> 1655 raise head_call_error 1656 else: 1657 # Otherwise: most likely a connection issue or Hub downtime => let's warn the user File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:1543, in _get_metadata_or_catch_error(repo_id, filename, repo_type, revision, endpoint, proxies, etag_timeout, headers, token, local_files_only, relative_filename, storage_folder) 1542 try: -> 1543 metadata = get_hf_file_metadata( 1544 url=url, proxies=proxies, timeout=etag_timeout, headers=headers, token=token, endpoint=endpoint 1545 ) 1546 except EntryNotFoundError as http_error: File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\utils\_validators.py:114, in validate_hf_hub_args.<locals>._inner_fn(*args, **kwargs) 112 kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.__name__, has_token=has_token, kwargs=kwargs) --> 114 return fn(*args, **kwargs) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:1460, in get_hf_file_metadata(url, token, proxies, timeout, library_name, library_version, user_agent, headers, endpoint) 1459 # Retrieve metadata -> 1460 r = _request_wrapper( 1461 method="HEAD", 1462 url=url, 1463 headers=hf_headers, 1464 allow_redirects=False, 1465 follow_relative_redirects=True, 1466 proxies=proxies, 1467 timeout=timeout, 1468 ) 1469 hf_raise_for_status(r) File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:283, in _request_wrapper(method, url, follow_relative_redirects, **params) 282 if follow_relative_redirects: --> 283 response = _request_wrapper( 284 method=method, 285 url=url, 286 follow_relative_redirects=False, 287 **params, 288 ) 290 # If redirection, we redirect only relative paths. 291 # This is useful in case of a renamed repository. File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\file_download.py:307, in _request_wrapper(method, url, follow_relative_redirects, **params) 306 response = http_backoff(method=method, url=url, **params) --> 307 hf_raise_for_status(response) 308 return response File f:\Programmer\python\MyAI\.venv\Lib\site-packages\huggingface_hub\utils\_http.py:452, in hf_raise_for_status(response, endpoint_name) 443 message = ( 444 f"{response.status_code} Client Error." 445 + "\n\n" (...) 450 " https://huggingface.co/docs/huggingface_hub/authentication" 451 ) --> 452 raise _format(RepositoryNotFoundError, message, response) from e 454 elif response.status_code == 400: RepositoryNotFoundError: 404 Client Error. (Request ID: Root=1-692ba3e0-1947b16107347a7c6a49e57a;183d134c-894c-4f8a-8f50-ff146e2daab5) Repository Not Found for url: https://hf-mirror.com/Langboat/mengzi-t5-small/resolve/main/tokenizer_config.json. Please make sure you specified the correct `repo_id` and `repo_type`. If you are trying to access a private or gated repo, make sure you are authenticated. For more details, see https://huggingface.co/docs/huggingface_hub/authentication The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) Cell In[6], line 2 1 model_name = "Langboat/mengzi-t5-small" ----> 2 tokenizer = AutoTokenizer.from_pretrained(model_name) 3 if tokenizer.pad_token is None: 4 tokenizer.pad_token = tokenizer.eos_token File f:\Programmer\python\MyAI\.venv\Lib\site-packages\transformers\models\auto\tokenization_auto.py:804, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs) 801 return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs) 803 # Next, let's try to use the tokenizer_config file to get the tokenizer class. --> 804 tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs) 805 if "_commit_hash" in tokenizer_config: 806 kwargs["_commit_hash"] = tokenizer_config["_commit_hash"] File f:\Programmer\python\MyAI\.venv\Lib\site-packages\transformers\models\auto\tokenization_auto.py:637, in get_tokenizer_config(pretrained_model_name_or_path, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, **kwargs) 634 token = use_auth_token 636 commit_hash = kwargs.get("_commit_hash", None) --> 637 resolved_config_file = cached_file( 638 pretrained_model_name_or_path, 639 TOKENIZER_CONFIG_FILE, 640 cache_dir=cache_dir, 641 force_download=force_download, 642 resume_download=resume_download, 643 proxies=proxies, 644 token=token, 645 revision=revision, 646 local_files_only=local_files_only, 647 subfolder=subfolder, 648 _raise_exceptions_for_gated_repo=False, 649 _raise_exceptions_for_missing_entries=False, 650 _raise_exceptions_for_connection_errors=False, 651 _commit_hash=commit_hash, 652 ) 653 if resolved_config_file is None: 654 logger.info("Could not locate the tokenizer configuration file, will try to use the model config instead.") File f:\Programmer\python\MyAI\.venv\Lib\site-packages\transformers\utils\hub.py:421, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs) 416 raise EnvironmentError( 417 "You are trying to access a gated repo.\nMake sure to have access to it at " 418 f"https://huggingface.co/{path_or_repo_id}.\n{str(e)}" 419 ) from e 420 except RepositoryNotFoundError as e: --> 421 raise EnvironmentError( 422 f"{path_or_repo_id} is not a local folder and is not a valid model identifier " 423 "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a token " 424 "having permission to this repo either by logging in with `huggingface-cli login` or by passing " 425 "`token=<your_token>`" 426 ) from e 427 except RevisionNotFoundError as e: 428 raise EnvironmentError( 429 f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists " 430 "for this model name. Check the model page at " 431 f"'https://huggingface.co/{path_or_repo_id}' for available revisions." 432 ) from e OSError: Langboat/mengzi-t5-small is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models' If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`
最新发布
12-01
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值