public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, \DataModel $model)
 {
     if ($request->getVar('clear') && Member::currentUserID() && Permission::check('ADMIN')) {
         $key = trim($request->getVar('url'), '/');
         $key = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . '/' . $key;
         $item = $this->dynamicCache->get($key);
         if ($item) {
             $response->addHeader('X-SilverStripe-Cache', 'deleted ' . $key);
             $this->dynamicCache->delete($key);
         }
     }
 }
 public function __construct($cache = null)
 {
     if (is_null($cache)) {
         $cache = SimpleCache::instance();
     }
     $this->_cacheImpl = $cache;
 }
 /**
  * Returns a collection of MetaDataRelation objects based on the relations mask generated by the four parameters
  * <code>
  * $relations = MetaDataRelations::getRelations(1); 
  * // returns relations for organisation_id 1
  * 
  * $relations = MetaDataRelations::getRelations(1, "student"); 
  * // returns relations for members of the "student" group in organisation_id 1
  * </code>
  * @param int $organisation
  * @param string $group
  * @param string $role
  * @param int $user Proxy ID
  * @return MetaDataRelations
  */
 public static function getRelations($organisation = null, $group = null, $role = null, $user = null)
 {
     $cache = SimpleCache::getCache();
     $relation_set = $cache->get("MetaTypeRelation", "{$organization}-{$group}-{$role}-{$user}");
     if ($relation_set) {
         return $relation_set;
     }
     global $db;
     $conditions = generateMaskConditions($organisation, $group, $role, $user);
     $query = "SELECT * from `meta_type_relations`";
     if ($conditions) {
         $query .= "\n WHERE " . $conditions;
     }
     $results = $db->getAll($query);
     $relations = array();
     if ($results) {
         foreach ($results as $result) {
             $relation = MetaDataRelation::fromArray($result);
             $relations[] = $relation;
         }
     }
     $relation_set = new self($relations);
     $cache->set($relation_set, "MetaTypeRelation", "{$organization}-{$group}-{$role}-{$user}");
     return $relation_set;
 }
 public function testClearThousands()
 {
     Filesystem::removeFolder(TEMP_FOLDER . '/my_test_cache');
     $cache = new SimpleCache(new SimpleFileBasedCacheStore(TEMP_FOLDER . '/my_test_cache'));
     for ($i = 0; $i < 1000; $i++) {
         $object = new stdClass();
         $object->Title = "Object {$i}";
         $other = $i % 10;
         $cache->store('key_' . $i, $object, -1, array('mytag', "mod{$other}"));
     }
     $start = microtime(true);
     $cache->deleteByTag('mytag');
     $end = microtime(true) - $start;
     $elems = $cache->getByTag('mytag');
     $this->assertEquals(0, count($elems));
 }
 public function forTemplate()
 {
     $key = $this->getKey();
     $cache = $this->getCache();
     $data = $cache->get($key);
     if ($data && !$this->forceRegen) {
         if (defined('PROXY_CACHE_GENERATING')) {
             return '<!--SimpleCache::' . $key . ',FragmentCache-->';
         }
         return $data;
     }
     $data = $this->context->renderWith($this->templateName);
     $data = $data->raw();
     if (Versioned::current_stage() != 'Stage' && strlen($data) && !isset($_GET['flush'])) {
         //			$this->cache->store($key, $menu, self::CACHE_LENGTH);
         // when storing for the frontend cache replacement stuff, use whatever we have
         // configured
         // do NOT store if we're on stage or we have _flush=1
         $this->cache->store($key, $data);
     }
     // clear the cache key for next request
     if (isset($_GET['flush'])) {
         $this->cache->delete($key);
     }
     if (defined('PROXY_CACHE_GENERATING')) {
         return '<!--SimpleCache::' . $key . ',FragmentCache-->';
     }
     return $data;
 }
Exemple #6
0
 public function singleton()
 {
     if (!isset(self::$instance)) {
         $c = __CLASS__;
         self::$instance = new $c();
     }
     return self::$instance;
 }
 public static function init()
 {
     self::$gzip_level = (int) SIMPLE_CACHE_GZIP_LEVEL >= 0 && (int) SIMPLE_CACHE_GZIP_LEVEL <= 9 ? (int) SIMPLE_CACHE_GZIP_LEVEL : 0;
     //			self::$intervals['default'] = (int)SIMPLE_CACHE_DEFAULT_TIME;
     //			$cache_block_intervals = explode(',', SIMPLE_CACHE_BLOCK_TIME);
     //			foreach($cache_block_intervals as $cache_block_interval){
     //				$interval = explode(':', $cache_block_interval);
     //				self::$intervals[$cache_block_interval[0]] = (int)$cache_block_interval[1];
     //			}
     self::$cache_folder = DIR_FS_SQL_CACHE . '/simple_cache/';
     self::setStore(self::$cache_folder);
 }
