Jump to content

Making an Advanced cache system


afshin.m

Recommended Posts

Hello,

 

I'm programming an advanced caching system that work with PHP but i have an small problem.

 

I want to know when the pages content changed to remove the page cache from cache folder and make a new cache file with new content.

 

i don't want to work with cache time...

 

Please help me, thanks a lot.  :)

Link to comment
Share on other sites

If you do not want to work with time, how do you want to implement that? Asking "when" means using some form of storing information about time. The only other alternative is a tag/category system - you tag the cached content and update the cache explicitely while adding something, requesting i.e. to remove the cache entries with the specified tag.

Link to comment
Share on other sites

If you do not want to work with time, how do you want to implement that? Asking "when" means using some form of storing information about time. The only other alternative is a tag/category system - you tag the cached content and update the cache explicitely while adding something, requesting i.e. to remove the cache entries with the specified tag.

 

Thank you dear Zyx,

 

Many caching systems work with time, it means renew the cache file every 5min etc.

But i don't want to do that, i want to renew the cache file only WHEN the page content changed...

 

Thank you again  ::)

Link to comment
Share on other sites

I'm not sure what kind of caching you're doing as I've seen a few different types.

One is where it pulls page data out of a database and creates a file. I think this is what you're doing.

You could always md5_file() or create an md5 checksum of the page data and have that stored somewhere.

It could check the md5 sum of the data and if it's not the same, it recaches the page.

Link to comment
Share on other sites

Why not just use the database? The cache you are proposing is replicating the functionality ayway, the only difference is that eg. MySQL is optimised to handle data, where as PHP isnt optimised for file handling.

 

Responding to a request with a static file is almost always more efficient than re-generating a new page dynamically upon each request.

Link to comment
Share on other sites

LOL Just it just occurred to me as I was about to reply that not needing time removes the time checking functions :-[

 

But the system the OP talks about it is not really a cache, its more of a flat file db

 

It sounds like a cache to me. It just seems the OP wants the cache flushed when the relevant data is changed.

Link to comment
Share on other sites

All friends, thanks for your responds.

 

Based on my research about caching systems, it's not possible to re-generate cache file only when the file changed, you should use a period (for example 5min) to re-generate cache file or remove all caches and make a new cache file.

 

This is true?  :shy:

Link to comment
Share on other sites

OP another way(s) that you could do this, if you haven't figure it out already, well I can list a few ways this could be done.

 

1) You can store an md5sum of all the data on the page in a database or file and check those on each page load.

2) If your pages are user generated pages from your own site, have the page update on the user saving the page.

Otherwise (similar to what I mentioned with the HTTP headers)

3) Use HTTP headers. (304 = not changed) and Etags.

 

That's all I can think of at the moment.

here are some links:

http://en.wikipedia.org/wiki/HTTP_ETag

http://en.wikipedia.org/wiki/List_of_HTTP_headers

http://php.net/manual/en/function.header.php

http://php.net/manual/en/function.md5.php

http://www.php.net/manual/en/function.md5-file.php

These two aren't necesarrily mentioned, but an alternative hashing construct in php,

http://www.php.net/manual/en/function.sha1-file.php

http://www.php.net/manual/en/function.sha1.php

 

And for the sake of 'you might find something else useful in this list'

http://www.php.net/manual/en/ref.strings.php

 

Link to comment
Share on other sites

OP another way(s) that you could do this, if you haven't figure it out already, well I can list a few ways this could be done.

 

1) You can store an md5sum of all the data on the page in a database or file and check those on each page load.

2) If your pages are user generated pages from your own site, have the page update on the user saving the page.

Otherwise (similar to what I mentioned with the HTTP headers)

3) Use HTTP headers. (304 = not changed) and Etags.

 

That's all I can think of at the moment.

here are some links:

http://en.wikipedia.org/wiki/HTTP_ETag

http://en.wikipedia.org/wiki/List_of_HTTP_headers

http://php.net/manual/en/function.header.php

http://php.net/manual/en/function.md5.php

http://www.php.net/manual/en/function.md5-file.php

These two aren't necesarrily mentioned, but an alternative hashing construct in php,

http://www.php.net/manual/en/function.sha1-file.php

http://www.php.net/manual/en/function.sha1.php

 

And for the sake of 'you might find something else useful in this list'

http://www.php.net/manual/en/ref.strings.php

 

Thanks for your useful response but i can you please explain HTTP_ETag and give some examples?

 

And about (304 = not changed), this is only relevant to client and browser cache as far as i know, right?

 

Thank you  :shy:

 

Link to comment
Share on other sites

And about (304 = not changed), this is only relevant to client and browser cache as far as i know, right?

 

Indeed this puts the cache on the client instead on the server. So, when a client asks for a particular page which has not been changed since his lasts visit you return a "304 Not Modified" header and the browser will use the cached page.

Link to comment
Share on other sites

so useful...i don't use this parameter in my caching system so when i refresh the page, nothing loaded from cache ;)

 

And just another question, is there any limitation for browser caching?

 

Thank you!  :D

 

Yes, users can turn it off. In which they will always retrieve the page.

Link to comment
Share on other sites

Thanks, good

 

Now i check a website with my caching system and also enable YSlow...

I get this errors:

 

Grade F on Add Expires headers

There are 93 static components without a far-future expiration date.

    * (no expires) http://localhost/cache/test_files/mod_thumbsup.css
...

 

I think this is because i redirect all images/js/css with .htaccess to another address like this :

css_minify.php?css=[cssaddress]
image_compress.php?image=[imageaddress]
js_packer.php?js=[jsaddress]

 

And also i don't set any expires header in my PHP files (in js_packer.php for example)

How can i set any expires header in my PHP files?

 

Thank you  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.