/**
  * 
  * Sets internal variables and persistent data for when job is created without
  * constructor params, and process() is called in {@link QueuedJobService}.
  * 
  * @param CacheableNavigationService $service
  * @param array $chunk                          An array of objects to cache
  * @param string $stage                         "Live" or "Stage"
  * @param number $subsiteID
  * @return void
  */
 public function __construct(CacheableNavigationService $service, $chunk, $stage, $subsiteID)
 {
     // Increase memory to max-allowable
     CacheableConfig::configure_memory_limit();
     // Setters required for internal methods except $this->process()
     $this->setService($service);
     $this->setChunk($chunk);
     $this->setStage($stage);
     $this->setSubsiteID($subsiteID);
     // Persist structured "metadata" about the job using {@link CachableChunkedRefreshJobStorageService}.
     $jobConfig = array('CachableChunkedRefreshJobStorageService' => array('service' => $this->getService(), 'chunk' => $this->getChunk()));
     $this->setCustomConfig($jobConfig);
     $this->totalSteps = $this->chunkSize();
 }
 /**
  * 
  * Generate key information about the object cache.
  * 
  * @return HTMLText
  */
 public function inspect()
 {
     $backend = ucfirst(CacheableConfig::current_cache_mode());
     $backendData['PHPMemoryLimit'] = ini_get('memory_limit');
     switch ($backend) {
         // File backend
         case 'File':
             $fileList = ArrayList::create(Cacheable::get_cache_files());
             $backendData['FileTotal'] = $fileList->count();
             $backendData['FileList'] = $fileList;
             $backendData['FileCacheDir'] = CACHEABLE_STORE_DIR;
             $backendData['FileSizeOnDisk'] = round($this->getCacheSize() / 1024, 2) . 'Kb';
             $backendData['CacheLastEdited'] = $this->getCacheLastUpdated();
             break;
             // Memcached backend
             // TODO
         // Memcached backend
         // TODO
         case 'memcached':
             // Memcache backend
             // TODO
         // Memcache backend
         // TODO
         case 'memcache':
             // APCu backend
             // TODO
         // APCu backend
         // TODO
         case 'apc':
             break;
     }
     $comparisonData = array('StatusList' => ArrayList::create(array($this->cacheToORMCompareDataObject('Stage', 'SiteTree'), $this->cacheToORMCompareDataObject('Live', 'SiteTree'))));
     $templateLocal = 'BackendData_' . $backend;
     $templateAbsolute = __DIR__ . '/../../templates/includes/' . $templateLocal . '.ss';
     $bData = '';
     if (file_exists($templateAbsolute)) {
         $bData = $this->renderWith($templateLocal, $backendData);
     }
     $backendData = array_merge(array('BackEndMode' => $backend), array('BackEndDataList' => $bData));
     $viewable = array_merge($comparisonData, $backendData);
     return $this->renderWith('Inspector', $viewable);
 }
 public function testCacheDirPath()
 {
     // Default:
     Config::inst()->remove('CacheableConfig', 'alt_cache_dir');
     $this->assertEquals(TEMP_FOLDER . '/cacheable', CacheableConfig::cache_dir_path());
     // Userland - no hierarchy
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', 'cacheable');
     $this->assertEquals(ASSETS_PATH . '/_cacheable', CacheableConfig::cache_dir_path());
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', ' cacheable');
     $this->assertEquals(ASSETS_PATH . '/_cacheable', CacheableConfig::cache_dir_path());
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', ' cacheable/');
     $this->assertEquals(ASSETS_PATH . '/_cacheable', CacheableConfig::cache_dir_path());
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', '/cacheable/');
     $this->assertEquals(ASSETS_PATH . '/_cacheable', CacheableConfig::cache_dir_path());
     // Userland - yes hierarchy, variations
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', '/foo/bar');
     $this->assertEquals(ASSETS_PATH . '/_foo/bar/cacheable', CacheableConfig::cache_dir_path());
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', '/foo/bar/');
     $this->assertEquals(ASSETS_PATH . '/_foo/bar/cacheable', CacheableConfig::cache_dir_path());
     Config::inst()->update('CacheableConfig', 'alt_cache_dir', 'foo/bar/');
     $this->assertEquals(ASSETS_PATH . '/_foo/bar/cacheable', CacheableConfig::cache_dir_path());
 }
 /**
  * 
  * Summarise the task's configuration details at the end of a run.
  * 
  * @param string $totalTime
  * @param SS_HTTPRequest $request
  * @param boolean $lowCount         If the total no. pages is greater than 
  *                                  the chunk divisor, we don't attempt to chunk
  *                                  and let the user know        
  * @return void
  */
 public function showConfig($totalTime, $request, $lowCount = false)
 {
     $skipQueue = $request->getVar('SkipQueue') && $request->getVar('SkipQueue') == 1;
     $queueOn = interface_exists('QueuedJob') ? 'On' : 'Off';
     $queueSkipped = $skipQueue ? ' (Skipped: User instruction)' : '';
     if ($lowCount && !$skipQueue) {
         $queueSkipped = ' (Skipped: Low page count)';
     }
     /**
      * Is the system underpowered enough such that {@link CacheableConfig::configure_memory_limit()}
      * has kicked-in? Notify the user accordingly.
      */
     $memMode = CacheableConfig::$ini_modified_memory_limit ? 'Auto-modified' : 'PHP Default';
     echo 'Job Queue: ' . $queueOn . $queueSkipped . self::new_line();
     echo 'Cache backend: ' . CacheableConfig::current_cache_mode() . self::new_line();
     echo 'Peak memory: ' . $this->memory() . 'Mb' . self::new_line();
     echo 'Execution time: ' . $totalTime . 's' . self::new_line();
     echo 'System memory_limit: ' . ini_get('memory_limit') . ' (' . $memMode . ')' . self::new_line();
     echo 'Cache directory: ' . CACHEABLE_STORE_DIR . self::new_line();
 }
 /**
  * 
  * @return boolean True if APCu is installed as an extension and is enabled in php.ini
  */
 public static function configure_apc()
 {
     $isApcEnabled = extension_loaded('apc') && ini_get('apc.enabled') == 1;
     if (!$isApcEnabled) {
         return false;
     }
     SS_Cache::add_backend(CACHEABLE_STORE_NAME, 'Apc');
     self::$current_mode = 'apc';
     return true;
 }
 /**
  * Allows us to clear a specific cache (particulalrly test-caches) based on 
  * Zend_Cache's tagging system.
  * 
  * @return string
  */
 private static function get_default_cache_tag()
 {
     return CacheableConfig::is_running_test() ? CACHEABLE_STORE_TAG_DEFAULT_TEST : CACHEABLE_STORE_TAG_DEFAULT;
 }