function display_tencent($args = '')
{
    $default = array('username' => 'Weibo_ID', 'number' => '5', 'time' => '3600');
    $r = wp_parse_args($args, $default);
    extract($r);
    $path = dirname(dirname(__FILE__)) . '/wordpress-tencent-microblog/xxx';
    $cache = new SimpleCache();
    $cache->cache_path = $path;
    $cache->cache_time = 43200;
    date_default_timezone_set('PRC');
    if ($data = $cache->get_cache(date('Y-m-d-H'))) {
        $list = json_decode($data);
    } else {
        $uinfo = OpenSDK_Tencent_Weibo::call('statuses/broadcast_timeline', array('reqnum' => $number, 'type' => '1', 'contenttype' => '0'));
        $cache->set_cache(date('Y-m-d-H'), json_encode($uinfo));
    }
    $decodedArray = $list;
    echo '<ul style="list-style-type:none;">';
    foreach ($decodedArray['data']['info'] as $value) {
        echo '<li><div class="microblog"><a href="http://t.qq.com/' . $value['nick'] . '" rel="external nofollow" title="来自 腾讯微博" target="_blank" style="padding-right:3px;"><img class="microblog-ico"  alt="腾讯微博" src="' . WP_PLUGIN_URL . '/wordpress-tencent-microblog/txwb.png" /></a><span class="microblog-content">' . str_replace('&#160;', ' ', $value['origtext']) . '</span>  <span class="microblog-from" style="font-size:smaller;">-' . date("Y/m/d", $value['timestamp']) . ' 来自 ' . $value['from'] . '-</span></div></li>';
    }
    echo '</ul>';
}
 /**
  * Returns a MetaDataRelation object belonging to the specified id, if one exists.
  * @param int $meta_data_relation_id
  * @return MetaDataRelation
  */
 public function get($meta_data_relation_id)
 {
     $cache = SimpleCache::getCache();
     $relation = $cache->get("MetaDataRelation", $meta_data_relation_id);
     if (!$relation) {
         global $db;
         $query = "SELECT * FROM `meta_data_relations` WHERE `meta_data_relation_id` = ?";
         $result = $db->getRow($query, array($meta_data_relation_id));
         if ($result) {
             $relation = self::fromArray($result);
         }
     }
     return $relation;
 }
 public function generateCache($host, $url)
 {
     $url = $this->urlForCaching($url);
     $key = "{$host}/{$url}";
     define('PROXY_CACHE_GENERATING', true);
     $config = $this->configForUrl($url, $host);
     $expiry = isset($config['expiry']) ? $config['expiry'] : -1;
     if ($expiry < 0) {
         return;
     }
     ob_start();
     include BASE_PATH . '/framework/main.php';
     $toCache = new stdClass();
     $toCache->Content = ob_get_clean();
     $toCache->LastModified = date('Y-m-d H:i:s');
     // check headers to see if we have an expires header
     $headers = headers_list();
     if ($headerExpiry = $this->ageFromHeaders($headers, $expiry)) {
         if ($headerExpiry < $expiry && $headerExpiry != 0) {
             $expiry = $headerExpiry;
         }
     }
     $storedHeaders = $this->headersToStore($headers);
     if (count($storedHeaders)) {
         $toCache->Headers = $storedHeaders;
     }
     $toCache->Age = $expiry;
     if (function_exists('http_response_code')) {
         $response = http_response_code();
         $this->enabled = $response >= 200 && $response < 300;
     }
     // see if we've got a no cache header
     if (isset($storedHeaders['x-silverstripe-nocache'])) {
         $this->enabled = false;
     }
     // store the content for this
     if ($this->enabled && $this->dynamicCache && strlen($toCache->Content)) {
         $tags = isset($config['tags']) ? $config['tags'] : null;
         $this->dynamicCache->store($key, $toCache, $expiry, $tags);
         if ($this->includeCacheHeaders) {
             $this->headers[] = 'X-SilverStripe-Cache: miss-gen at ' . date('r');
         }
     }
     $this->currentItem = $toCache;
 }
 public function run($request)
 {
     if (!Director::is_cli()) {
         exit("Invalid\n");
     }
     $cacheName = $request->getVar('cache');
     $key = $request->getVar('key');
     if (!strlen($cacheName) || !strlen($key)) {
         exit("Cache and key required\n");
     }
     $cache = SimpleCache::get_cache($cacheName);
     if ($cache) {
         $data = $cache->get($key);
         if ($data) {
             var_export($data);
         }
     }
 }
 public function generateCache($host, $url)
 {
     $key = "{$host}/{$url}";
     define('PROXY_CACHE_GENERATING', true);
     $config = $this->configForUrl($url);
     $expiry = isset($config['expiry']) ? $config['expiry'] : -1;
     if ($expiry < 0) {
         return;
     }
     ob_start();
     include BASE_PATH . '/framework/main.php';
     $toCache = new stdClass();
     $toCache->Content = ob_get_clean();
     $toCache->LastModified = date('Y-m-d H:i:s');
     $toCache->Age = $expiry;
     // store the content for this
     if ($this->dynamicCache && strlen($toCache->Content)) {
         $tags = isset($config['tags']) ? $config['tags'] : null;
         $this->dynamicCache->store($key, $toCache, $expiry, $tags);
         $this->headers[] = 'X-SilverStripe-Cache: miss-gen at ' . @date('r') . ' on ' . $key;
     }
     $this->currentItem = $toCache;
 }
