我正在寻找一些关于缓存 php 的信息。 所有我找到的信息都是在一定的时间间隔( 每几个小时) 缓存一个 php 文件 。 是否有办法为每50 个页面视图缓存 。 每50 个页面视图后, 缓存文件将过期 。
有谁知道吗?
提前感谢您!
我正在寻找一些关于缓存 php 的信息。 所有我找到的信息都是在一定的时间间隔( 每几个小时) 缓存一个 php 文件 。 是否有办法为每50 个页面视图缓存 。 每50 个页面视图后, 缓存文件将过期 。
有谁知道吗?
提前感谢您!
这里的解决方案 i 组合在一起, 而不是使用基于文件的缓存, 而是使用数据库, PDO sqlite (这样就很容易删除缓存文件数据库, 以清除所有缓存 ) 。
向底部看您可以看到它是如何运作的, 它在50次点击后将删除行并重定向, 这样它就可以生成新副本 。 希望它能帮助您 。
< 坚固> qqlite.cache. class.php 强 >
<?php
/**
* PDO sqlite cache class
* You can include( sqlite.cache.class.php ); this class
*/
class sqlite_cache{
private $db;
function __construct($dsn){
$this->dsn = $dsn;
$this->chkSetup();
}
/*Singleton Connect*/
private function connect(){
if (!$this->db instanceof PDO){
$this->db = new PDO($this->dsn);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
/*Raw Select*/
public function rawSelect($sql){
$this->connect();
return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
public function get($fieldname=null, $id=null){
$this->connect();
$sql = "SELECT * FROM cache WHERE $fieldname = :id";
$statement = $this->db->prepare($sql);
$statement->bindParam( :id , $id, PDO::PARAM_STR);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/*Insert*/
public function put($values){
$this->connect();
$fieldnames = array_keys($values[0]);
$sql = "INSERT INTO cache ";
$fields = ( .implode( , , $fieldnames). ) ;
$bound = (: .implode( , : , $fieldnames). ) ;
$sql .= $fields. VALUES .$bound;
$statement = $this->db->prepare($sql);
foreach($values as $vals){
$statement->execute($vals);
}
}
/*Update*/
public function update($fieldname, $value, $pk, $id){
$this->connect();
$sql = "UPDATE cache SET $fieldname = :value WHERE $pk = :id";
$statement = $this->db->prepare($sql);
$statement->bindParam( :id , $id, PDO::PARAM_STR);
$statement->bindParam( :value , $value, PDO::PARAM_STR);
$statement->execute();
}
/*Update Hits*/
public function add_hit($id){
$this->connect();
$sql = "UPDATE cache SET hits = hits + 1 WHERE url = :id";
$statement = $this->db->prepare($sql);
$statement->bindParam( :id , $id, PDO::PARAM_STR);
$statement->execute();
}
/*Delete*/
public function delete($id){
$this->connect();
$sql = "DELETE FROM cache WHERE url = :id";
$statement = $this->db->prepare($sql);
$statement->bindParam( :id , $id, PDO::PARAM_STR);
$statement->execute();
}
/*Database Setup*/
private function chkSetup(){
$dso = explode( : ,$this->dsn);
if(file_exists($dso[1])){
return;
}else{
$this->connect();
//Create Table
$sql ="CREATE TABLE cache (id INTEGER PRIMARY KEY,
title TEXT,
url TEXT,
hits INTEGER,
date INTEGER,
contents TEXT)";
$this->db->query($sql);
header("refresh:0;url=./");
die;
}
}
}
?>
<强度 > index.php 强度>
<?php
include( sqlite.cache.class.php );
$cache = new sqlite_cache( sqlite:./cache.db );
//Check if cache exists
$cache_result = $cache->get( url ,$_SERVER[ REQUEST_URI ]);
//Exists
if(!empty($cache_result)){
//Add Hit
$cache->add_hit($_SERVER[ REQUEST_URI ]);
//Delete If over 50 hits
if($cache_result[0][ hits ]>=50){
$cache->delete($_SERVER[ REQUEST_URI ]);
header( Location: .$_SERVER[ REQUEST_URI ]);
die;
}
echo $cache_result[0][ contents ];
}else{
//Generate your page contents ect
ob_start();
///////////////////////////////
//Your script code goes here
///////////////////////////////
echo Your content ;
//End your script code/////////
///////////////////////////////
$return = ob_get_contents();
ob_end_clean();
//Before output build values to put in cache
$cache_contents = array(array( id =>NULL,
title => Page Title ,
url =>$_SERVER[ REQUEST_URI ],
hits => 0 ,
date =>time(),
contents =>$return));
//Store cache
$cache->put($cache_contents);
echo $return;
}
?>
你可以去非常低的技术 找到解决这个问题的办法。
每次点击您的 URL 时, 您可以创建一个临时文件作为计数器 。 使用类似方式- NAME OF TRANSLATORS - NAME OF TRANSLATORS
// recursively remove a directory
function rrmdir($dir) {
foreach(glob($dir . /* ) as $file) {
if ($file != "." && $file != "..") {
if(is_dir($file))
rrmdir($file);
else
unlink($file);
}
}
rmdir($dir);
}
$fileCount = count(glob($tempfile_directory."/*"));
if ($fileCount >= $someLimit){
rrmdir($tempfile_directory); // clear the counter
mkdir($tempfile_directory);
// clear the cached server data and refresh.
}
touch($tempfile_directory . / . time()); // create a new dummy counter file
从 < a href=> 中借用的递归删除功能
当 $tempfile_ directory
中的文件数量大于或等于 $someLimit
时,文件夹将被清空,并更新缓存数据。
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...