PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Proper way to preload cache items with crontab #888

Closed monsefsolutions closed 1 year ago

monsefsolutions commented 1 year ago

What's your question ?

I have set up a cron task that 'preloads' cache items. However, I see that a different directory gets created called cache/cli, instead of the normal cache folder cache/[domain_name]. Is it possible to force PHP Fast Cache Manager to use the same cache/[domain_name] instead of creating its own folder? Because now items get preloaded in a separate cache folder that my browser does not seem to load (no hit after cron task is finished).

References (optional)

No response

Do you have anything more you want to share? (optional)

No response

github-actions[bot] commented 1 year ago

Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:

Geolim4 commented 1 year ago

Hello,

Use securityKey for that:

https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV4%CB%96%5D-Configuration-Options#file-based-drivers-options-

monsefsolutions commented 1 year ago

First of all, thank you for the quick response, I appreciate that! I have included the securityKey to the ConfigurationOption object. I will wait for the next cron run and let you know if it works. Thanks again.

monsefsolutions commented 1 year ago

I get the following error:

Message: 'Invalid option(s) for the config Phpfastcache\Config\ConfigurationOption: securityKey'

I use version 8.1 of PHP Fast Cache. If I look in lib\Phpfastcache\Config\ConfigurationOption.php I don't see the property securityKey. How to solve?

Geolim4 commented 1 year ago

It's because you used the wrong Configuration class (the generic one) x)

Use IOConfigurationOption of Phpfastcache\Drivers\Files\Config instead.

monsefsolutions commented 1 year ago

Thank you again. I only have a trait class IOConfigurationOptionTrait. How to use this i.c.m. with the generic ConfigurationOption class? I want to set the properties: path and securityKey.

marios88 commented 1 year ago

@monsefsolutions

If it helps you, this the current way i initialize the files cache ( i am on V8 )

self::$file = \Phpfastcache\CacheManager::getInstance('files',new \Phpfastcache\Drivers\Files\Config([
                'path' => sys_get_temp_dir(),
                'itemDetailedDate' => false,
                'htaccess' => false,
                'securityKey'=> 'YOUR_KEY_HERE',
            ]));
monsefsolutions commented 1 year ago

@marios88

Thank you! I will try it and post an update ASAP.

monsefsolutions commented 1 year ago

@marios88

Looks like it works, thank you! However, I now get a permission related error in the cron.log:

Warning: fopen([cache_dir]/Files/11/9d/119d86aff6cd70dcd16a047b72440cb2.txt): failed to open stream: Permission denied in [app_root]/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php on line 355

It only seems to go wrong for specific cached files, not for all files. Permissions for the folders are correct from what I can see. Any suggestions or tips how to debug and solve this? I'm struggling because it seems to only occur occassionally.

Geolim4 commented 1 year ago

It's an issue with your webserver not sharing the same file permissions than the crontab user.

On Wed, 11 Jan 2023, 2:53 pm Arash Monsef, @.***> wrote:

@marios88 https://github.com/marios88

Looks like it works, thank you! However, I now get a permission related error in the cron.log:

Warning: fopen([cache_dir]/Files/11/9d/119d86aff6cd70dcd16a047b72440cb2.txt): failed to open stream: Permission denied in [app_root]/vendor/phpfastcache/phpfastcache/lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php on line 355

It only seems to go wrong for specific cached files, not for all files. Permissions for the folders are correct from what I can see. Any suggestions or tips how to debug and solve this? I'm struggling because it seems to only occur occassionally.

— Reply to this email directly, view it on GitHub https://github.com/PHPSocialNetwork/phpfastcache/issues/888#issuecomment-1378785241, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKFGZ65B4VBCT6UBA36Y7LWR23L3ANCNFSM6AAAAAATVSDLAY . You are receiving this because you were assigned.Message ID: @.***>

marios88 commented 1 year ago

Yes, probably you run it as a different user from cli than the web server

eg cron runs as localUser web is www-data

after you set the correct permissions on your tmp folder,
your cron should look something like this

Replace with your required user sudo crontab -e -u www-data

SHELL=/bin/bash
0-58/2 * * * * /bin/echo 'CRON: tick' $(whoami) $(date) > /tmp/cron.www-data.tick.log 2>&1
1-59/2 * * * * /bin/echo 'CRON: tock' $(whoami) $(date) > /tmp/cron.www-data.tock.log 2>&1
monsefsolutions commented 1 year ago

Yes, probably you run it as a different user from cli than the web server

eg cron runs as localUser web is www-data

after you set the correct permissions on your tmp folder, your cron should look something like this

Replace with your required user sudo crontab -e -u www-data

SHELL=/bin/bash
0-58/2 * * * * /bin/echo 'CRON: tick' $(whoami) $(date) > /tmp/cron.www-data.tick.log 2>&1
1-59/2 * * * * /bin/echo 'CRON: tock' $(whoami) $(date) > /tmp/cron.www-data.tock.log 2>&1

Thank you, I will try this if I encounter the issue again. As I said, the issue seems to occur occasionally and I don't see it in the latest log.

A last question regarding the www-data crontab: thank you for the clear explanation. Does this solution affect my current crontab setup? Is it possible to run a cron task inside the current crontab (localUser) and switch there to the www-data user? I want to be sure that I'm not changing anything that I can't revert :)

marios88 commented 1 year ago

Personally i avoid switching users in crontab. But in case you want to, and the user has permission and you setup your sudoers correctly try something like this

* * * * * sudo su www-data -s /bin/bash -c 'cd /folder && php index.php' >> /tmp/cron.www-data.log 2>&1

crontab -e -> edit current user's crontab
crontab -e -u XXX -> edit XXX's crontab
Why bother switching users inside crontab?

Geolim4 commented 1 year ago

Hello !

I'm closing this issue since the crontab configuration is out of phpfastcache's scope :)

Have a nice day !