<?php

SimpleCache::$cache_configs = array('default' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('silverstripe-cache/cache_store'), 'cache_options' => array('expiry' => 3600)), 'PublisherCache' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('publisher_cache'), 'cache_options' => array('expiry' => 8640000)), 'DynamicPublisherCache' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('silverstripe-cache/dynamic_publisher'), 'cache_options' => array('expiry' => 3600)), 'FragmentCache' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('silverstripe-cache/fragment_cache'), 'cache_options' => array('expiry' => 864000)));
$PROXY_CACHE_URLS = array('news/(\\d+)/.*?$' => array('default' => 7200, 'tags' => array('news', 1)));
 /**
  * Returns the Organisation corresponding to the supplied ID
  * @param int $organisation_id
  * @return Organisation
  */
 static function get($organisation_id)
 {
     $cache = SimpleCache::getCache();
     $organisation = $cache->get("Organisation", $organisation_id);
     if (!$organisation) {
         global $db;
         $query = "SELECT * FROM `" . AUTH_DATABASE . "`.`organisations` WHERE `organisation_id` = " . $db->qstr($organisation_id);
         $result = $db->getRow($query);
         if ($result) {
             $organisation = new Organisation($result['organisation_id'], $result['organisation_title'], $result['organisation_address1'], $result['organisation_address2'], $result['organisation_city'], $result['organisation_province'], $result['organisation_country'], $result['organisation_postcode'], $result['organisation_telephone'], $result['organisation_fax'], $result['organisation_email'], $result['organisation_url'], $result['organisation_desc'], $result['aamc_institution_id'], $result['aamc_institution_name'], $result['aamc_program_id'], $result['aamc_program_name'], $result['organisation_active']);
         }
     }
     return $organisation;
 }
 /**
  * Returns the Course belonging to the specified ID
  * @param int $course_id
  * @return Course
  */
 public static function get($course_id)
 {
     $cache = SimpleCache::getCache();
     $course = $cache->get("Course", $course_id);
     if (!$course) {
         global $db;
         $query = "SELECT * FROM `courses` WHERE `course_id` = " . $db->qstr($course_id);
         $result = $db->getRow($query);
         if ($result) {
             $course = self::fromArray($result);
         }
     }
     return $course;
 }
 /**
  * Removes this Value from the dabatase
  */
 public function delete()
 {
     $cache = SimpleCache::getCache();
     $cache->remove("MetaValue", $this->meta_value_id);
     global $db;
     $query = "DELETE FROM `meta_values` where `meta_value_id`=?";
     if (!$db->Execute($query, array($this->meta_value_id))) {
         add_error("Failed to remove meta data from database.");
         application_log("error", "Unable to delete a meta_values record. Database said: " . $db->ErrorMsg());
     }
 }
 /**
  * Returns an Event specified by the provided ID 
  * @param unknown_type $event_id
  * @return unknown
  */
 public static function get($event_id)
 {
     $cache = SimpleCache::getCache();
     $event = $cache->get("Event", $event_id);
     if (!$user) {
         global $db;
         $query = "SELECT * FROM `events` WHERE `id` = " . $db->qstr($event_id);
         $result = $db->getRow($query);
         if ($result) {
             $event = new Event($result['event_id'], $result['recurring_id'], $result['region_id'], $result['course_id'], $result['event_phase'], $result['event_title'], $result['event_description'], $result['event_goals'], $result['event_objectives'], $result['event_message'], $result['event_location'], $result['event_start'], $result['event_finish'], $result['event_duration'], $result['release_date'], $result['release_until'], $result['updated_date'], $result['updated_by']);
         }
     }
     return $user;
 }
