## 模块缓存
- 缓存是应用比较广泛的,可以使用TPHP内置的缓存。
- 来源于 .env 中的 CACHE_DRIVER 设置
设置文件/html/www/demo/test/cache/_init.php内容如下
```
<?php
return function (){
$testCache = $this->cache(function (){
// 第一次执行
dump("You Come Here !");
return "Hello TPHP !";
});
// 清除上面缓存
// $this->unCache(); //或$this->unCache("")
dump($testCache);
// "username" 和 300秒 可选,默认60 * 60 = 360秒
$testCache = $this->cache(function (){
// 第一次执行
dump("My Name !");
return "OK !";
}, "username", 300);
// 清除上面缓存
// $this->unCache("username");
dump($testCache);
// 指向上一路径
dump($this->getCacheid("../"));
// 指向当前路径下的tphp路径
dump($this->getCacheid("tphp"));
// 指向上一路径下的tphp路径
dump($this->getCacheid("../tphp"));
};
```
打开网址[http://demo.tphp.com/test/cache](http://demo.tphp.com/test/cache)获得如下效果
第一次打开
![](/static/plugins/tphp/help/init_cache/cache1.png)
第二次打开
![](/static/plugins/tphp/help/init_cache/cache2.png)