|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
eschew sesquipedalians
![]() Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,795
|
I wrote a class to implement server side data caching that I have used for several projects at work. I decided to give a presentation on data caching (Day 3-9:30) and consequently rewrote the class. I ended up putting enough effort into getting the class right for the presentation that I thought it merited release as an open source project (I hope you will agree
).Please have a look at the project homepage and especially the project documentation. The idea here is that the DataCache object represents a very simple API to persist data. Here is a sample example of how the cache object can be used: PHP Code:
SourceForge's file upload was broken, so I will attached a tarball to this post for people interested in the source. Thanks.
__________________
Jason Sweat ZCE - jsweat_php@yahoo.com Book: PHP Patterns Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI Detestable (adjective): software that isn't testable. Last edited by sweatje; Dec 11, 2003 at 19:24.. Reason: spelling :( |
|
|
|
|
|
#2 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Apr 2002
Posts: 331
|
The problem of many data cache solutions including yours is that they are not robust enough to work in a multi-user environment. What happens is that if two users try simultaneously to access the cache this is outdated or non-existing, chances are that both will corrupt the data that each other tries to store in the cache.
The correct solution employs locking schemes that prevent more than one cache writer or that the cache is updated while there are any cache reader processes. You may want to look at this file data cache class that uses file locks to prevent the problems above.
__________________
Manuel Lemos Metastorage - Data object relational mapping layer generator PHP Classes - Free ready to use OOP components in PHP Last edited by mlemos; Dec 12, 2003 at 05:04.. Reason: fixed URL |
|
|
|
|
|
#3 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2003
Location: Mainz, Germany
Posts: 122
|
Quote:
__________________
Things that try to look like Things, sometimes
look more like Things than Things. - Granny Weatherwax |
|
|
|
|
|
|
#4 | |
|
eschew sesquipedalians
![]() Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,795
|
Quote:
PHP Code:
|
|
|
|
|
|
|
#5 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Apr 2002
Posts: 331
|
Quote:
What is your point? Censorship?!?!
__________________
Manuel Lemos Metastorage - Data object relational mapping layer generator PHP Classes - Free ready to use OOP components in PHP |
|
|
|
|
|
|
#6 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Apr 2002
Posts: 331
|
Quote:
If you verify that the cache needs to be update, you should lock it from update before you start writing to it. Your code has several problems. Your verification code consists just of a file_exists() call. Two simultaneous processes can verify that the cache needs to be updated and so both will try to write it. Furthermore, if for some reason your process exits during the cache update, it will leave a corrupt cache file that with a single file_exists() call will validate as if the cache file is not corrupt. There are probably more problems that I did not analyse.
__________________
Manuel Lemos Metastorage - Data object relational mapping layer generator PHP Classes - Free ready to use OOP components in PHP |
|
|
|
|
|
|
#7 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2003
Location: Wixom, Michigan
Posts: 591
|
Quote:
|
|
|
|
|
|
|
#8 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2003
Location: Mainz, Germany
Posts: 122
|
Quote:
I will not lead any discussion on this basis.
__________________
Things that try to look like Things, sometimes
look more like Things than Things. - Granny Weatherwax |
|
|
|
|
|
|
#9 | ||
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2003
Location: Mainz, Germany
Posts: 122
|
Quote:
Quote:
I might have a look at this language when I have some time to kill, but after seeing several multi-million-dollar attempts at "meta-programming" fail, I beg to be allowed to have my doubts, preferably without being accused of censorship. |
||
|
|
|
|
|
#10 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Apr 2002
Posts: 331
|
Quote:
MetaL is a PHP application but that is not relevant either. What is relevant that it can generate standalone PHP code that is exactly what I would produce if I would write it directly in PHP. This means that for those that use it, it is irrelevant how I created the class code. It also means that the generated code does not depend on any external libraries. Any PHP version would do. Even if you want to change the code, nothing stops you for editing the class PHP code. The license is BSD and by that license you are free to modify and distribute modified versions of the class. As a matter of fact I have received many suggestions to improve the class in the form changed versions of the class in PHP. Anyway, I just mentioned the class as an example of doing proper cache locking to prevent access concorrency problems. I do not see how the access to the original MetaL source code would help, but if you think that is relevant, MetaL is a fully Open Source project and its source is available for download, including the MetaL source for the file cache class.
__________________
Manuel Lemos Metastorage - Data object relational mapping layer generator PHP Classes - Free ready to use OOP components in PHP |
|
|
|
|
|
|
#11 | |
|
eschew sesquipedalians
![]() Join Date: Jun 2003
Location: Iowa, USA
Posts: 3,795
|
Quote:
|
|
|
|
|
|
|
#12 | |||
|
SitePoint Addict
![]() ![]() ![]() Join Date: Apr 2002
Posts: 331
|
Quote:
In the PHP Classes site there is a whole section for PHP cache classes but most of them have the same reentrancy problem as yours. Quote:
However, better than caching variables eventually taken from database query results, I recommend that you cache the actual HTML pages or excerpts that you would generate from those variables values. This way, you also avoid recomputing the PHP code that generates that HTML. Quote:
Anyway, I don't see much sense in caching data in databases unless you are caching something much slower to compute than a database query. As a matter of fact, usually you use file or shared memory based caches to avoid database accesses. I see your point regarding server farms but often is better to rely on the database server caches, like MySQL 4 has now.
__________________
Manuel Lemos Metastorage - Data object relational mapping layer generator PHP Classes - Free ready to use OOP components in PHP |
|||
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 21:13.




).





Linear Mode