Exemple #18
0
        }
        ?>

<?php 
        if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MAIN_NEW_PRODUCTS') {
            /**
             * display the New Products Center Box
             */
            require $template->get_template_dir('tpl_modules_whats_new.php', DIR_WS_TEMPLATE, $current_page_base, 'templates') . '/tpl_modules_whats_new.php';
        }
        ?>

<?php 
        if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MAIN_UPCOMING') {
            /**
             * display the Upcoming Products Center Box
             */
            include DIR_WS_MODULES . zen_get_module_directory(FILENAME_UPCOMING_PRODUCTS);
        }
        ?>


<?php 
        $show_display_category->MoveNext();
    }
    // !EOF
    ?>
</div>
<?php 
    SimpleCache::End();
}
Exemple #19
0
<?php

require 'SimpleCache.php';
$cache = new SimpleCache('/path/to/cache/dir');
$feed = $cache->read('feed-phparch');
if (!$feed) {
    $feed = file_get_contents('http://www.phparch.com/feed/');
    $cache->save('feed-phparch', $feed, '5 minutes');
}
$xml = simplexml_load_string($feed);
foreach ($xml->channel->item as $item) {
    echo '- <a href="', $item->link, '">', $item->title, '</a><br />', PHP_EOL;
}
Exemple #20
0
<?php

