Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear() wipes out other files #362

Closed
plweil opened this issue Sep 20, 2016 · 16 comments
Closed

clear() wipes out other files #362

plweil opened this issue Sep 20, 2016 · 16 comments

Comments

@plweil
Copy link

plweil commented Sep 20, 2016

I've been working with phpfastcache for only a short time. I'm using the Files driver and my config look like this:

$config = [
"storage"   => "files",
"path"      =>  "",
];

phpfastcache is creating and saving the cache just as I want it to. Apparently the default location is in a directory at the root level named [hostname]/files.

But if I run $cache->clear(), the cache directory is completely removed. Moreover, several other (very important) files at the root level get wiped out, including my composer.json and Gruntifile.js. The browser displays a warning:

Warning: unlink(): No such file or directory in /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Util/Directory.php on line 102

How can I set this up so that the cache gets cleared and all of my other files remain untouched?

@Geolim4
Copy link
Member

Geolim4 commented Sep 20, 2016

Hi,

You have to setup an absolute path for the cache, relative path are not recommended at all because it depend how the webserver set up the working directory.

Cheers,
Georges

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

I'm closing ticket, do not hesitate to reopen it if the bug persists.

Cheers,

Georges

@Geolim4 Geolim4 closed this as completed Sep 21, 2016
@plweil
Copy link
Author

plweil commented Sep 21, 2016

Ok. I've changed my path to sys_get_temp_dir()."/cache", which translates to something like:

/var/folders/nx/9f74ykx97yg0jm4yf30ysblc0000gn/T/cache/localhost_8002/files

I set the cache var:

$cache = CacheManager::getInstance('Files', $config);

The first time I run the script, the data gets cached in this dir, as expected.

Then I add clear() to see whether it works:

$cache = CacheManager::getInstance('Files', $config);
$cache->clear();

The cache in the above dir does appear to be cleared, but my browser returns the following error (below). Presumably I need to adjust permissions someplace, but I'm not sure where or why.

Fatal error: Uncaught exception 'phpFastCache\Exceptions\phpFastCacheDriverException' with message 'PLEASE CHMOD - 511 OR ANY WRITABLE PERMISSION!' in /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/PathSeekerTrait.php:159 Stack trace: #0 /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Drivers/Files/Driver.php(117): phpFastCache\Drivers\Files\Driver->getFilePath('news') #1 /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/StandardPsr6StructureTrait.php(57): phpFastCache\Drivers\Files\Driver->driverRead(Object(phpFastCache\Drivers\Files\Item)) #2 /www/apache/html/uw_home_redesign/public/index.php(39): phpFastCache\Core\DriverAbstract->getItem('news') #3 {main} thrown in /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/PathSeekerTrait.php on line 159

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

You may need to chmod the target directory in 0777

@Geolim4 Geolim4 reopened this Sep 21, 2016
@plweil
Copy link
Author

plweil commented Sep 21, 2016

I did change the permissions as suggested. No matter which dir I designate as the target directory ("path") in config; e.g., [site-root[/cache, clear() deletes the cache directory itself and all of it's contents. I would expect the contents to be gone, but the directory should remain, yes? Once it's gone, the permissions are irrelevant, because the directory no longer exists. It feels like a Catch 22. I'm not sure what I might be doing wrong here.

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

Yeah the directory is deleted this is a purpose to make sure that all files are being deleted.
The cache directory must be in a writable directory, just by curiosity which version are you using ?

@plweil
Copy link
Author

plweil commented Sep 21, 2016

Composer says v5.0.9. So I take it that the target directory must be embedded within another writable directory.... I can try that.

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

Also if your system temp dir is not writable by default there is maybe a huge configuration issue on your server !!

@plweil
Copy link
Author

plweil commented Sep 21, 2016

I'm just working in my local development environment. I have full control over the permissions. I just changed my $config to a subdirectory within the site root:

$config = [
"storage"   => "files",
"path"      =>  "/www/apache/html/uw_home_redesign/storage/cache",
];

where storage (and all of its contents) is writable:

drwxrwxrwx   3 pweil  _www    102 Sep 21 11:45 storage

The exact same thing happened as before when I ran clear(), except that the storage directory is still there (one small victory):

Fatal error: Uncaught exception 'phpFastCache\Exceptions\phpFastCacheDriverException' with message 'PLEASE CHMOD - 511 OR ANY WRITABLE PERMISSION!' in /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/PathSeekerTrait.php:159 Stack trace: #0 /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Drivers/Files/Driver.php(117): phpFastCache\Drivers\Files\Driver->getFilePath('news') #1 /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/StandardPsr6StructureTrait.php(57): phpFastCache\Drivers\Files\Driver->driverRead(Object(phpFastCache\Drivers\Files\Item)) #2 /www/apache/html/uw_home_redesign/public/index.php(39): phpFastCache\Core\DriverAbstract->getItem('news') #3 {main} thrown in /www/apache/html/uw_home_redesign/vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/PathSeekerTrait.php on line 159

It should be getting the path correctly; otherwise it wouldn't be able to write the cache in the first place. Somehow the getFilePath method in PathSeekerTrait.php is either getting the wrong path, thinks it's not writeable, or....I don't know what. There's a disconnect somewhere.

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

The Chmod is maybe not the only one issue, is the Webserver running under the same user ?
Usually on Unix environment it runs under the www-data user.
You may need to chown the cache dir to your webserver user.

@plweil
Copy link
Author

plweil commented Sep 21, 2016

PHP is running with me as the user (remember, this is just a dev environment). All of these directories need to be owned by me. If I change the owner to _www then phpfastcache complains that FIles is configured incorrectly. If PathSeekerTrait.php thinks that there's still a directory that still needs to be made writeable, there must be a way to tell which one it complaining about.

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

In that case I don't know where the issue is from, this is maybe env-dev specific but I am currently not able to reproduce this issue. Since the Files driver is the most used, i think that a lot of people would reported the issue before :'(

@khoaofgod
Copy link
Member

/www/apache/html/uw_home_redesign/storage/cache <-- you need cache is writeable , not storage.

@plweil
Copy link
Author

plweil commented Sep 21, 2016

I appreciate everyone's patience to stick with me here. As I explained above, I can set permissions to cache to 0777 or anything else, but once it gets deleted, it's permissions are no longer relevant. When I run clear(), the error appears even when cache is writeable.

In trying to debug this, what is getFilePath() in PathSeekerTrait.php trying to do when it is trying to write something? clear() deletes the entire cache directory, but the $path variable it is testing is:

/www/apache/html/uw_home_redesign/storage/cache/localhost_8002/files/50

which contained the actual cached text file.

But that directory and file already have been deleted by clear(). So what's supposed to be going on here?

@plweil
Copy link
Author

plweil commented Sep 21, 2016

I think I may have figured it out, or at least have a workaround. I realize that what I was testing was an artificial situation, so I just inserting cache->clear() as described above. The code that follows essentially checks to see whether anything is in the cache, and either retrieve it or save some new data to the cache. What I had to do is clear or reset $cache within this block; e.g.:

$cache = CacheManager::getInstance('Files', $config);
$news_item = $cache->getItem("news");

Then the problem goes away.

I suspected something like this was going on a little while ago, but it took me a little time to think through it. I should have seen it sooner. Phew.

In this project, the clear() method likely will live in a separate script (triggered by a url request), and will not be commingled with the script that writes to the cache.

Thanks Geolim4 and others for hanging with me.

@Geolim4
Copy link
Member

Geolim4 commented Sep 21, 2016

Fine then,
Happy coding :)
Georges

@Geolim4 Geolim4 closed this as completed Sep 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants