日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php如何存到磁盘,php缓存----磁盘缓存

發(fā)布時間:2025/3/20 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php如何存到磁盘,php缓存----磁盘缓存 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

PHP開發(fā)也有幾個月了,感覺php緩存是很重要的一塊,無論是頁面級的(主要指smarty)還是dao級的。使用上還不怎么熟,但還是記錄下筆記。本篇講述的緩存是寫到磁盤文件,這是看piwik源碼時看到的,感覺思想很好,這也得益于PHP的var_export 方法。主要把要保存的內(nèi)容(int,string,也可以使array)保存為php文件,這樣當include這個php文件后,保存的內(nèi)容就自動當做變量被include進來了。

源碼如下:

class Piwik_CacheFile

{

protected $cachePath;

protected $cachePrefix;

function __construct($directory)

{

$this->cachePath = PIWIK_USER_PATH . '/tmp/cache/' . $directory . '/';

}

function get($id)

{

$cache_complete = false;

$content = '';

// We are assuming that most of the time cache will exists

$ok = @include($this->cachePath . $id . '.php');

if ($ok && $cache_complete == true) {

return $content;

}

return false;

}

function set($id, $content)

{

if( !is_dir($this->cachePath))

{

Piwik_Common::mkdir($this->cachePath);

}

if (!is_writable($this->cachePath)) {

return false;

}

$id = $this->cachePath . $id . '.php';

$cache_literal = "

$cache_literal .= "$"."content = ".var_export($content, true).";\n\n";

$cache_literal .= "$"."cache_complete = true;\n\n";

$cache_literal .= "?".">";

// Write cache to a temp file, then rename it, overwritng the old cache

// On *nix systems this should guarantee atomicity

$tmp_filename = tempnam($this->cachePath, 'tmp_');

if ($fp = @fopen($tmp_filename, 'wb')) {

@fwrite ($fp, $cache_literal, strlen($cache_literal));

@fclose ($fp);

if (!@rename($tmp_filename, $id)) {

// On some systems rename() doesn't overwrite destination

@unlink($id);

if (!@rename($tmp_filename, $id)) {

// Make sure that no temporary file is left over

// if the destination is not writable

@unlink($tmp_filename);

}

}

return true;

}

return false;

}

function delete($id)

{

$filename = $this->cachePath . $id . '.php';

if (file_exists($filename)) {

@unlink ($filename);

return true;

}

return false;

}

}

總結(jié)

以上是生活随笔為你收集整理的php如何存到磁盘,php缓存----磁盘缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。