Silverstripe Module

Overview

Simpliest way how to use elefast on your website is use one of our developed modules. If you are user of Silverstripe, you can create own module based on elefast API or just use this module. It provides setting elefast header for cache and delete cache on elefast server.

Requirements

The only requirement to use this module is has own project build on silverstripe framework 3.0 and bigger. You don't need to use CMS. For all versions of framework you can use same version of module.

Installation

Installation of this module is same as installation other modules and plugins. Just copy content of downloaded zip file to your project to root file. Silverstripe automatic add all included classes to your project

Configuration

After create your profile on elefast.com set to your _config.php file security token from website.

ElefastClient::$token = '123456789abcdef123456789abcdef123456789abcdef';

Cache

If you want to cache HTTP or HTTPS requests on elefast server you have to add ho header.

header('X-Elefast-Cache: 3600');

This provides class ElefastExtension which is Extension. It has to be added on Controllers and is triggered in method onAfterInit. If you use CMS best way for you is probably add this on Extension on Page_Controller.

Page_Controller::add_extension('ElefastExtension');

Cache parameters are defined in Silverstripe Config class. Simpliest way to set parameters is use declarative YAML files. E.g. you can create Cache.yml file in your project config folder and define this simple cache rules.

ElefastExtension:
    HomePage_Controller:
        index: 1d
    Page_Controller:
        index: 600
list: 1h

This means that request on class HomePage_Controller with action index is cached for 1 day. Requests on Page_Controller are cached for 600 seconds on method index and for 1 hour on method list. Method list can also exists on HomePage_Controller but there request will be not cached. 

Uncache

You can uncache your website in your profile on elefast.com or you can add elefast uncache extension to dataobject and define rules. 

DataObject::add_extension('ElefastUncache');

Now you have planty of choises how to write uncache rules for your project. Uncache event are triggered after methods write or delete. If you using CMS also on classes extends SiteTree after publish and unpublish.

Each rule defined in config should refer to method which exists on current object and returns other object, string with url or array with urls to uncache. If you use simple this, uncache works with current object same as with methot returns object in $this. If defined rule returns object, url to uncache is taken from method Link on that object.

ElefastUncache:
    Page:
        onAfterPublish:
            - this.Link
            - this.Parent.Link
        onAfterUnpublish:
- this
            - this.Parent

In this example, after publish and unpublish will be uncached same urls, onAfterUnpublish is simplier declaration.

You can also use regular expressions to create url to uncache. As base you can define methods or link, regexp must be string. For use regular expressions you should keep following declaration.

ElefastUncache:
    Page:
        onAfterPublish:
        - { base: this, regexp: '*' }
            - { base: this.Parent, regexp: '?*' }
- { base: '', regexp: '.*list$'}
- { base: '', regexp: '*'}
  • first rule uncache all urls which started with current page url (probably it is current page and all it children)
  • second rule clear all links with get parameters on parent object
  • third rule clear all cached urls which ended with 'list'
  • fourth method clear whole website.

If you want to uncache returned objects by rules in config you can use this definitions.

ElefastUncache:
    Page:
        onAfterPublish:
- this.HomePage.uncache(onAfterUnpublish)
    HomePage:
        onAfterPublish:
            - this.uncache(onAfterUnpublish)
        onAfterUnpublish:
- this
- { base: this, regexp: '*' }

If you want to use elefast on maximum you should reduce uncache requests. This you can do with conditional uncache definitions. Uncache extension allow you to use predefined conditional methods or you can use your own.

ElefastUncache:
    Page:
        onAfterPublish:
            - { if: "changed(Title)", uncache: customUncacheMethod}
- { if: "has_changes", delete: this}
- { if: "field_is(Count, 1)", delete: this}
- { if: "change_from(Count, 0)", delete: this}
- { if: "change_to(Count, 1)", delete: this}
- { if: "has_changes", delete: this}
        onAfterUnpublish:
- { if: "my_own_method", delete: this.Parent}
- { if: "my_own_method2", uncache: customUncacheMethod}
customUncacheMethod:
- this
- this.Parent

As second parameter you can use key delete which can contains object, url or array of urls or you use key uncache which trigger defined uncache method defined .

You can also works with DataList. As list you have to define method which returns instances of DataList. Methods delete and uncache works same as on object and are aplied on each object in list. In following example both methods return same list of urls.

ElefastUncache:
    Page:
        onAfterPublish:
        - { list: this.ChildrenPage, delete: this }
        - { list: this.ChildrenPage, uncache: 'onAfterPublish'}
    ChildrenPage:
        onAfterPublish:
- this

In config you can also define names of classes which have to be uncached.

ElefastUncache:
    Page:
        onAfterPublish:
     - ChildrenPage

If class ChildrenPage exists and is derived from DataObject and has method Link this will uncache all urls from list created from this class.

Download

You can download elefast module from repositary

https://github.com/elefast/SilverStripe

or directly from our website

elefast.com