Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Static Cache
  • Hi I'd like to know if there's something planned about static cache, i mean, caching the output in physical files and loading them with .htaccess (garbage should be done with a cron or with controllers) I've been developing in Kohana in the past, but i found Fuel more complete and clear and i want to contribute code with you. If nobody is developing a static cache feature, i'd like to create a package to handle this.
  • For anyone who's interested, I'm doing this in memcache. I use nginx, which has a Memcached module, and it handles serving static pages out of memcache without ever sending a request to php-fpm. I have an abstract controller, Controller_Cacheable which extends Controller_Template (only because I use that controller anyway, you don't really need Controller_Template). Its after() method basically just sets a key called 'page:/request_path/foo' with the output of $this->template->render() The controller has a property called $cache_ttl that defaults to 3600, and individual methods can override it if they need longer/shorter TTLs. Then in nginx:
     location ~ \.php$ {
      set $memcached_key "page:$request_uri";
      memcached_pass 127.0.0.1:11211;
      default_type text/html;
      error_page 404 405 502 = @cache_miss;
     }
    
     location @cache_miss {
      fastcgi_index index.php;
      include /etc/nginx/fastcgi_params;
      fastcgi_param FUEL_ENV production;
      fastcgi_pass 127.0.0.1:9000;
     }
    

    If the page doesn't exist in memcache (either because it's expired or it's not set to be cached at all) nginx will just serve the page as usual. Works pretty well, and it was much easier than using something like Varnish. One annoying thing is that Fuel's Cache class won't allow keys with slashes, so I just implemented a quick Cache class of my own to do the work.
  • At last, i've setup a repo to host the 'fuel-pagecache' package. This is the URL:
    https://github.com/xavividal/fuel-pagecache In short, this is a port of Lysender's pagecache kohana module, but i rewrote a lot of parts and i'm planning to add some enhancements like expired deletion, stats, .. This days i'll add more doc to let people setup the package without problems.
  • Hi. Thanks for this package. It worked for me! However, I have 2 servers -- an Apache and a Nginx server. It worked on Apache. I'd like to convert my ap to Nginx totally. Will this work on Nginx?
  • Yes, I know the Cache component but i'm talking about integrate in the execution flow the code to save the response in cache (file, redis or whatever) in a centralized way. Take a look at: http://blog.lysender.com/2010/06/php-page-caching-serving-static-files/ The goal here is to learn about this Kohana module and think about integrate in the response workflow to get this task automated. I mean, in the controller we could do this: $this->enableCache(86400); And if the request is GET, etc... the response can be saved in a static file in the webroot. Is the same principle as SuperCache from Wordpress.
  • Doesn't exist in the core or as a third party package/class as far as I know. How do you plan to handle custom headers for such a cache? Only cache status 200 responses? Or still use plain PHP to return those as well?
  • This is pretty easy to make. Extend the Request class in your app. Overload the execute function. Use $this->route to determine a unique cache-id per URI. Do a cache lookup with this id. If exists, set the response object with the cached data as response body. If it doesn't exist, call parent::execute to create the new response. Upon return, cache the body with the determined cache-id. Takes about 15 minutes I guess...
  • Another strategy could be the as Symfony people did creating a reverse proxy with PHP, easily replaced with Varnish, Squid or another one.
  • I actually kinda liked the .htaccess proposal, that would cut PHP out giving you the maximum possible performance. If you do something like that it should only cache:
    - when the response status is 200
    - when no headers about HTTP caching were found It has limitations but really speeds up your system and makes it something like a publishing system, or close enough that it'd only need a few tweaks to become one.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion