API Use symfony/cache

See https://github.com/silverstripe/silverstripe-framework/issues/6252
This commit is contained in:
Ingo Schommer 2017-02-22 14:34:07 +13:00 committed by Sam Minnée
parent dd5c050108
commit 6bae804744
3 changed files with 18 additions and 9 deletions

9
_config/cache.yml Normal file
View File

@ -0,0 +1,9 @@
---
After:
- '#corecache'
---
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.CMSMain_SiteTreeHints:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "CMSMain_SiteTreeHints"

View File

@ -21,7 +21,7 @@ use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Cache; use Psr\SimpleCache\CacheInterface;
use SilverStripe\Forms\DateField; use SilverStripe\Forms\DateField;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup; use SilverStripe\Forms\FieldGroup;
@ -59,7 +59,6 @@ use SilverStripe\Security\SecurityToken;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use Translatable; use Translatable;
use Zend_Cache;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
@ -556,12 +555,12 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
} }
// Generate basic cache key. Too complex to encompass all variations // Generate basic cache key. Too complex to encompass all variations
$cache = Cache::factory('CMSMain_SiteTreeHints'); $cache = Injector::inst()->get(CacheInterface::class . '.CMSMain_SiteTreeHints');
$cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes)))); $cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes))));
if ($this->getRequest()->getVar('flush')) { if ($this->getRequest()->getVar('flush')) {
$cache->clean(Zend_Cache::CLEANING_MODE_ALL); $cache->clear();
} }
$json = $cache->load($cacheKey); $json = $cache->get($cacheKey);
if (!$json) { if (!$json) {
$def['Root'] = array(); $def['Root'] = array();
$def['Root']['disallowedChildren'] = array(); $def['Root']['disallowedChildren'] = array();
@ -608,7 +607,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$this->extend('updateSiteTreeHints', $def); $this->extend('updateSiteTreeHints', $def);
$json = Convert::raw2json($def); $json = Convert::raw2json($def);
$cache->save($json, $cacheKey); $cache->set($cacheKey, $json);
} }
return $json; return $json;
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException; use SilverStripe\ORM\ValidationException;
@ -9,7 +10,7 @@ use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Admin\CMSBatchActionHandler; use SilverStripe\Admin\CMSBatchActionHandler;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Core\Cache; use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
@ -45,11 +46,11 @@ class CMSMainTest extends FunctionalTest
public function testSiteTreeHints() public function testSiteTreeHints()
{ {
$cache = Cache::factory('CMSMain_SiteTreeHints'); $cache = Injector::inst()->get(CacheInterface::class . '.CMSMain_SiteTreeHints');
// Login as user with root creation privileges // Login as user with root creation privileges
$user = $this->objFromFixture('SilverStripe\\Security\\Member', 'rootedituser'); $user = $this->objFromFixture('SilverStripe\\Security\\Member', 'rootedituser');
$user->logIn(); $user->logIn();
$cache->clean(Zend_Cache::CLEANING_MODE_ALL); $cache->clear();
$rawHints = singleton('SilverStripe\\CMS\\Controllers\\CMSMain')->SiteTreeHints(); $rawHints = singleton('SilverStripe\\CMS\\Controllers\\CMSMain')->SiteTreeHints();
$this->assertNotNull($rawHints); $this->assertNotNull($rawHints);