PHP Code:
<?php
class Cache{
public $CacheFile;
public $CacheLife;
function __construct($CacheFile = "Cache.txt", $CacheLife = 10){
$this->CacheFile = $CacheFile;
$this->CacheLife = $CacheLife;
}
function WriteCache($Content){
$Handle = fopen($this->CacheFile, 'w');
fwrite($Handle, $Content);
fclose($Handle);
}
function ReadCache(){
if(file_exists($this->CacheFile)){
if((time() - ($this->CacheLife * 60)) > filemtime($this->CacheFile)){
return false;
}
$Cache = file($this->CacheFile);
return implode('', $Cache);
}
return false;
}
}
?>
Results 1 to 14 of 14
Thread: [PHP] Simple Cache Class
- 16 Nov. 2009 05:53am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 500.00
[PHP] Simple Cache Class
- 17 Nov. 2009 12:23am #2
What is it used for?
- 17 Nov. 2009 04:42am #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
- 17 Nov. 2009 04:43am #4
Its not a class if you don't teach it.
- 17 Nov. 2009 04:48am #5
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
- 17 Nov. 2009 05:49am #6
It doesn't clear the cache without rewriting it.
- 18 Nov. 2009 04:06am #7
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
- 18 Nov. 2009 05:09am #8
Cool.
Why did you do this?
$Cache = file($this->CacheFile);
return implode('', $Cache);
Wouldn't that fuck up files that have line-breaks?
I'm thinking it should be return file_get_contents($this->CacheFile);
- 18 Nov. 2009 05:23am #9
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
- 18 Nov. 2009 06:21am #10
It does, unless file() leaves \n at the end of each array value. On second thought, I'm thinking it does.
Not sure how file() is faster than file_get_contents(), since it returns the same thing, except in an array instead of a string. I imagine the time it took to split into an array would lag it compared to just returning the data.
If you've benchmarked it, I'd love to see the results. I'll definitely wanna do this once I finish my benchmark extension to my framework, as this is relevant to my interests.
- 18 Nov. 2009 06:24am #11
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
I had the data, it proved if I remember correctly to be about 1.3 ms faster. It wasn't a lot but that was the average.
I do not have the data because the classes I released are quite old (About 5 months,) and all the old data I had was gone.
To tell the truth I made the code so long ago that I am sure it could use some major updating. As for using it in a framework you will need to majorly change it to make it valuable.
- 18 Nov. 2009 07:18am #12
I mean a benchmark system in the framework, not a cache system.
e.g.
$fw->benchmark->start();
whatever();
$fw->benchmark->end();
- 18 Nov. 2009 07:29am #13
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
, oh I thought you wanted to add a cache system .
Yeah benchmarking is a major need when it comes to any type of pre-built system.
I advice you make a system that allows multiple executions of the code to evaluate a mean time.
Because of the fact that the human error (Can't execute the code with the precise alignment of the timer.)
- 18 Nov. 2009 02:01pm #14
I was thinkin'
$fw->benchmark->start(1);
whatever(1);
$fw->benchmark->end(1);
$fw->benchmark->start(2);
whatever(2);
$fw->benchmark->end(2);
$fw->benchmark->compare(1, 2);
Or something along those lines.
EDIT:
Nevermind. There's a topic for the framework. Let's stay on topic here, eh.