<?php

/**
 * 
 * @author Deviate Ltd 2014-2015 http://www.deviate.net.nz
 * @package silverstripe-cachable
 * 
 * Configure the module's storage:
 * 
 * The default is to use "file" for the cache store via {@link Zend_Cache_Backend_File}, 
 * but this can be overriden in YML config. See the README for more options.
 */
define('CACHEABLE_STORE_DIR_NAME', 'cacheable');
define('CACHEABLE_STORE_DIR_TEST', TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cacheable_tests');
define('CACHEABLE_STORE_NAME', 'cacheablestore');
define('CACHEABLE_STORE_FOR', 'Cacheable');
define('CACHEABLE_STORE_TAG_DEFAULT', 'cacheable_tag_nav');
// Default Zend tag name for this cache
define('CACHEABLE_STORE_TAG_DEFAULT_TEST', 'cacheable_tag_nav_test');
define('CACHEABLE_STORE_WEIGHT', 1000);
define('CACHEABLE_STORE_DIR', CacheableConfig::cache_dir_path());
define('CACHEABLE_MODULE_DIR', __DIR__);
CacheableConfig::configure();
SS_Cache::pick_backend(CACHEABLE_STORE_NAME, CACHEABLE_STORE_FOR, CACHEABLE_STORE_WEIGHT);