Template caching based on urls not DB queries.
doctrine/cache
library, providing many cache backends<% cache_include 'TemplateName' %>
syntax in templates<% cache %><% end_cache %>
syntax in templatesFor a SilverStripe 2.4
compatible version, see the 2.0.4
tag.
$ composer require heyday/silverstripe-cacheinclude:~5.0
To be able to invalidate caches from DataObject writes, add the InvalidationExtension
:
mysite/_config/caching.yml
---
After: 'silverstripe-cacheinclude/*'
---
DataObject:
extensions:
- Heyday\CacheInclude\SilverStripe\InvalidationExtension
Cache a section of a template:
<% cache 'SomeCacheBlock' %>
<% loop ExpensiveSet %><% end_loop %>
<% end_cache %>
Cache an included template (assumes a cache block config name of SomeTemplateName
):
<% cache_include 'SomeTemplateName' %>
Cache an included template with a different cache block config name:
<% cache_include 'App\Includes\SomeTemplateName', 'CacheBlockConfigName' %>
For each cache block that is used, you need a corresponding config provided to CacheInclude
.
The following is an example of a config for SomeCacheBlock
and AnotherCacheBlock
:
mysite/_config/caching.yml
---
After: 'silverstripe-cacheinclude/*'
---
Injector:
CacheIncludeConfig:
class: Heyday\CacheInclude\Configs\ArrayConfig
properties:
Config:
SomeCacheBlock:
context: full
contains:
- MyDataObject
AnotherCacheBlock:
context: no
expires: +1 hour
Key creation options:
context
Context is a method to tell the key creator what information about the request to include in the created key.
Possible values:
no
page
full
expires
Possible values:
member
Possible values:
true
any
versions
Possible values:
This is useful for when a cache block contains random content, but you still want caching.
e.g. set to 20 to get 20 (potentially) different version of a cache block.
Cache invalidation options
contains
invalidation_rules
The Expression Language is provided by Symfony, but also has the following available:
item
action
list()
instanceof()
Theses can be used to do the following:
invalidation_rules:
- "instanceof(item, 'CreativeProfile') and item.ID in list('CreativeProfile').sort('Created DESC').limit(4).getIDList()"
CacheInclude comes with a RequestCache
service that can be added to cache full request objects for use in high load
sites.
To enable the full request cache, the RequestCacheMiddleware
needs to be applied and a Global
config block needs to be created:
---
After: '#cacheinclude'
---
SilverStripe\Core\Injector\Injector:
CacheIncludeConfig:
class: Heyday\CacheInclude\Configs\ArrayConfig
properties:
Config:
Global:
contains:
- SilverStripe\CMS\Model\SiteTree
context: full
expires: '+ 1 hour'
---
After: '#coresecurity'
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director:
properties:
Middlewares:
RequestCacheMiddleware: '%$RequestCacheMiddleware'
Note: the After:
condition in the above example is important. Without it, the middleware that handles request caching will
run before SilverStripe's authentication middleware - meaning that the current user (stored in the session) isn't available. This
could result in cache contamination between users, or between guests & registered users.
Full request caching increases performance substantially but it isn't without a cost. It can be hard to configure, as there
are numerous cases where you don't want to either cache a request or alternatively serve a cached request.
To help in this there is quite a bit you can do out of the box to configure the way that caching is handled.
The following gives some demonstration of how to configure things and what you can do:
Injector:
RequestCacheMiddleware:
class: 'Heyday\CacheInclude\SilverStripe\RequestCacheMiddleware'
constructor:
0: '%$CacheInclude'
1: '%$CacheIncludeExpressionLanguage'
2: 'Global'
properties:
Tokens:
- '%$SilverStripe\Security\SecurityToken'
SaveExcludeRules:
- 'request.getUrl() matches "{^admin|dev}"'
SaveIncludeRules:
- "request.httpMethod() == 'GET'"
- "response.getStatusCode() == 200"
FetchExcludeRules:
- 'request.getUrl() matches "{^admin|dev}"'
FetchIncludeRules:
- "request.httpMethod() == 'GET'"
As you can see above there are some variables made accessible to you in the expression language.
The following is made available in the "Save" rules:
request
response
member
session
The following is made available in the "Fetch" rules:
request
member
session
Additional variables can be provided through the injector system.
Injector:
RequestCacheMiddleware:
properties:
ExtraExpressionVars:
'hello': 'Something'
Because of the heavy usage of dependency injection and the SilverStripe Injector
component, most parts
of CacheInclude
can be completely customised by replacing the standard classes with ones of your own.
CacheInclude
comes built in with one key creator Heyday\CacheInclude\KeyCreators\ControllerBased
.
This key creator makes keys based on the config supplied in yaml, the current request and the environment.
You can create your own key creators by extending the KeyCreatorInterface
and specifying the creator's service name from the template.
<% cache 'SomeBlock', 'MyKeyCreator' %>
Some content
<% end_cache %>
class MyKeyCreator implements \Heyday\CacheInclude\KeyCreators\KeyCreatorInterface
{
public function getKey($name, $config)
{
return [
'key',
'parts'
];
}
}
SilverStripe CacheInclude is released under the MIT license
$ composer install --prefer-dist --dev
$ phpunit
This project follows the standards defined in:
Run the following before contributing:
$ php-cs-fixer fix .
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem