㈠ 阿里雲伺服器 Thinkphp緩存文件寫入失敗,是什麼原因
緩存文件寫入失敗,一般原因都是因為對緩存目錄不可寫許可權引起;
可使用linux命令:
chmod
-R
777
{網站目錄路徑}
㈡ 怎麼在php文件中添加緩存代碼
if(cache)
return cache
else
邏輯
set cache
㈢ php 寫入讀取緩存
你好,根據你提出的需求,數據量不大的情況下,最簡單的就是使用資料庫裡面的【事務】。
begin; //開啟事務
insert into....;
update.....
rollback; //出現錯誤回滾事務 (之前的所有操作都回到最初狀態)
commit; //全部正常執行,提交事務。(真正執行到資料庫)
另外,注意myisam資料庫引擎不支持事務。
㈣ php 緩存怎麼寫
PHP數據緩存常用頁面緩存 靜態緩存
php頁面緩存主要用到的是ob系列函數,如ob_start(),ob_end_flush(),ob_get_contents()
靜態緩存是指靜態化,直接生成HTML或XML等文本文件,有更新的時候重生成一次,適合於不太變化的頁面
給你個頁面緩存的例子
function page_cache($ttl = 0)
{
$ttl = $ttl ? $ttl : PAGE_TTL;//緩存時間,默認3600s
$contents = ob_get_contents();//從緩存中獲取內容
$contents = "<!--page_ttl:".(time() + $ttl)."-->n".$contents;
//加上自定義頭部:過期時間=生成時間+緩存時間
file_put_contents(PAGE_FILE, $contents);//寫入緩存文件中
ob_end_flush();//釋放緩存
}
㈤ thinkphp緩存文件寫入失敗是什麼原因
Thinkphp緩存文件寫入失敗,主要原因是沒有緩存文件夾(runtime)的管理許可權。
這種情況大部分出現在Linux伺服器中,Windows伺服器默認具備可寫許可權。
以Thinkphp5.0版本為例:
在Linux伺服器的情況下,它必須是具備可寫許可權才行,並且這種可寫許可權應該遞歸到所有子文件夾。
假設thinkphp放在 /home/www/thinkphp 目錄。
chomd -R 777 /home/www/thinkphp/runtime/
通過以上命令使runtime文件夾以及其子文件夾都具備可寫許可權。
㈥ php文件緩存類匯總
本文實例講述了php的文件緩存類。分享給大家供大家參考。具體分析如下:
緩存類是我們開發應用中會常用使用到的功能,下面就來給大家整理幾個php文件緩存類了,各個文件緩存類寫法不同,但在性能上會有區別,有興趣測試的朋友可測試一下這些緩存類。
例1
復制代碼
代碼如下:<?php
$fzz
=
new
fzz_cache;
$fzz->kk
=
$_SERVER;
//寫入緩存
//$fzz->set("kk",$_SERVER,10000);
//此方法不與類屬性想沖突,可以用任意緩存名;
print_r($fzz->kk);
//讀取緩存
//print_r($fzz->get("kk"));
//unset($fzz->kk);
//刪除緩存
//$fzz->_unset("kk");
var_mp(isset($fzz->kk));
//判斷緩存是否存在
//$fzz->_isset("kk");
//$fzz->clear();
//清理過期緩存
//$fzz->clear_all();
//清理所有緩存文件
class
fzz_cache{
public
$limit_time
=
20000;
//緩存過期時間
public
$cache_dir
=
"data";
//緩存文件保存目錄
//寫入緩存
function
__set($key
,
$val){
$this->_set($key
,$val);
}
//第三個參數為過期時間
function
_set($key
,$val,$limit_time=null){
$limit_time
=
$limit_time
?
$limit_time
:
$this->limit_time;
$file
=
$this->cache_dir."/".$key.".cache";
$val
=
serialize($val);
@file_put_contents($file,$val)
or
$this->error(__line__,"fail
to
write
in
file");
@chmod($file,0777);
@touch($file,time()+$limit_time)
or
$this->error(__line__,"fail
to
change
time");
}
//讀取緩存
function
__get($key){
return
$this->_get($key);
}
function
_get($key){
$file
=
$this->cache_dir."/".$key.".cache";
if
(@filemtime($file)>=time()){
return
unserialize(file_get_contents($file));
}else{
@unlink($file)
or
$this->error(__line__,"fail
to
unlink");
return
false;
}
}
//刪除緩存文件
function
__unset($key){
return
$this->_unset($key);
}
function
_unset($key){
if
(@unlink($this->cache_dir."/".$key.".cache")){
return
true;
}else{
return
false;
}
}
//檢查緩存是否存在,過期則認為不存在
function
__isset($key){
return
$this->_isset($key);
}
function
_isset($key){
$file
=
$this->cache_dir."/".$key.".cache";
if
(@filemtime($file)>=time()){
return
true;
}else{
@unlink($file)
;
return
false;
}
}
//清除過期緩存文件
function
clear(){
$files
=
scandir($this->cache_dir);
foreach
($files
as
$val){
if
(filemtime($this->cache_dir."/".$val)<time()){
@unlink($this->cache_dir."/".$val);
}
}
}
//清除所有緩存文件
function
clear_all(){
$files
=
scandir($this->cache_dir);
foreach
($files
as
$val){
@unlink($this->cache_dir."/".$val);
}
}
function
error($msg,$debug
=
false)
{
$err
=
new
Exception($msg);
$str
=
"<pre>
<span
style='color:red'>error:</span>
".print_r($err->getTrace(),1)."
</pre>";
if($debug
==
true)
{
file_put_contents(date('Y-m-d
H_i_s').".log",$str);
return
$str;
}else{
die($str);
}
}
}
?>