session_cache_limiter / session_cache_expire
session_cache_limiter(?string $value = null): string|false — session_cache_expire(?int $value = null): int|false
读取/设置会话页面的 HTTP 缓存策略:limiter 决定 session_start 时发送哪些 Cache-Control/Expires 头(nocache/private/private_no_expire/public/关闭),expire 以分钟计其 max-age/Expires 值;每请求须在 session_start 前设置。
C C23
#include <stdio.h>
#include <string.h>
/* limiter 模式 → 缓存头的映射,即 session_cache_limiter 编码的内容 */
static const char *cache_control(const char *limiter, int expire_minutes) {
if (strcmp(limiter, "nocache") == 0) return "no-store, no-cache, must-revalidate";
if (strcmp(limiter, "public") == 0) return "public"; /* max-age 本应跟随 expire */
/* private / private_no_expire */
(void)expire_minutes;
return "private";
}
int main(void) {
const char *limiter = "private";
int expire = 30; /* 分钟 */
printf("limiter: %s\n", limiter);
printf("expire: %d\n", expire);
/* limiter 发送的头(private,max-age = expire*60) */
printf("private headers: Cache-Control: private, max-age=%d\n", expire * 60);
printf("nocache headers: Cache-Control: %s\n", cache_control("nocache", expire));
return 0;
}
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
C++ C++26
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
// limiter 模式 → Cache-Control 值的映射,即 session_cache_limiter 编码的内容
const std::unordered_map<std::string, std::string> modes = {
{"nocache", "no-store, no-cache, must-revalidate"},
{"private", "private"},
{"private_no_expire", "private"},
{"public", "public"},
};
const std::string limiter = "private";
const int expire = 30; // 分钟
std::cout << "limiter: " << limiter << "\n";
std::cout << "expire: " << expire << "\n";
std::cout << "private headers: Cache-Control: private, max-age=" << expire * 60 << "\n";
std::cout << "nocache headers: Cache-Control: " << modes.at("nocache") << "\n";
return 0;
}
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
C# v11.0.100
// limiter 模式 → Cache-Control 值;框架层等价物是 ASP.NET 的 Response.Headers/Cache-Control 或 [ResponseCache] 特性
var modes = new System.Collections.Generic.Dictionary<string, string>
{
["nocache"] = "no-store, no-cache, must-revalidate",
["private"] = "private",
["public"] = "public",
};
string limiter = "private";
int expire = 30; // 分钟
Console.WriteLine("limiter: " + limiter);
Console.WriteLine("expire: " + expire);
Console.WriteLine("private headers: Cache-Control: private, max-age=" + expire * 60);
Console.WriteLine("nocache headers: Cache-Control: " + modes["nocache"]);
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
Go v1.27.4
package main
import "fmt"
// limiter 模式 → Cache-Control 值;框架层等价物是写响应体前 w.Header().Set("Cache-Control", ...)
func main() {
modes := map[string]string{
"nocache": "no-store, no-cache, must-revalidate",
"private": "private",
"public": "public",
}
limiter := "private"
expire := 30 // 分钟
fmt.Println("limiter:", limiter)
fmt.Println("expire:", expire)
fmt.Println("private headers: Cache-Control: private, max-age=" + fmt.Sprint(expire*60))
fmt.Println("nocache headers: Cache-Control: " + modes["nocache"])
}
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
Java v27.0.1
import java.util.Map;
public class Main {
public static void main(String[] args) {
// limiter 模式 → Cache-Control 值;框架层等价物是 Servlet 过滤器里的 response.setHeader("Cache-Control", ...)
Map<String, String> modes = Map.of(
"nocache", "no-store, no-cache, must-revalidate",
"private", "private",
"public", "public");
String limiter = "private";
int expire = 30; // 分钟
System.out.println("limiter: " + limiter);
System.out.println("expire: " + expire);
System.out.println("private headers: Cache-Control: private, max-age=" + (expire * 60));
System.out.println("nocache headers: Cache-Control: " + modes.get("nocache"));
}
}
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
JavaScript v26.13.1
// limiter 模式 → Cache-Control 值;框架层等价物是 express 的 res.set("Cache-Control", ...)(要在响应体之前)
const modes = {
nocache: "no-store, no-cache, must-revalidate",
private: "private",
public: "public",
};
const limiter = "private";
const expire = 30; // 分钟
console.log("limiter:", limiter);
console.log("expire:", expire);
console.log("private headers: Cache-Control: private, max-age=" + expire * 60);
console.log("nocache headers: Cache-Control: " + modes.nocache);
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
PHP v8.6.0
// session_cache_limiter:决定 session_start 时会话扩展发送哪些 Cache-Control 头
// 模式:nocache / private / private_no_expire / public / ''(关闭自动发送)
// 与 cookie 参数一样,设置随请求重置,必须在 session_start() 之前调用
$prev = session_cache_limiter("private");
session_cache_expire(30); // 分钟;limiter 为 nocache 时只影响头发送与否
$lim = session_cache_limiter();
$exp = session_cache_expire();
$off = session_cache_limiter(""); // '' 完全关闭自动缓存头
$offOk = session_cache_limiter() === "";
$known = in_array($prev, ["", "nocache", "private", "private_no_expire", "public"], true);
echo "prev limiter known: ", var_export($known, true), "\n";
echo "limiter: ", $lim, "\n";
echo "expire: ", $exp, "\n";
echo "off ok: ", var_export($offOk, true), "\n";
echo "nocache headers: Cache-Control: no-store, no-cache, must-revalidate\n";
echo "private headers: Cache-Control: private, max-age=1800";
prev limiter known: true limiter: private expire: 30 off ok: true nocache headers: Cache-Control: no-store, no-cache, must-revalidate private headers: Cache-Control: private, max-age=1800
Python v3.15.2
# limiter 模式 → Cache-Control 值;框架层等价物是 Flask 的 after_request 钩子设置 response.headers["Cache-Control"]
modes = {
"nocache": "no-store, no-cache, must-revalidate",
"private": "private",
"public": "public",
}
limiter = "private"
expire = 30 # 分钟
print("limiter:", limiter)
print("expire:", expire)
print("private headers: Cache-Control: private, max-age=" + str(expire * 60))
print("nocache headers: Cache-Control: " + modes["nocache"])
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
Rust v1.99.1
fn main() {
// limiter 模式 → Cache-Control 值;框架层等价物是在 axum 里通过 HeaderValue 在响应体前插头
let modes = [
("nocache", "no-store, no-cache, must-revalidate"),
("private", "private"),
("public", "public"),
];
let limiter = "private";
let expire = 30; // 分钟
println!("limiter: {}", limiter);
println!("expire: {}", expire);
println!("private headers: Cache-Control: private, max-age={}", expire * 60);
let nocache = modes.iter().find(|(k, _)| *k == "nocache").unwrap().1;
println!("nocache headers: Cache-Control: {}", nocache);
}
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
TypeScript v26.13.1
// limiter 模式 → Cache-Control 值;框架层等价物是 express 的 res.set("Cache-Control", ...)(要在响应体之前)
const modes: Record<string, string> = {
nocache: "no-store, no-cache, must-revalidate",
private: "private",
public: "public",
};
const limiter: string = "private";
const expire: number = 30; // 分钟
console.log("limiter:", limiter);
console.log("expire:", expire);
console.log("private headers: Cache-Control: private, max-age=" + expire * 60);
console.log("nocache headers: Cache-Control: " + modes.nocache);
limiter: private expire: 30 private headers: Cache-Control: private, max-age=1800 nocache headers: Cache-Control: no-store, no-cache, must-revalidate
语言差异说明
C:CGI 层手写这些头(printf 进响应头区,头与正文间空行分隔);缓存策略是应用自己的职责。
C++:同 C;或框架层在响应组装处设置头映射。
C#:框架层是 ASP.NET 的 Response.Headers.CacheControl 或控制器上的 [ResponseCache] / HttpCachePolicy;语言级手写头串。
Go:标准库 w.Header().Set("Cache-Control", ...) + w.Header().Set("Expires", ...)——PHP limiter 模式就是这些头的固定组合;必须在写响应体前设置。
Java:Servlet 过滤器 response.setHeader("Cache-Control", ...)/setExpires;Spring Security 的配置类也是这组头的封装。
JavaScript:express 的 res.set("Cache-Control", ...) 或 helmet 中间件的 noCache 选项;框架中间件对应 PHP 的 limiter 概念。
PHP:五个模式决定 session_start() 自动发的头——nocache(默认)发 no-store, no-cache, must-revalidate + Pragma + 过去的 Expires;private 发过去的 Expires(1981 年那个固定值)+ private, max-age=expire*60;private_no_expire 是不发 Expires 的 private 变体(规避老浏览器困惑);public 发未来 Expires + public, max-age;limiter 设 '' 完全关闭。expire 以分钟计(默认 180),仅在 limiter 非 nocache 时有实际意义;两个设置都随请求重置,须在 session_start() 前调用。
Python:框架层在 Flask 的 after_request 或 Django 中间件里设置 response.headers["Cache-Control"];无内建 limiter 等价物。
Rust:axum 用 headers::CacheControl 构建器或中间件层统一插入;无内建 limiter。
TypeScript:同 JavaScript。