require 'SimpleCache.php';
$cache = new SimpleCache('/path/to/cache1/dir');
$content = $cache->read('cache1');
if (!$content) {
    $content = mt_rand(1, 99999999);
    $cache->save('cache1', $content, '1 minute');
}
echo 'Cache 1: ', $content, '<br />';
$cache = new SimpleCache('/path/to/cache2/dir');
$content = $cache->read('cache2');
if (!$content) {
    $content = mt_rand(1, 99999999);
    $cache->save('cache2', $content, '2 minutes');
}
echo 'Cache 2: ', $content;
 /**
  * Reset the cache flushing it and returning it to its default size.
  */
 static function resetCache()
 {
     self::$cache = array();
     self::$entries = array();
     self::$size = self::CACHESIZE;
 }
 protected function getCache($name)
 {
     $cache = SimpleCache::get_cache($name);
     return $cache;
 }
}
// Remove base folders from the URL if webroot is hosted in a subfolder
if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) {
    $url = substr($url, strlen(BASE_URL));
}
if (defined('USE_PROXY') && USE_PROXY) {
    if (defined('PROXY_CONFIG_FILE')) {
        include_once BASE_PATH . '/' . PROXY_CONFIG_FILE;
    }
    $publisher = null;
    if (defined('PROXY_PUBLISHER')) {
        $publisher = SimpleCache::get_cache(PROXY_PUBLISHER);
    }
    $dynamic = null;
    if (defined('PROXY_DYNAMIC_PUBLISHER')) {
        $dynamic = SimpleCache::get_cache(PROXY_DYNAMIC_PUBLISHER);
    }
    $cookies = defined('PROXY_BYPASS_COOKIES') ? explode(',', PROXY_BYPASS_COOKIES) : array();
    $url_config = isset($PROXY_CACHE_URLS) ? $PROXY_CACHE_URLS : null;
    $blacklist = isset($PROXY_CACHE_BLACKLIST) ? $PROXY_CACHE_BLACKLIST : array();
    $proxyclass = defined('PROXY_CLASS') ? PROXY_CLASS : 'FrontendProxy';
    $proxy = new $proxyclass($publisher, $dynamic, $url_config, $cookies);
    $proxy->setCacheGetVars(defined('CACHE_ALLOW_GET_VARS') && CACHE_ALLOW_GET_VARS)->setIgnoreGetVars(defined('CACHE_IGNORE_GET_VARS') && CACHE_IGNORE_GET_VARS)->setBlacklist($blacklist);
    if (isset($PROXY_CACHE_REMAP)) {
        $proxy->setRemapHosts($PROXY_CACHE_REMAP);
    }
    if (defined('PROXY_CONTENT_REWRITER')) {
        $proxy->setContentRewriter(PROXY_CONTENT_REWRITER);
    }
    $host = $_SERVER['HTTP_HOST'];
    $trimFunc = defined('PROXY_TRIM_FUNC') ? PROXY_TRIM_FUNC : 'trim';
<?php

if (defined('PROXY_CONFIG_FILE')) {
    if (!isset(SimpleCache::$cache_configs['PublisherCache'])) {
        include_once BASE_PATH . '/' . PROXY_CONFIG_FILE;
    }
    SimpleCache::register_caches();
}
 /**
  * {@inheritDoc}
  *
  * @param string $key
  *
  * @return \DateTime|boolean
  **/
 public function getCacheExpiration($key)
 {
     return parent::getCacheExpiration($this->getHierarchicalKey($key));
 }
 /**
  * Clears the dynamic cached data for a particular data object
  * @param DataObject $object
  */
 public function clearDynamicCacheFor(DataObject $object)
 {
     $dynamicCache = SimpleCache::get_cache('DynamicPublisherCache');
     if ($dynamicCache) {
         $clearUrls = array($object->Link());
         if ($object->hasMethod('pagesAffectedByChanges')) {
             $affectedUrls = $object->pagesAffectedByChanges();
             if (count($affectedUrls)) {
                 foreach ($affectedUrls as $afUrl) {
                     $clearUrls[] = $afUrl;
                 }
             }
         }
         $clearUrls = array_unique($clearUrls);
         $baseUrl = $object->SiteID ? $object->Site()->getUrl() : Director::absoluteBaseURL();
         foreach ($clearUrls as $absolute) {
             if (strpos($absolute, '://') === false) {
                 $absolute = $baseUrl . '/' . trim($absolute, '/');
             }
             $parts = parse_url($absolute);
             $host = $parts['host'];
             $path = '/index';
             $path = isset($parts['path']) && $parts['path'] != '/' ? $parts['path'] : $path;
             $key = trim($parts['host'] . $path, '/');
             $dynamicCache->delete($key);
         }
     }
 }
 /**
  * Returns a MetaDataType corresponding to the provided ID
  * <code>
  * $type_id = 1234;
  * $type = MetaDataType::get($type_id);
  * </code>
  * @param int $meta_type_id
  * @return MetaDataType
  */
 public static function get($meta_type_id)
 {
     $cache = SimpleCache::getCache();
     $type = $cache->get("MetaDataType", $meta_type_id);
     if (!$type) {
         global $db;
         $query = "SELECT * FROM `meta_types` WHERE `meta_type_id` = ?";
         $result = $db->getRow($query, array($meta_type_id));
         if ($result) {
             $type = self::fromArray($result);
         }
     }
     return $type;
 }
 /**
  * Returns an Notification specified by the provided ID
  * @param int $notification_id
  * @return Notification
  */
 public static function get($notification_id)
 {
     global $db;
     $cache = SimpleCache::getCache();
     $notification = $cache->get("Notification", $notification_id);
     if (!$notification) {
         $query = "SELECT * FROM `notifications` WHERE `notification_id` = " . $db->qstr($notification_id);
         $result = $db->getRow($query);
         if ($result) {
             $notification = self::fromArray($result);
         }
     }
     return $notification;
 }
 /**
  * Returns a NotificationUser specified by the provided ID 
  * @param unknown_type $event_id
  * @return unknown
  */
 public static function getByID($nuser_id)
 {
     $cache = SimpleCache::getCache();
     $notification_user = $cache->get("NotificationUser", $nuser_id);
     if (!$notification_user) {
         global $db;
         $query = "SELECT * FROM `notification_users` WHERE `nuser_id` = " . $db->qstr($nuser_id);
         $result = $db->getRow($query);
         if ($result) {
             $notification_user = self::fromArray($result);
         }
     }
     return $notification_user;
 }
<?php

SimpleCache::$cache_configs = array('default' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('silverstripe-cache/cache_store'), 'cache_options' => array('expiry' => 3600)), 'publisher' => array('store_type' => 'SimpleFileBasedCacheStore', 'store_options' => array('silverstripe-cache/publisher'), 'cache_options' => array('expiry' => 8640000)));
/* 
 * A list of URLs (as regexes) that if matched, will have their content cached
 * for the given number of seconds
 */
$cache_urls = array('custom-controller/view/\\d+' => 900);
/**
 * a list of hosts we're restricting access to 
 */
$allowed_hosts = array('define.hosts.that.can.be.accessed.com');
include_once dirname(__FILE__) . '/cache-main-auth.php';