A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[PHP] Simple Cache Class

1.3k views · started by Chris ·
#1
[PHP] Simple Cache Class
<?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;
}
}
?>
#2
What is it used for?
#3
Digg wrote:
What is it used for?


Caching data to reduce the need of constant updating.

Google it.
#4
Its not a class if you don't teach it.
#5
Defy wrote:
Its not a class if you don't teach it.


PHP class commonly refereed to as OOP.

It is an object orientation structure :P

$Cache = new Cache();

$Cache->ReadFile();
#6
It doesn't clear the cache without rewriting it.
#7
GAMEchief wrote:
It doesn't clear the cache without rewriting it.


Yeah I know, It's meant for a constant use.

Like Image generations etc.
#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);
#9
GAMEchief wrote:
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);


My testing shows that the implosion method returns the same data faster.

All it does is take the file array and put it into one code.

I'm not sure if it removes line-breaks but if I remember correctly it does not.
#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.
#11
GAMEchief wrote:
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.


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.
#12
I mean a benchmark system in the framework, not a cache system.

e.g.
$fw->benchmark->start();
whatever();
$fw->benchmark->end();
#13
GAMEchief wrote:
I mean a benchmark system in the framework, not a cache system.

e.g.
$fw->benchmark->start();
whatever();
$fw->benchmark->end();


O.o, oh I thought you wanted to add a cache system :P.

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.)
#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.