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

Ss4 upgrade #79

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -66,4 +66,4 @@ checks:
argument_type_checks: true

filter:
paths: [code/*, tests/*]
paths: [src/*, tests/*]
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ server heavy operations such as database access.

## Requirements

* SilverStripe 3.1.7 or above
* PHP 5.4
* SilverStripe 4.*
* PHP 5.6

## How it works

@@ -39,14 +39,7 @@ form submissions) by checking for any url-segments that start with an uppercase
* Either extract the module into the dynamiccache folder, or install using composer

```bash
composer require "tractorcow/silverstripe-dynamiccache" "4.2.*"
```

* Edit your .htaccess (or web.config, etc) to redirect requests to the dynamiccache/cache-main.php
file instead of the framework/main.php file

```apache
RewriteRule .* dynamiccache/cache-main.php?url=%1&%{QUERY_STRING} [L]
composer require "tractorcow/silverstripe-dynamiccache" "dev-ss4-upgrade"
```

## Configuration options
@@ -99,7 +92,7 @@ If this should be done in response to a conditional, or non-dataobject related a
the cache with the below code:

```php
DynamicCache::inst()->clear();
\TractorCow\DynamicCache\DynamicCacheMiddleware::inst()->clear();
```

Additionally, if you are logged in (or are in dev mode) the cache can be flushed by adding the 'cache=flush' query
@@ -118,7 +111,7 @@ mobile / non-mobile users (assuming silverstripe/mobile module is installed).
```php

class CacheCustomisation extends DynamicCacheExtension {
public function updateEnabled(&$enabled) {
public function updateEnabled(&$enabled, HTTPRequest $request) {
// Disable caching for this request if a user is logged in
if (Member::currentUserID()) $enabled = false;

@@ -127,7 +120,7 @@ mobile / non-mobile users (assuming silverstripe/mobile module is installed).

// Disable caching for this request if we have a message to display
// or the request shouldn't be cached for other reasons
elseif (Session::get('StatusMessage') || Session::get('Uncachable')) $enabled = false;
elseif ($request->getSession()->get('StatusMessage') || $request->getSession()->('Uncachable')) $enabled = false;
}

public function updateCacheKeyFragments(array &$fragments) {
19 changes: 19 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by [email protected]
* Date: 9/20/18
* Time: 4:10 PM
*/

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
use TractorCow\DynamicCache\DynamicCacheMiddleware;

if (DynamicCacheMiddleware::config()->logHitMiss) {
$logger = Injector::inst()->get(LoggerInterface::class);
if ($logger instanceof Logger) {
$logger->pushHandler(new StreamHandler(BASE_PATH.'/dynamic_cache.log', Logger::INFO));
}
}
14 changes: 14 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: dynamiccache_config
---

SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director:
properties:
Middlewares:
DynamicCacheMiddleware: '%$TractorCow\DynamicCache\DynamicCacheMiddleware'
Psr\SimpleCache\CacheInterface.DynamicCacheStore:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
defaultLifetime: 600
namespace: "DynamicCacheStore"
2 changes: 1 addition & 1 deletion _config/dynamiccache.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Name: dynamiccache
---
DynamicCache:
TractorCow\DynamicCache\DynamicCacheMiddleware:
# Global override. Turn to false to turn caching off.
enabled: true
# If a header should be used to opt in to caching, set the regular expression
12 changes: 6 additions & 6 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
Name: dynamiccacheextensions
---
SiteTree:
SilverStripe\CMS\Model\SiteTree:
extensions:
- 'DynamicCacheDataObjectExtension'
SiteConfig:
- 'TractorCow\DynamicCache\Extension\DynamicCacheDataObjectExtension'
SilverStripe\SiteConfig\SiteConfig:
extensions:
- 'DynamicCacheDataObjectExtension'
Page_Controller:
- 'TractorCow\DynamicCache\Extension\DynamicCacheDataObjectExtension'
PageController:
extensions:
- 'DynamicCacheControllerExtension'
- 'TractorCow\DynamicCache\Extension\DynamicCacheControllerExtension'
89 changes: 0 additions & 89 deletions cache-main.php

This file was deleted.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tractorcow/silverstripe-dynamiccache",
"description": "Silverstripe module for simple on the fly caching of dynamic content",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "cache", "caching", "performance"],
"license": "BSD-3-Clause",
"authors": [
@@ -14,8 +14,8 @@
"issues": "http://github.com/tractorcow/silverstripe-dynamiccache/issues"
},
"require": {
"silverstripe/framework": "^3.1.7",
"silverstripe/cms": "^3.1.7",
"silverstripe/framework": "4.*",
"silverstripe/cms": "4.*",
"composer/installers": "*"
},
"extra": {
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

namespace TractorCow\DynamicCache\Dev;

use SilverStripe\Dev\BuildTask;
use TractorCow\DynamicCache\DynamicCacheMiddleware;


/**
* Clears the cache
*
@@ -13,7 +19,7 @@ class DynamicCacheClearTask extends BuildTask
protected $description = "This task clears the entire DynamicCache";

public function run($request) {
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
echo 'DynamicCache has been cleared.';
}
}
392 changes: 186 additions & 206 deletions code/DynamicCache.php → src/DynamicCacheMiddleware.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

namespace TractorCow\DynamicCache\Extension;

use SilverStripe\Core\Extension;
use SilverStripe\Security\Permission;
use TractorCow\DynamicCache\DynamicCacheMiddleware;


/**
* Dynamic caching enhancements for page controller
*
@@ -12,7 +19,7 @@ public function onBeforeInit()
{

// Determine if this page is of a non-cacheable type
$ignoredClasses = DynamicCache::config()->ignoredPages;
$ignoredClasses = DynamicCacheMiddleware::config()->ignoredPages;
$ignoredByClass = false;
if ($ignoredClasses) {
foreach ($ignoredClasses as $ignoredClass) {
@@ -27,13 +34,13 @@ public function onBeforeInit()
// - current page is an ignored page type
// - current_stage is not live
if ($ignoredByClass) {
$header = DynamicCache::config()->optOutHeaderString;
$header = DynamicCacheMiddleware::config()->optOutHeaderString;
header($header);
}

// Flush cache if requested
if (isset($_GET['cache']) && ($_GET['cache'] === 'flush') && Permission::check('ADMIN')) {
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

namespace TractorCow\DynamicCache\Extension;

use SilverStripe\ORM\DataExtension;;

use SilverStripe\Versioned\Versioned;
use TractorCow\DynamicCache\DynamicCacheMiddleware;


/**
* Ensures that dataobjects are correctly flushed from the cache on save
*
@@ -16,7 +24,7 @@ class DynamicCacheDataObjectExtension extends DataExtension
*/
public function onAfterWrite()
{
if (!DynamicCache::config()->cacheClearOnWrite) {
if (!DynamicCacheMiddleware::config()->cacheClearOnWrite) {
return;
}

@@ -30,7 +38,7 @@ public function onAfterWrite()
return;
}

DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}

/**
@@ -40,50 +48,53 @@ public function onAfterWrite()
*/
public function onBeforeDelete()
{
if (!DynamicCache::config()->cacheClearOnWrite) {
if (!DynamicCacheMiddleware::config()->cacheClearOnWrite) {
return;
}
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}

/**
* Support Versioned::publish()
* - Use case: SheaDawson Blocks module support
*/
public function onBeforeVersionedPublish() {
if (!DynamicCache::config()->cacheClearOnWrite) {
public function onBeforeVersionedPublish()
{
if (!DynamicCacheMiddleware::config()->cacheClearOnWrite) {
return;
}
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}

/**
* Support SiteTree::doPublish()
*/
public function onAfterPublish() {
if (!DynamicCache::config()->cacheClearOnWrite) {
public function onAfterPublish()
{
if (!DynamicCacheMiddleware::config()->cacheClearOnWrite) {
return;
}
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}

/**
* Support HeyDay's VersionedDataObject extension
* - Use case: DNADesign Elemental support
*/
public function onAfterVersionedPublish() {
if (!DynamicCache::config()->cacheClearOnWrite) {
public function onAfterVersionedPublish()
{
if (!DynamicCacheMiddleware::config()->cacheClearOnWrite) {
return;
}
DynamicCache::inst()->clear();
DynamicCacheMiddleware::inst()->clear();
}

protected function hasLiveStage() {
$class = $this->owner->class;
protected function hasLiveStage()
{
// NOTE: Using has_extension over hasExtension as the former
// takes subclasses into account.
$hasVersioned = $class::has_extension('Versioned');
if (!$hasVersioned) {
$hasVersioned = $this->owner->hasExtension(Versioned::class);
if (!$this->owner->hasExtension(Versioned::class)) {
return false;
}
$stages = $this->owner->getVersionedStages();
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

namespace TractorCow\DynamicCache\Extension;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension;


/**
* Abstract extension class to customise the behaviour of DynamicCache.
*
@@ -16,7 +22,7 @@ abstract class DynamicCacheExtension extends Extension
* @param boolean &$enabled Out parameter containing the current $enabled flag. The initial value of this will
* be the result of DynamicCache's internal rules.
*/
public function updateEnabled(&$enabled)
public function updateEnabled(&$enabled, HTTPRequest $request)
{
}

4 changes: 4 additions & 0 deletions tests/DynamicCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace TractorCow\DynamicCache\Tests;

use SilverStripe\Dev\SapphireTest;

class DynamicCacheTest extends SapphireTest
{
public function testOptInURL()