browscap / browscap-php

Officially supported Browscap for PHP
http://browscap.org/
MIT License
422 stars 82 forks source link

is the locking mechanism for the cache really working? #10

Closed croensch closed 6 years ago

croensch commented 10 years ago

I noticed this library uses a cache file. My experience with file caching in PHP is that simple things like file_put_contents() versus require() or file_get_contents() often fail in multi-core environments. Speaking a multi-core server with Apache in preforking mode.

I've seen it in other libraries for example Zend_Cache_File, breaking our production environment with empty cache files whenever two processes coincidentally collide. That's why i moved everything to php_apc for good.

Now this library does not provide other caching mechanisms, so i'd like to know what measures are taken to keep it running smoothly in a high load multi-core environment.

To demonstrate, run this in the background

<?php
while (true) {
    $i = rand(1000, 9999);
    file_put_contents('c.php', "<?php echo $i;", LOCK_EX);
}

while trying to run this

<?php require 'c.php';

on my CLI i get three results nothing, "Bus error" and a number.

mimmi20 commented 10 years ago

What cache would you like to use?

asgrim commented 10 years ago

It might be worth waiting for a PSR-n for caching? That way anyone can use any caching back end they want, thus making the library interoperable.

croensch commented 10 years ago

Other caches won't match the read speed of requireing a PHP file. So i have no preference. I've currently settled with ua-parser for which i have to generate the file only once.

I later found out that the folks at symfony discovered a way to atomically write files: https://github.com/symfony/Filesystem/blob/master/Filesystem.php Basically they file_put_contents() to a tempnam() and then rename() it. Maybe this trick could be incorporated into browscap-php somehow (e.g. rather as own implementation instead of being a dependency).

PSR-n caching would be cool too.

mimmi20 commented 10 years ago

Is someone working on a PSR-n caching interface?

asgrim commented 10 years ago

Yes, it is a WIP over at php-fig/fig-standards#149

cziegenberg commented 10 years ago

Just a note: Creating a temporary file and renaming it, can fail on Windows. In some very rare cases the created file does not yet exists when you try to work with it (e.g. want to rename it).

mimmi20 commented 6 years ago

@asgrim May we close this issue?

asgrim commented 6 years ago

@mimmi20 we already replaced with PSR simple cache, right?

mimmi20 commented 6 years ago

Yes, in V4.

asgrim commented 6 years ago

Ok great, yes this can be closed. Thanks!